Gradle 安装 + 配置 + 构建大型项目(CAS)

作者: judasn 分类: SSO 发布时间: 2017-12-18 10:12

Gradle 安装(Windows)

  • 官网下载:https://services.gradle.org/distributions/
    • 然后找这类文件命名格式:gradle-4.4-bin.zip 下载下来(当前最新版本为 4.4)
  • 官网安装说明:https://gradle.org/install/
  • 解压 Gradle 到自己电脑某个地方,比如我的地址是 D:\gradle\gradle-4.4(zip 压缩包不要丢掉,后面 CAS 应用要用到)
  • 配置系统变量:GRADLE_HOME,值为:D:\gradle\gradle-4.4
  • Path 新增值:;%GRADLE_HOME%\bin
  • 打开 cmd 输入:gradle -v,可以显示 Gradle 版本介绍信息就表示安装成功。

Gradle 配置(Windows)

  • 新建系统变量:GRADLE_USER_HOME,值为:D:\gradle\gradle_local_repository
  • 新建一个文件:C:\Users\用户名\.gradle\gradle.properties,里面增加下面内容:
org.gradle.jvmargs=-Xms1048m -Xmx4048m -XX:MaxPermSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.daemon=true

Gradle 在 IntelliJ IDEA 构建大型项目流程

  • 这里以 CAS 项目为例,CAS 的源码有两百多个模块,整个 clone 下来有 1.5G 左右。
  • 计算机内存至少 8G,最佳 16G 以上。
  • Gradle 比 Maven 耗 CPU 和内存。
  • CAS 也有针对这个问题写一篇文章,大家也可以参考这个:https://apereo.github.io/cas/developer/Build-Process.html

设置项目的 Gradle 配置文件

  • 先不要打开 IntelliJ IDEA。
  • 先查看 CAS 官网用哪个版本的 Gradle
  • 找到 CAS 根目录下的 gradle.properties 文件,检查 VM 参数设置:org.gradle.jvmargs,VM 最大值 4G ~ 6G 左右为佳。
  • 找到 CAS 根目录下:\gradle\wrapper\ 目录下的 gradle-wrapper.properties 文件,把下面值改为前面让你下载 zip 的路径:
distributionUrl=file:///D:/gradle/gradle-4.4-bin.zip
  • 接着打开 CAS 根目录下的 build.gradle 文件,设置依赖仓库。
  • build.gradle 文件中找到这类配置(有好几个地方,自己 Ctrl + F 搜索下):
repositories {
    mavenLocal() 
    mavenCentral() 
    maven { url "https://maven.eveoh.nl/content/repositories/releases" }
    maven { url "https://plugins.gradle.org/m2/" }
    maven { url "https://repo.spring.io/plugins-release" }
    jcenter()
}
  • 然后改为这样:
repositories {
    mavenLocal() 
    maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } 
    mavenCentral() 
    maven { url "https://maven.eveoh.nl/content/repositories/releases" }
    maven { url "https://plugins.gradle.org/m2/" }
    maven { url "https://repo.spring.io/plugins-release" }
    jcenter()
}
  • Gradle 依赖配置是按书写顺序来的,所以 mavenLocal() Maven 的本地仓库肯定是优先的,接下来就是国内的阿里云镜像,接着才是其他源。

配置 IntelliJ IDEA 的 VM

  • 现在打开 IntelliJ IDEA,但是不要用 IntelliJ IDEA 打开 CAS 项目。
  • 然后点击菜单中的 Help -> Edit Custom VM Options 设置 VM,最重要的参数修改为如下:
-Xms2028m
-Xmx8050m
  • 或者参考 CAS 官网提供的参数:
-server
-Xms2g
-Xmx8g
-XX:NewRatio=3
-Xss16m
-XX:+UseConcMarkSweepGC
-XX:+CMSParallelRemarkEnabled
-XX:ConcGCThreads=4
-XX:ReservedCodeCacheSize=840m
-XX:+AlwaysPreTouch
-XX:+TieredCompilation
-XX:+UseCompressedOops
-XX:SoftRefLRUPolicyMSPerMB=50
-Dsun.io.useCanonCaches=false
-Djava.net.preferIPv4Stack=true
-Djsse.enableSNIExtension=false
-ea
-XX:MaxPermSize=512m
-XX:PermSize=512m
-Xverify:none
  • 如果要了解更多 VM 设置说明可以看这份 资料链接
  • 然后重启 IntelliJ IDEA

通过 IntelliJ IDEA 打开 CAS 项目

  • 打开 IntelliJ IDEA,然后 File -> Open,选中 CAS 根目录进行打开,此时会出现这样的选择,如下图:

Gradle CAS

  • 请选择 Use default gradle wrapper (recommended)
  • 接着选择 OK,IntelliJ IDEA 会开始构建 CAS 项目,预计需要 10 ~ 30 分钟不等。

CAS 项目额外还可以继续优化的地方

  • 下面这些工作是额外的,不这样做,CAS 也是可以构建成功的,只是比较卡而已。
  • 关闭部分插件,配置文件在 CAS 根目录的:tasks.gradle
  • 排除部分模块,配置文件在 CAS 根目录的:settings.gradle,比如下面我的配置(请自行判断):
rootProject.name = "cas-server"

include "api:cas-server-core-api-configuration"
include "api:cas-server-core-api-util"
include "api:cas-server-core-api-authentication"
include "api:cas-server-core-api-logout"
include "api:cas-server-core-api-monitor"
include "api:cas-server-core-api-services"
include "api:cas-server-core-api-ticket"
include "api:cas-server-core-api-validation"
include "api:cas-server-core-api-web"
include "api:cas-server-core-api"
include "api:cas-server-core-api-protocol"
include "api:cas-server-core-api-events"
include "api:cas-server-core-api-webflow"

include "core:cas-server-core-util-api"
include "core:cas-server-core-util"
include "core:cas-server-core-webflow-api"
include "core:cas-server-core-webflow"
include "core:cas-server-core-web-api"
include "core:cas-server-core-web"
include "core:cas-server-core-logging-config-cloudwatch"
include "core:cas-server-core-logging-config-splunk"
include "core:cas-server-core-logging-config"
include "core:cas-server-core-logging"
include "core:cas-server-core-logout-api"
include "core:cas-server-core-logout"
include "core:cas-server-core-monitor"
include "core:cas-server-core-services-api"
include "core:cas-server-core-services-authentication"
include "core:cas-server-core-services-registry"
include "core:cas-server-core-services"
include "core:cas-server-core-tickets-api"
include "core:cas-server-core-tickets"
include "core:cas-server-core-validation-api"
include "core:cas-server-core-validation"
include "core:cas-server-core-cookie-api"
include "core:cas-server-core-cookie"
include "core:cas-server-core-events"
include "core:cas-server-core-events-configuration"
include "core:cas-server-core-events-configuration-cloud-bus"
include "core:cas-server-core-audit"
include "core:cas-server-core-configuration-api"
include "core:cas-server-core-configuration"
include "core:cas-server-core-configuration-metadata-repository"
include "core:cas-server-core-configuration-cloud-azure-keyvault"
include "core:cas-server-core-configuration-cloud-jdbc"
include "core:cas-server-core-configuration-cloud-amqp"
include "core:cas-server-core-configuration-cloud-kafka"
include "core:cas-server-core-configuration-cloud-vault"
include "core:cas-server-core-configuration-cloud-mongo"
include "core:cas-server-core-configuration-cloud-dynamodb"
include "core:cas-server-core-configuration-cloud-zookeeper"
include "core:cas-server-core-authentication-attributes"
include "core:cas-server-core-authentication-api"
include "core:cas-server-core-authentication"
include "core:cas-server-core-authentication-mfa"
include "core:cas-server-core"

include "docs:cas-server-documentation"
include "docs:cas-server-documentation-swagger"

include "support:cas-server-support-bom"
include "support:cas-server-support-discovery-profile"
include "support:cas-server-support-person-directory"
include "support:cas-server-support-bootadmin-client"
include "support:cas-server-support-jdbc-drivers"
include "support:cas-server-support-metrics"
include "support:cas-server-support-audit-mongo"
include "support:cas-server-support-audit-jdbc"
include "support:cas-server-support-influxdb-core"
include "support:cas-server-support-cosmosdb-core"
include "support:cas-server-support-eureka-client"
include "support:cas-server-support-consul-client"
include "support:cas-server-support-generic"
include "support:cas-server-support-generic-remote-webflow"
include "support:cas-server-support-jdbc-authentication"
include "support:cas-server-support-jdbc"
include "support:cas-server-support-configuration"
include "support:cas-server-support-jpa-util"
include "support:cas-server-support-geolocation"
include "support:cas-server-support-geolocation-googlemaps"
include "support:cas-server-support-geolocation-maxmind"
include "support:cas-server-support-ldap"
include "support:cas-server-support-ldap-core"
include "support:cas-server-support-token-core"
include "support:cas-server-support-token-authentication"
include "support:cas-server-support-token-tickets"
include "support:cas-server-support-token-webflow"
include "support:cas-server-support-trusted-mfa"
include "support:cas-server-support-oauth-api"
include "support:cas-server-support-oauth-core"
include "support:cas-server-support-oauth-services"
include "support:cas-server-support-oauth"
include "support:cas-server-support-oauth-webflow"
include "support:cas-server-support-pac4j-core-clients"
include "support:cas-server-support-pac4j"
include "support:cas-server-support-pac4j-webflow"
include "support:cas-server-support-mongo-core"
include "support:cas-server-support-mongo"
include "support:cas-server-support-saml-core"
include "support:cas-server-support-saml"
include "support:cas-server-support-saml-idp-core"
include "support:cas-server-support-saml-idp"
include "support:cas-server-support-saml-idp-metadata-mongo"
include "support:cas-server-support-saml-idp-metadata-jpa"
include "support:cas-server-support-saml-googleapps-core"
include "support:cas-server-support-saml-googleapps"
include "support:cas-server-support-saml-mdui-core"
include "support:cas-server-support-saml-mdui"
include "support:cas-server-support-saml-sp-integrations"
include "support:cas-server-support-memcached-core"
include "support:cas-server-support-redis-core"
include "support:cas-server-support-pac4j-authentication"
include "support:cas-server-support-pac4j-core"
include "support:cas-server-support-captcha"
include "support:cas-server-support-rest"
include "support:cas-server-support-rest-tokens"
include "support:cas-server-support-rest-services"
include "support:cas-server-support-rest-authentication"
include "support:cas-server-support-hazelcast-core"
include "support:cas-server-support-reports"
include "support:cas-server-support-themes"
include "support:cas-server-support-throttle-core"
include "support:cas-server-support-throttle"
include "support:cas-server-support-throttle-jdbc"
include "support:cas-server-support-validation"
include "support:cas-server-support-actions"
//include "support:cas-server-support-electrofence"
//include "support:cas-server-support-sentry"
//include "support:cas-server-support-sleuth"
//include "support:cas-server-support-cloud-directory-authentication"
//include "support:cas-server-support-interrupt-api"
//include "support:cas-server-support-interrupt-core"
//include "support:cas-server-support-interrupt-webflow"
//include "support:cas-server-support-openid"
//include "support:cas-server-support-openid-webflow"
//include "support:cas-server-support-wsfederation"
//include "support:cas-server-support-wsfederation-webflow"
//include "support:cas-server-support-sms-twilio"
//include "support:cas-server-support-sms-textmagic"
//include "support:cas-server-support-sms-clickatell"
//include "support:cas-server-support-fortress"
//include "support:cas-server-support-basic"
//include "support:cas-server-support-scim"
//include "support:cas-server-support-gua"
//include "support:cas-server-support-spnego"
//include "support:cas-server-support-spnego-webflow"
//include "support:cas-server-support-trusted-mfa-jdbc"
//include "support:cas-server-support-trusted-mfa-mongo"
//include "support:cas-server-support-trusted-mfa-rest"
//include "support:cas-server-support-trusted"
//include "support:cas-server-support-trusted-webflow"
//include "support:cas-server-support-digest-authentication"
//include "support:cas-server-support-x509"
//include "support:cas-server-support-x509-core"
//include "support:cas-server-support-x509-webflow"
//include "support:cas-server-support-oidc-core"
//include "support:cas-server-support-oidc-services"
//include "support:cas-server-support-oidc"
//include "support:cas-server-support-cassandra-core"
//include "support:cas-server-support-cassandra-authentication"
//include "support:cas-server-support-couchbase-core"
//include "support:cas-server-support-couchbase-authentication"
//include "support:cas-server-support-ws-sts"
//include "support:cas-server-support-ws-idp-api"
//include "support:cas-server-support-ws-idp"
//include "support:cas-server-support-shibboleth"
//include "support:cas-server-support-otp-mfa"
//include "support:cas-server-support-grouper-core"
//include "support:cas-server-support-grouper"
//include "support:cas-server-support-shell"
//include "support:cas-server-support-rest-x509"
//include "support:cas-server-support-aup-core"
//include "support:cas-server-support-aup-mongo"
//include "support:cas-server-support-aup-jdbc"
//include "support:cas-server-support-aup-rest"
//include "support:cas-server-support-aup-ldap"
//include "support:cas-server-support-aup-webflow"

/*Multifactor Authentication start*/
//include "support:cas-server-support-yubikey-core"
//include "support:cas-server-support-yubikey-core-mfa"
//include "support:cas-server-support-yubikey-jpa"
//include "support:cas-server-support-yubikey-mongo"
//include "support:cas-server-support-yubikey"
//include "support:cas-server-support-duo-core-mfa"
//include "support:cas-server-support-duo-core"
//include "support:cas-server-support-duo"
//include "support:cas-server-support-authy-core"
//include "support:cas-server-support-authy"
//include "support:cas-server-support-swivel-core"
//include "support:cas-server-support-swivel"
//include "support:cas-server-support-azure-core"
//include "support:cas-server-support-azure"
//include "support:cas-server-support-azure-ad-authentication"
//include "support:cas-server-support-gauth-core-mfa"
//include "support:cas-server-support-gauth-core"
//include "support:cas-server-support-gauth"
//include "support:cas-server-support-gauth-jpa"
//include "support:cas-server-support-gauth-mongo"
//include "support:cas-server-support-radius-core"
//include "support:cas-server-support-radius"
//include "support:cas-server-support-radius-core-mfa"
//include "support:cas-server-support-radius-mfa"
//include "support:cas-server-support-u2f-core"
//include "support:cas-server-support-u2f"
//include "support:cas-server-support-u2f-jpa"
//include "support:cas-server-support-u2f-mongo"
/*Multifactor Authentication start*/


/*Service Registry start*/
include "support:cas-server-support-mongo-service-registry"
include "support:cas-server-support-json-service-registry"
include "support:cas-server-support-jpa-service-registry"
//include "support:cas-server-support-rest-service-registry"
//include "support:cas-server-support-yaml-service-registry"
//include "support:cas-server-support-cosmosdb-service-registry"
//include "support:cas-server-support-dynamodb-service-registry"
//include "support:cas-server-support-service-registry-stream"
//include "support:cas-server-support-service-registry-stream-hazelcast"
//include "support:cas-server-support-ldap-service-registry"
//include "support:cas-server-support-redis-service-registry"
//include "support:cas-server-support-couchbase-service-registry"
/*Service Registry end*/

/*Ticket Registry start*/
include "support:cas-server-support-redis-ticket-registry"
include "support:cas-server-support-memcached-ticket-registry"
//include "support:cas-server-support-dynamodb-ticket-registry"
//include "support:cas-server-support-jms-ticket-registry"
//include "support:cas-server-support-jpa-ticket-registry"
//include "support:cas-server-support-mongo-ticket-registry"
//include "support:cas-server-support-couchbase-ticket-registry"
//include "support:cas-server-support-ignite-ticket-registry"
//include "support:cas-server-support-infinispan-ticket-registry"
//include "support:cas-server-support-ehcache-ticket-registry"
//include "support:cas-server-support-hazelcast-ticket-registry"
/*Ticket Registry end*/

/*Monitoring start*/
//include "support:cas-server-support-jdbc-monitor"
//include "support:cas-server-support-ldap-monitor"
//include "support:cas-server-support-mongo-monitor"
//include "support:cas-server-support-memcached-monitor"
//include "support:cas-server-support-ehcache-monitor"
//include "support:cas-server-support-hazelcast-monitor"
/*Monitoring end*/

/*events start*/
//include "support:cas-server-support-events-mongo"
//include "support:cas-server-support-events-jpa"
//include "support:cas-server-support-events-memory"
//include "support:cas-server-support-events-influxdb"
/*events end*/

/*Attribute Consent start*/
//include "support:cas-server-support-consent-api"
//include "support:cas-server-support-consent-core"
//include "support:cas-server-support-consent-jdbc"
//include "support:cas-server-support-consent-ldap"
//include "support:cas-server-support-consent-rest"
//include "support:cas-server-support-consent-webflow"
//include "support:cas-server-support-consent-mongo"
/*Attribute Consent end*/

/*Surrogate Authentication start*/
//include "support:cas-server-support-surrogate-api"
//include "support:cas-server-support-surrogate-authentication"
//include "support:cas-server-support-surrogate-authentication-ldap"
//include "support:cas-server-support-surrogate-authentication-jdbc"
//include "support:cas-server-support-surrogate-authentication-rest"
//include "support:cas-server-support-surrogate-webflow"
/*Surrogate Authentication end*/

/*Password Management start*/
include "support:cas-server-support-pm"
include "support:cas-server-support-pm-webflow"
//include "support:cas-server-support-pm-jdbc"
//include "support:cas-server-support-pm-ldap"
//include "support:cas-server-support-pm-rest"
/*Password Management end*/

include "webapp:cas-server-webapp-init"
include "webapp:cas-server-webapp-session-redis"
include "webapp:cas-server-webapp-session-hazelcast"
include "webapp:cas-server-webapp-session-mongo"
include "webapp:cas-server-webapp-config"
include "webapp:cas-server-webapp-config-security"
include "webapp:cas-server-webapp"
include "webapp:cas-server-webapp-config-server"
include "webapp:cas-server-webapp-eureka-server"
include "webapp:cas-server-webapp-bootadmin-server"
include "webapp:cas-server-webapp-tomcat"
include "webapp:cas-server-webapp-jetty"
include "webapp:cas-server-webapp-undertow"
  • 还有这个模块的这几个配置可以排除:\cas\support\cas-server-support-discovery-profile\build.gradle
//implementation project(":support:cas-server-support-oidc-services")
//implementation project(":support:cas-server-support-oidc-core")
//implementation project(":support:cas-server-support-ws-idp-api")

//implementation project(":support:cas-server-support-authy-core")
//implementation project(":support:cas-server-support-azure-core")
//implementation project(":support:cas-server-support-u2f-core")
//implementation project(":support:cas-server-support-swivel-core")
//implementation project(":support:cas-server-support-gauth-core-mfa")
//implementation project(":support:cas-server-support-duo-core-mfa")
//implementation project(":support:cas-server-support-yubikey-core-mfa")
//implementation project(":support:cas-server-support-radius-core-mfa")

希望对你有帮助!