Gradle的依赖管理模型

Gradle的依赖管理模型由四部分组成:

其中:

我们用代码来说明:

configurations {
	defaultConfig {
		description = "Default Config"
	}
}

如上面的代码所示,我们定义了一个configuration叫做defaultConfig,然后我们可以在dependencies中指定config所包含的dependencies:

dependencies {
	defaultConfig group: 'org.jboss.resteasy', name: 'resteasy-jaxrs', version: '3.1.0.Beta1'
}

上面的代码配置了一条defaultConfig中包含的dependency:

group: 'org.jboss.resteasy', name: 'resteasy-jaxrs', version: '3.1.0-SNAPSHOT'

接下来我们要配置repositories,这样Gradle才能去查找和下载依赖的包。Gradle支持很多种repositories,比如:Maven仓库,Ivy仓库,文件系统,等等。

在本文里,我们使用Maven仓库:

repositories {
	mavenCentral()
}

在上面的配置中,我们配置使用gradle内置支持的mavenCentral()仓库,对应https://repo1.maven.org/maven2/

这样,Gradle会使用这个仓库下载dependencies中依赖包。接下来我们在task中使用defaultConfig,就可以使用它里面指定的这个dependency了:

task listJars << {
	configurations.defaultConfig.each { File file ->
		println file.path
	}
}

如上所示,我们定义了一个listJars任务,在这个任务当中,我们使用defaultConfig,查找它里面包含的依赖包。运行这个task,结果如下:

$ gradle listJars
:listJars
Download https://repo1.maven.org/maven2/org/jboss/resteasy/resteasy-jaxrs/3.1.0.Beta1/resteasy-jaxrs-3.1.0.Beta1.pom
Download https://repo1.maven.org/maven2/org/jboss/resteasy/resteasy-jaxrs-all/3.1.0.Beta1/resteasy-jaxrs-all-3.1.0.Beta1.pom
Download https://repo1.maven.org/maven2/org/jboss/resteasy/resteasy-jaxrs/3.1.0.Beta1/resteasy-jaxrs-3.1.0.Beta1.jar
/Users/weli/.gradle/caches/modules-2/files-2.1/org.jboss.resteasy/resteasy-jaxrs/3.1.0.Beta1/2c837d5360952d5438f518bdb70d0be2fdf2ef28/resteasy-jaxrs-3.1.0.Beta1.jar
/Users/weli/.gradle/caches/modules-2/files-2.1/org.jboss.spec.javax.ws.rs/jboss-jaxrs-api_2.0_spec/1.0.0.Final/dbf29e00dee135ef537b94167aa08b883f4d4cbf/jboss-jaxrs-api_2.0_spec-1.0.0.Final.jar
/Users/weli/.gradle/caches/modules-2/files-2.1/org.jboss.spec.javax.annotation/jboss-annotations-api_1.2_spec/1.0.0.Final/6d7ff02a645227876ed550900d32d618b8f0d556/jboss-annotations-api_1.2_spec-1.0.0.Final.jar
/Users/weli/.gradle/caches/modules-2/files-2.1/javax.activation/activation/1.1.1/485de3a253e23f645037828c07f1d7f1af40763a/activation-1.1.1.jar
/Users/weli/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpclient/4.5/a1e6cbb3cc2c5f210dd1310ff9fcb2c09c0d1438/httpclient-4.5.jar
/Users/weli/.gradle/caches/modules-2/files-2.1/commons-io/commons-io/2.4/b1b6ea3b7e4aa4f492509a4952029cd8e48019ad/commons-io-2.4.jar
/Users/weli/.gradle/caches/modules-2/files-2.1/net.jcip/jcip-annotations/1.0/afba4942caaeaf46aab0b976afd57cc7c181467e/jcip-annotations-1.0.jar
/Users/weli/.gradle/caches/modules-2/files-2.1/org.jboss.logging/jboss-logging/3.3.0.Final/3616bb87707910296e2c195dc016287080bba5af/jboss-logging-3.3.0.Final.jar
/Users/weli/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.4.1/f5aa318bda4c6c8d688c9d00b90681dcd82ce636/httpcore-4.4.1.jar
/Users/weli/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar
/Users/weli/.gradle/caches/modules-2/files-2.1/commons-codec/commons-codec/1.9/9ce04e34240f674bc72680f8b843b1457383161a/commons-codec-1.9.jar

BUILD SUCCESSFUL

Total time: 13.985 secs

如上所示,Gradle执行了listJars任务,从mavenCentral下载了相关的dependencies,然后最后一行打印出了依赖包被下载到本地的完整路径。之所以下载了很多包,是因为我们依赖的包本身也依赖其它包,Gradle处理了Maven仓库中的这种依赖的传递关系。我们可以使用下面的命令输出这种transitive dependencies:

$ gradle dependencies
:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

defaultConfig - Default Config
\--- org.jboss.resteasy:resteasy-jaxrs:3.1.0.Beta1
	 +--- org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.0_spec:1.0.0.Final
	 +--- org.jboss.spec.javax.annotation:jboss-annotations-api_1.2_spec:1.0.0.Final
	 +--- javax.activation:activation:1.1.1
	 +--- org.apache.httpcomponents:httpclient:4.5
	 |    +--- org.apache.httpcomponents:httpcore:4.4.1
	 |    +--- commons-logging:commons-logging:1.2
	 |    \--- commons-codec:commons-codec:1.9
	 +--- commons-io:commons-io:2.4
	 +--- net.jcip:jcip-annotations:1.0
	 \--- org.jboss.logging:jboss-logging:3.3.0.Final

BUILD SUCCESSFUL

Total time: 0.464 secs

如上所示,我们使用gradle dependencies命令输出了各个config中的dependencies。因为我们的项目只有一个defaultConfig,所以就输出了这个config当中的dependencies。

My Github Page: https://github.com/liweinan

Powered by Jekyll and Theme by solid

If you have any question want to ask or find bugs regarding with my blog posts, please report it here:
https://github.com/liweinan/liweinan.github.io/issues