Gradle是一个构建工具,构建包括依赖管理、编译、打包等过程。总结了Gradle和maven之间的主要区别:灵活性、用户体验和依赖关系管理。
依赖管理
1.申明简洁
<!-- 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以一个常见的JAVA日志库冲突问题为例
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 {
// 支持通过group、module排除,可以同时使用
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
}
// 引入依赖 所有类型的日志库都桥接到 slf4j 实现库为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 依赖拉取缓存策略
configurations.all {
//每隔24小时检查远程依赖是否存在更新
resolutionStrategy.cacheChangingModulesFor 24, 'hours'
//每隔10分钟..
// resolutionStrategy.cacheChangingModulesFor 10, 'minutes'
// 采用动态版本声明的依赖缓存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'
//在整个构建过程中排除pkaq.tiger:share
all*.exclude group: 'pkaq.tiger', module: 'share'
}
dependencies {
//当前指定依赖不传递
compile("pkaq.tiger:web:1.0") {
exclude module: 'share'
}
}
// 强制使用该版本
compile('org.hibernate:hibernate:3.1') {
force = true
}
// 全局配置,使用版本
configurations.all {
resolutionStrategy {
force 'org.hamcrest:hamcrest-core:1.3'
}
}
网友评论