[关闭]
@xtccc 2016-09-06T11:07:05.000000Z 字数 7824 阅读 3162

Dependencies

给我写信
GitHub

此处输入图片的描述

Gradle



参考链接:


目录


1. 坐标


Maven中有坐标的概念,Gradle中也有类似的概念,它们之间的对应关系如下:

Maven坐标 Gradle坐标 Gradle默认值
groupId group blank
artifactId name / archivesBaseName project's directory name
version version unspecified
name N/A N/A
description description null


如果我们的build.gradle文件如下:

  1. apply plugin: 'java'
  2. group = 'cn.gridx'
  3. archivesBaseName = 'example_gradle'
  4. version = 'SNAPSHOT_0.1'

那么,执行完命令gradle clean build之后,会在build/libs目录下生成 example_gradle-SNAPSHOT_0.1.jar




2. 仓库与依赖


2.1 仓库

Gradle不会自动添加任何仓库,必须自己添加。Gradle可以直接使用Maven的仓库,例如:

  1. repositories {
  2. // 使用maven central仓库
  3. mavenCentral()
  4. // 可以使用本地的Maven repo 缓存
  5. // 通过~/.m2/setting.xml文件确定本地Maven Repo缓存的路径
  6. mavenLocal()
  7. // 使用其他 repo
  8. maven {
  9. url 'https://repository.cloudera.com/artifactory/cloudera-repos/'
  10. }
  11. }



可以用命令gradle dependencies来查看build文件中包含的所有依赖 —— 不仅包含build文件中指定的依赖,每一个依赖的间接依赖也会打印出来。例如,对于org.apache.hadoop:hadoop-yarn-client:2.6.0-cdh5.5.1

  1. \--- org.apache.hadoop:hadoop-yarn-client:2.6.0-cdh5.5.1
  2. +--- com.google.guava:guava:11.0.2
  3. | \--- com.google.code.findbugs:jsr305:1.3.9
  4. +--- commons-logging:commons-logging:1.1.3
  5. +--- commons-lang:commons-lang:2.6
  6. +--- commons-cli:commons-cli:1.2
  7. +--- log4j:log4j:1.2.17
  8. +--- org.apache.hadoop:hadoop-annotations:2.6.0-cdh5.5.1
  9. +--- org.apache.hadoop:hadoop-yarn-api:2.6.0-cdh5.5.1
  10. | +--- commons-lang:commons-lang:2.6
  11. | +--- com.google.guava:guava:11.0.2 (*)
  12. | +--- commons-logging:commons-logging:1.1.3
  13. | +--- org.apache.hadoop:hadoop-annotations:2.6.0-cdh5.5.1
  14. | \--- com.google.protobuf:protobuf-java:2.5.0
  15. \--- org.apache.hadoop:hadoop-yarn-common:2.6.0-cdh5.5.1
  16. +--- org.apache.hadoop:hadoop-yarn-api:2.6.0-cdh5.5.1 (*)
  17. +--- javax.xml.bind:jaxb-api:2.2.2
  18. | +--- javax.xml.stream:stax-api:1.0-2
  19. | \--- javax.activation:activation:1.1
  20. +--- org.apache.commons:commons-compress:1.4.1
  21. | \--- org.tukaani:xz:1.0
  22. +--- commons-lang:commons-lang:2.6
  23. +--- javax.servlet:servlet-api:2.5
  24. +--- commons-codec:commons-codec:1.4
  25. +--- org.mortbay.jetty:jetty-util:6.1.26.cloudera.4
  26. +--- com.sun.jersey:jersey-core:1.9
  27. +--- com.sun.jersey:jersey-client:1.9
  28. | \--- com.sun.jersey:jersey-core:1.9
  29. +--- org.codehaus.jackson:jackson-core-asl:1.8.8
  30. +--- org.codehaus.jackson:jackson-mapper-asl:1.8.8
  31. | \--- org.codehaus.jackson:jackson-core-asl:1.8.8
  32. +--- org.codehaus.jackson:jackson-jaxrs:1.8.8
  33. | +--- org.codehaus.jackson:jackson-core-asl:1.8.8
  34. | \--- org.codehaus.jackson:jackson-mapper-asl:1.8.8 (*)
  35. +--- org.codehaus.jackson:jackson-xc:1.8.8
  36. | +--- org.codehaus.jackson:jackson-core-asl:1.8.8
  37. | \--- org.codehaus.jackson:jackson-mapper-asl:1.8.8 (*)
  38. +--- com.google.guava:guava:11.0.2 (*)
  39. +--- commons-logging:commons-logging:1.1.3
  40. +--- commons-cli:commons-cli:1.2
  41. +--- org.slf4j:slf4j-api:1.7.5
  42. +--- org.apache.hadoop:hadoop-annotations:2.6.0-cdh5.5.1
  43. +--- com.google.inject.extensions:guice-servlet:3.0
  44. | \--- com.google.inject:guice:3.0
  45. | +--- javax.inject:javax.inject:1
  46. | +--- aopalliance:aopalliance:1.0
  47. | \--- org.sonatype.sisu.inject:cglib:2.2.1-v20090111
  48. | \--- asm:asm:3.1
  49. +--- com.google.protobuf:protobuf-java:2.5.0
  50. +--- commons-io:commons-io:2.4
  51. +--- com.google.inject:guice:3.0 (*)
  52. +--- com.sun.jersey:jersey-server:1.9
  53. | +--- asm:asm:3.1
  54. | \--- com.sun.jersey:jersey-core:1.9
  55. +--- com.sun.jersey:jersey-json:1.9
  56. | +--- org.codehaus.jettison:jettison:1.1
  57. | +--- com.sun.xml.bind:jaxb-impl:2.2.3-1
  58. | | \--- javax.xml.bind:jaxb-api:2.2.2 (*)
  59. | +--- org.codehaus.jackson:jackson-core-asl:1.8.3 -> 1.8.8
  60. | +--- org.codehaus.jackson:jackson-mapper-asl:1.8.3 -> 1.8.8 (*)
  61. | +--- org.codehaus.jackson:jackson-jaxrs:1.8.3 -> 1.8.8 (*)
  62. | +--- org.codehaus.jackson:jackson-xc:1.8.3 -> 1.8.8 (*)
  63. | \--- com.sun.jersey:jersey-core:1.9
  64. +--- com.sun.jersey.contribs:jersey-guice:1.9
  65. | +--- javax.inject:javax.inject:1
  66. | +--- com.google.inject:guice:3.0 (*)
  67. | +--- com.google.inject.extensions:guice-servlet:3.0 (*)
  68. | \--- com.sun.jersey:jersey-server:1.9 (*)
  69. \--- log4j:log4j:1.2.17


2.2 添加外部依赖

参考 How to declare your dependencies

可以声明不同类型的依赖,例如: External module dependency, Project dependency, File Dependency 等等。

  1. dependencies {
  2. // `compile` phase 依赖的JAR包
  3. compile 'commons-beanutils:commons-beanutils:1.8.3'
  4. // `archives` phase 所依赖的JAR包
  5. archives 'org.apache.hadoop:hadoop-yarn-client:2.6.0-cdh5.5.1'
  6. }

可以为每一个依赖包指定其scope,对于Java plugin而言,有6种scopes:

  • compile
  • default
  • testCompile
  • testRuntime
  • archives
  • runtime


下载的依赖包被存放在本地的什么地方呢?
在默认情况下,存储在本地的 ~/.gradle/caches/modules-2/files-2.1 目录中,可以通过下面的task显示出来看:

  1. task showMeCache << {
  2. configurations.compile.each { println it }
  3. }


2.3 上传Artifacts

如果将Artifact上传至Maven仓库,需要使用maven plugin,build.gradle文件可以如下:

  1. apply plugin: 'scala'
  2. apply plugin: 'maven'
  3. group = 'cn.gridx.win'
  4. archivesBaseName = 'example_graddle'
  5. version = 'SNAPSHOT_0.2'
  6. repositories {
  7. mavenCentral()
  8. }
  9. dependencies {
  10. compile 'org.scala-lang:scala-library:2.10.4'
  11. }
  12. uploadArchives {
  13. repositories {
  14. mavenDeployer {
  15. repository(url: "http://host:8081/nexus/content/repositories/thirdparty/") {
  16. authentication(userName: "admin", password: "admin123")
  17. }
  18. }
  19. }
  20. }

运行命令:

gradle clean build uploadArchives

如果出现 Could not find metadata cn.gridx.win:example_graddle/maven-metadata.xml in remote (http://host:8081/nexus/content/repositories/thirdparty/),也没关系,参考这里


2.4 列出所有的依赖

可以用命令 "gradle dependencies" 列出项目的所有依赖。这个命令不仅能列出所有的top-level dependencies,还可以把下面的全部sub dependencies都列出来,并显示它们之间的关系。




3. 创建Fat Jar


参考 Gradle Build Language Reference


正常构建过程完成后,生成的JAR包是不含依赖的。我们可以将所有的依赖都放入到生成的JAR包中,也可以控制只将制定的部分依赖放入生成的JAR包中。

3.1 包含全部的依赖


3.2 包含特定phase的依赖

如果想把runtime phase的依赖都打到JAR包中,则可以用下面的方法:

  1. group 'cn.gridx'
  2. version '1.0-SNAPSHOT'
  3. apply plugin: 'java'
  4. apply plugin: 'scala'
  5. repositories {
  6. mavenCentral()
  7. mavenLocal()
  8. }
  9. dependencies {
  10. testCompile group: 'junit', name: 'junit', version: '4.11'
  11. compile 'org.scala-lang:scala-library:2.10.4',
  12. 'com.amazonaws:aws-java-sdk:1.9.6'
  13. runtime 'joda-time:joda-time:2.9.1'
  14. }
  15. jar {
  16. from configurations.runtime.collect {
  17. it.isDirectory() ? it : zipTree(it)
  18. }
  19. }

注意:通过命令tar -tf [fat.jar]可以看出,runtime phase的依赖中,也默认包含了compile phase的依赖。

3.3 包含指定的某个依赖

打入*.class文件
如果我们想将指定的依赖包 'org.scala-lang:scala-library:2.10.4' 中的 .class 文件都打到生成的JAR中,则可以用下面的方法:

  1. group 'cn.gridx'
  2. version '1.0-SNAPSHOT'
  3. apply plugin: 'java'
  4. apply plugin: 'scala'
  5. repositories {
  6. mavenCentral()
  7. mavenLocal()
  8. }
  9. configurations {
  10. singleJar
  11. }
  12. dependencies {
  13. testCompile group: 'junit', name: 'junit', version: '4.11'
  14. compile 'org.scala-lang:scala-library:2.10.4',
  15. 'com.amazonaws:aws-java-sdk:1.9.6'
  16. singleJar 'org.scala-lang:scala-library:2.10.4'
  17. }
  18. jar {
  19. from configurations.singleJar.collect {
  20. it.isDirectory() ? it : zipTree(it)
  21. }
  22. }



打入整个*.jar文件
在这种方式下,"org.scala-lang:scala-library:2.10.4"对应的JAR包文件会被直接打入到我们生成的目标Jar包中。

  1. configuration {
  2. singleJar
  3. }
  4. dependencies {
  5. singleJar 'org.scala-lang:scala-library:2.10.4'
  6. }
  7. jar {
  8. from configurations.singleJar
  9. }


3.4 排除某个依赖

  1. configurations {
  2. all {
  3. exclude group: 'org.slf4j', module: 'slf4j-log4j12'
  4. }
  5. }

或者

  1. configurations {
  2. compile.exclude group:'ch.qos.logback'
  3. }




4. Dependency Configurations


Gradle通过 configurations 对依赖进行分组 —— 一个configuration是一个有名字的依赖集合。

Java plugin定义了一些标准的configurations,这些configurations代表了Java plugin使用的classpaths。下面列出一些:

Configuration Description
compile 编译production source of the project时所需的依赖
runtime The dependencies required by the production classes at runtime. 默认包含 the compile time dependencies
testCompile 编译test source of the project时所需的依赖. 默认包含 compiled production classes and the compile time dependencies
testRuntime The dependencis required to run the tests. 默认包含 compile、runtime and test compile dependencies


4.1 Custom Configurations

参考 Dependency configurations

不同的plugins可以增加其他的标准configurations,我们也可以定义自己的custom configurations(例如,添加在build和test阶段并不需要的依赖,比如将JDBC drivers放入发行包中)。

4.2 定义configuration

  1. configuration {
  2. compile
  3. custom
  4. }

4.3 访问configuration

  1. println "configurations.compile.name = " + configurations.compile.name
  2. println "configurations['compile'].name = " + configurations['compile'].name
  3. println "configurations.custom.name = " + configurations.custom.name
  4. println "configurations['custom'].name = " + configurations['custom'].name

运行输出为
QQ20160211-0@2x.png-53.5kB

4.3 配置configuration

  1. configurations {
  2. compile {
  3. description = 'compile classpath'
  4. transitive = true
  5. }
  6. custom {
  7. extendsFrom compile
  8. }
  9. }
  10. configurations.custom {
  11. description = 'custom classpath'
  12. }
  13. println "configurations.custom.description = " + configurations.custom.description

运行输出为:
QQ20160211-1@2x.png-21.6kB



添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注