在本教程中,我们将向您展示如何使用Gradle构建工具显示项目依赖性。 查看一个简单的Spring MVC项目依赖项。
build.gradle
dependencies {
compile 'org.slf4j:jcl-over-slf4j:1.7.12'
compile 'ch.qos.logback:logback-classic:1.1.3'
compile('org.springframework:spring-webmvc:4.1.6.RELEASE'){
exclude group: 'commons-logging', module: 'commons-logging'
}
compile 'org.hsqldb:hsqldb:2.3.2'
providedCompile 'javax.servlet:servlet-api:2.5'
}
PS已通过Gradle 2.4测试
1. gradle依赖
以树格式显示项目依赖项(对所有配置都是直接的和传递的)。 该依赖关系报告非常大,看不到太大的价值。
Terminal
$ gradle dependencies
archives - Configuration for archive artifacts.
No dependencies
compile - Compile classpath for source set 'main'.
+--- org.slf4j:jcl-over-slf4j:1.7.12
| \--- org.slf4j:slf4j-api:1.7.12
+--- ch.qos.logback:logback-classic:1.1.3
| +--- ch.qos.logback:logback-core:1.1.3
| \--- org.slf4j:slf4j-api:1.7.7 -> 1.7.12
+--- org.springframework:spring-webmvc:4.1.6.RELEASE
| +--- org.springframework:spring-beans:4.1.6.RELEASE
| | \--- org.springframework:spring-core:4.1.6.RELEASE
| +--- org.springframework:spring-context:4.1.6.RELEASE
| | +--- org.springframework:spring-aop:4.1.6.RELEASE
| | | +--- aopalliance:aopalliance:1.0
| | | +--- org.springframework:spring-beans:4.1.6.RELEASE (*)
| | | \--- org.springframework:spring-core:4.1.6.RELEASE
| | +--- org.springframework:spring-beans:4.1.6.RELEASE (*)
| | +--- org.springframework:spring-core:4.1.6.RELEASE
| | \--- org.springframework:spring-expression:4.1.6.RELEASE
| | \--- org.springframework:spring-core:4.1.6.RELEASE
| +--- org.springframework:spring-core:4.1.6.RELEASE
| +--- org.springframework:spring-expression:4.1.6.RELEASE (*)
| \--- org.springframework:spring-web:4.1.6.RELEASE
| +--- org.springframework:spring-aop:4.1.6.RELEASE (*)
| +--- org.springframework:spring-beans:4.1.6.RELEASE (*)
| +--- org.springframework:spring-context:4.1.6.RELEASE (*)
| \--- org.springframework:spring-core:4.1.6.RELEASE
+--- org.hsqldb:hsqldb:2.3.2
\--- javax.servlet:servlet-api:2.5
default - Configuration for default artifacts.
+--- org.slf4j:jcl-over-slf4j:1.7.12
| \--- org.slf4j:slf4j-api:1.7.12
+--- ch.qos.logback:logback-classic:1.1.3
| +--- ch.qos.logback:logback-core:1.1.3
| \--- org.slf4j:slf4j-api:1.7.7 -> 1.7.12
...
providedCompile - Additional compile classpath for libraries that should not be part of the WAR arc
\--- javax.servlet:servlet-api:2.5
providedRuntime - Additional runtime classpath for libraries that should not be part of the WAR arc
\--- javax.servlet:servlet-api:2.5
runtime - Runtime classpath for source set 'main'.
+--- org.slf4j:jcl-over-slf4j:1.7.12
| \--- org.slf4j:slf4j-api:1.7.12
+--- ch.qos.logback:logback-classic:1.1.3
| +--- ch.qos.logback:logback-core:1.1.3
| \--- org.slf4j:slf4j-api:1.7.7 -> 1.7.12
...
testCompile - Compile classpath for source set 'test'.
+--- org.slf4j:jcl-over-slf4j:1.7.12
| \--- org.slf4j:slf4j-api:1.7.12
+--- ch.qos.logback:logback-classic:1.1.3
| +--- ch.qos.logback:logback-core:1.1.3
| \--- org.slf4j:slf4j-api:1.7.7 -> 1.7.12
...
... this report can be very large
2. gradle依赖关系–配置
通常,我们只需要查看特定配置的依赖项报告即可。 在下面的示例中,它仅显示“运行时”配置的依赖项报告。
Terminal
$ gradle dependencies --configuration runtime
runtime - Runtime classpath for source set 'main'.
+--- org.slf4j:jcl-over-slf4j:1.7.12
| \--- org.slf4j:slf4j-api:1.7.12
+--- ch.qos.logback:logback-classic:1.1.3
| +--- ch.qos.logback:logback-core:1.1.3
| \--- org.slf4j:slf4j-api:1.7.7 -> 1.7.12
+--- org.springframework:spring-webmvc:4.1.6.RELEASE
| +--- org.springframework:spring-beans:4.1.6.RELEASE
| | \--- org.springframework:spring-core:4.1.6.RELEASE
| +--- org.springframework:spring-context:4.1.6.RELEASE
| | +--- org.springframework:spring-aop:4.1.6.RELEASE
| | | +--- aopalliance:aopalliance:1.0
| | | +--- org.springframework:spring-beans:4.1.6.RELEASE (*)
| | | \--- org.springframework:spring-core:4.1.6.RELEASE
| | +--- org.springframework:spring-beans:4.1.6.RELEASE (*)
| | +--- org.springframework:spring-core:4.1.6.RELEASE
| | \--- org.springframework:spring-expression:4.1.6.RELEASE
| | \--- org.springframework:spring-core:4.1.6.RELEASE
| +--- org.springframework:spring-core:4.1.6.RELEASE
| +--- org.springframework:spring-expression:4.1.6.RELEASE (*)
| \--- org.springframework:spring-web:4.1.6.RELEASE
| +--- org.springframework:spring-aop:4.1.6.RELEASE (*)
| +--- org.springframework:spring-beans:4.1.6.RELEASE (*)
| +--- org.springframework:spring-context:4.1.6.RELEASE (*)
| \--- org.springframework:spring-core:4.1.6.RELEASE
+--- org.hsqldb:hsqldb:2.3.2
\--- javax.servlet:servlet-api:2.5
(*) - dependencies omitted (listed previously)
3. gradle依赖关系
这种“洞察力”使您可以找出特定依赖关系。
3.1在下面的示例中, jcl-over-slf4j
和logback-classic
依赖于slf4j-api
,但是存在版本冲突,Gradle选择了最新版本slf4j-api:1.7.12
。
Terminal
$ gradle dependencyInsight --dependency slf4j-api --configuration compile
org.slf4j:slf4j-api:1.7.12 (conflict resolution)
\--- org.slf4j:jcl-over-slf4j:1.7.12
\--- compile
org.slf4j:slf4j-api:1.7.7 -> 1.7.12
\--- ch.qos.logback:logback-classic:1.1.3
\--- compile
3.2再举一个例子,声明Jackson 2.8.7
,但是Gradle选择了最新的Jackson 2.9.0.pr1
build.gradle
dependencies {
compile "com.fasterxml.jackson.core:jackson-databind:2.8.7"
compile ('com.maxmind.geoip2:geoip2:2.8.0') //depends on 2.9.0.pr1
}
Terminal
$ gradle :dependencyInsight --configuration compile --dependency jackson-databind
:core:dependencyInsight
com.fasterxml.jackson.core:jackson-databind:2.9.0.pr1 (conflict resolution)
com.fasterxml.jackson.core:jackson-databind:2.8.2 -> 2.9.0.pr1
\--- com.maxmind.geoip2:geoip2:2.8.0
\--- compile
com.fasterxml.jackson.core:jackson-databind:2.8.7 -> 2.9.0.pr1
\--- compile
com.fasterxml.jackson.core:jackson-databind:[2.7.0,) -> 2.9.0.pr1
\--- com.maxmind.db:maxmind-db:1.2.1
\--- com.maxmind.geoip2:geoip2:2.8.0
\--- compile
4. gradle依赖多项目
查看一个多项目示例。
settings.gradle
include 'core', 'crawler', 'whois', 'web','analyzer','security'
要显示子项目的依赖性报告:
$ gradle {subproject}:dependencies
例如,显示子项目core
依赖性报告:
$ gradle core:dependencies
$ gradle core:dependencies --configuration compile
$ gradle core:dependencyInsight --dependency slf4j-api --configuration compile
参考文献
翻译自: https://mkyong.com/gradle/gradle-display-project-dependency/