Gradle是一個(gè)構(gòu)建工具浙宜,構(gòu)建包括依賴管理官辽、編譯、打包等過(guò)程粟瞬⊥停總結(jié)了Gradle和maven之間的主要區(qū)別:靈活性、用戶體驗(yàn)和依賴關(guān)系管理亩钟。
依賴管理
1.申明簡(jiǎn)潔
<!-- pom.xml scope-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
// build.gradle configuration
dependencies {
testCompile 'junit:junit:4.12'
}
2.依賴顆粒度
Maven | When available | Leaks into consumers’ compile time | Leaks into consumers’ runtime | Included in Artifact |
---|---|---|---|---|
compile |
compile time, runtime乓梨, test鳖轰, compile time, test runtime | yes | yes | yes |
provided |
compile time, runtime test 扶镀,compile time, test runtime | no | no | no |
runtime |
runtime test, runtime | no | yes | yes |
test |
test compile, test runtime | no | no | no |
system | ||||
import |
Gradle | When available | Leaks into consumers’ compile time | Leaks into consumers’ runtime | Included in Artifact |
---|---|---|---|---|
implementation |
compile time, runtime test compile time, test runtime | no | yes | yes |
api |
compile time蕴侣, runtime test compile time, test runtime | yes | no | no |
compileOnly |
compile time | no | no | yes |
runtimeOnly |
runtime | no | yes | yes |
testImplementation |
test compile time, test runtime | no | no | no |
testCompileOnly |
test compile time | no | no | no |
testRuntimeOnly |
test runtime | no | no | no |
3.靈活性
3.1以一個(gè)常見(jiàn)的JAVA日志庫(kù)沖突問(wèn)題為例
image-20201113134242866.png
image-20201113134242866.png
<!-- pom.xml -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>2.1.0</version>
<exclusions>
<exclusion>
<artifactId>log4j-slf4j-impl</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
// build.gradle
// 全局排除依賴
configurations {
// 支持通過(guò)group、module排除臭觉,可以同時(shí)使用
all*.exclude group: 'commons-logging', module: 'commons-logging' // common-logging
all*.exclude group: 'log4j', module: 'log4j' // log4j
all*.exclude group: 'org.apache.logging.log4j', module: 'log4j-core' // slf4j -> log4j2
all*.exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl' // log4j2
all*.exclude group: 'org.slf4j', module: 'slf4j-jdk14' // slf4j -> jdk-logging
all*.exclude group: 'org.slf4j', module: 'slf4j-jcl' // slf4j -> common-logging
all*.exclude group: 'org.slf4j', module: 'slf4j-log4j12' // slf4j -> log4j
}
// 引入依賴 所有類型的日志庫(kù)都橋接到 slf4j 實(shí)現(xiàn)庫(kù)為logback
dependencies {
// log
compile "org.slf4j:slf4j-api:$slf4j_version"
compile "org.slf4j:jul-to-slf4j:$slf4j_version"
compile "org.slf4j:jcl-over-slf4j:$slf4j_version"
compile "org.slf4j:log4j-over-slf4j:$slf4j_version"
compile "org.apache.logging.log4j:log4j-api:$log4j2_version"
compile "org.apache.logging.log4j:log4j-to-slf4j:$log4j2_version"
compile "ch.qos.logback:logback-classic:$logback_version"
}
3.2 依賴?yán)【彺娌呗?/h3>
configurations.all {
//每隔24小時(shí)檢查遠(yuǎn)程依賴是否存在更新
resolutionStrategy.cacheChangingModulesFor 24, 'hours'
//每隔10分鐘..
// resolutionStrategy.cacheChangingModulesFor 10, 'minutes'
// 采用動(dòng)態(tài)版本聲明的依賴緩存10分鐘
// resolutionStrategy.cacheDynamicVersionsFor 10*60, 'seconds'
}
3.3 依賴傳遞可配置
// 依賴不傳遞
compile("org.springframework:spring-web:4.3.4.RELEASE") {
transitive = false
}
configurations {
//編譯期排除commons模塊
compile.exclude module: 'commons'
//在整個(gè)構(gòu)建過(guò)程中排除pkaq.tiger:share
all*.exclude group: 'pkaq.tiger', module: 'share'
}
dependencies {
//當(dāng)前指定依賴不傳遞
compile("pkaq.tiger:web:1.0") {
exclude module: 'share'
}
}
// 強(qiáng)制使用該版本
compile('org.hibernate:hibernate:3.1') {
force = true
}
// 全局配置昆雀,使用版本
configurations.all {
resolutionStrategy {
force 'org.hamcrest:hamcrest-core:1.3'
}
}
configurations.all {
//每隔24小時(shí)檢查遠(yuǎn)程依賴是否存在更新
resolutionStrategy.cacheChangingModulesFor 24, 'hours'
//每隔10分鐘..
// resolutionStrategy.cacheChangingModulesFor 10, 'minutes'
// 采用動(dòng)態(tài)版本聲明的依賴緩存10分鐘
// resolutionStrategy.cacheDynamicVersionsFor 10*60, 'seconds'
}
// 依賴不傳遞
compile("org.springframework:spring-web:4.3.4.RELEASE") {
transitive = false
}
configurations {
//編譯期排除commons模塊
compile.exclude module: 'commons'
//在整個(gè)構(gòu)建過(guò)程中排除pkaq.tiger:share
all*.exclude group: 'pkaq.tiger', module: 'share'
}
dependencies {
//當(dāng)前指定依賴不傳遞
compile("pkaq.tiger:web:1.0") {
exclude module: 'share'
}
}
// 強(qiáng)制使用該版本
compile('org.hibernate:hibernate:3.1') {
force = true
}
// 全局配置昆雀,使用版本
configurations.all {
resolutionStrategy {
force 'org.hamcrest:hamcrest-core:1.3'
}
}