@xtccc
2016-09-06T11:07:05.000000Z
字数 7824
阅读 3162
Gradle
参考链接:
目录
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
文件如下:
apply plugin: 'java'
group = 'cn.gridx'
archivesBaseName = 'example_gradle'
version = 'SNAPSHOT_0.1'
那么,执行完命令gradle clean build
之后,会在build/libs
目录下生成 example_gradle-SNAPSHOT_0.1.jar
Gradle不会自动添加任何仓库,必须自己添加。Gradle可以直接使用Maven的仓库,例如:
repositories {
// 使用maven central仓库
mavenCentral()
// 可以使用本地的Maven repo 缓存
// 通过~/.m2/setting.xml文件确定本地Maven Repo缓存的路径
mavenLocal()
// 使用其他 repo
maven {
url 'https://repository.cloudera.com/artifactory/cloudera-repos/'
}
}
可以用命令gradle dependencies
来查看build文件中包含的所有依赖 —— 不仅包含build文件中指定的依赖,每一个依赖的间接依赖也会打印出来。例如,对于org.apache.hadoop:hadoop-yarn-client:2.6.0-cdh5.5.1
:
\--- org.apache.hadoop:hadoop-yarn-client:2.6.0-cdh5.5.1
+--- com.google.guava:guava:11.0.2
| \--- com.google.code.findbugs:jsr305:1.3.9
+--- commons-logging:commons-logging:1.1.3
+--- commons-lang:commons-lang:2.6
+--- commons-cli:commons-cli:1.2
+--- log4j:log4j:1.2.17
+--- org.apache.hadoop:hadoop-annotations:2.6.0-cdh5.5.1
+--- org.apache.hadoop:hadoop-yarn-api:2.6.0-cdh5.5.1
| +--- commons-lang:commons-lang:2.6
| +--- com.google.guava:guava:11.0.2 (*)
| +--- commons-logging:commons-logging:1.1.3
| +--- org.apache.hadoop:hadoop-annotations:2.6.0-cdh5.5.1
| \--- com.google.protobuf:protobuf-java:2.5.0
\--- org.apache.hadoop:hadoop-yarn-common:2.6.0-cdh5.5.1
+--- org.apache.hadoop:hadoop-yarn-api:2.6.0-cdh5.5.1 (*)
+--- javax.xml.bind:jaxb-api:2.2.2
| +--- javax.xml.stream:stax-api:1.0-2
| \--- javax.activation:activation:1.1
+--- org.apache.commons:commons-compress:1.4.1
| \--- org.tukaani:xz:1.0
+--- commons-lang:commons-lang:2.6
+--- javax.servlet:servlet-api:2.5
+--- commons-codec:commons-codec:1.4
+--- org.mortbay.jetty:jetty-util:6.1.26.cloudera.4
+--- com.sun.jersey:jersey-core:1.9
+--- com.sun.jersey:jersey-client:1.9
| \--- com.sun.jersey:jersey-core:1.9
+--- org.codehaus.jackson:jackson-core-asl:1.8.8
+--- org.codehaus.jackson:jackson-mapper-asl:1.8.8
| \--- org.codehaus.jackson:jackson-core-asl:1.8.8
+--- org.codehaus.jackson:jackson-jaxrs:1.8.8
| +--- org.codehaus.jackson:jackson-core-asl:1.8.8
| \--- org.codehaus.jackson:jackson-mapper-asl:1.8.8 (*)
+--- org.codehaus.jackson:jackson-xc:1.8.8
| +--- org.codehaus.jackson:jackson-core-asl:1.8.8
| \--- org.codehaus.jackson:jackson-mapper-asl:1.8.8 (*)
+--- com.google.guava:guava:11.0.2 (*)
+--- commons-logging:commons-logging:1.1.3
+--- commons-cli:commons-cli:1.2
+--- org.slf4j:slf4j-api:1.7.5
+--- org.apache.hadoop:hadoop-annotations:2.6.0-cdh5.5.1
+--- com.google.inject.extensions:guice-servlet:3.0
| \--- com.google.inject:guice:3.0
| +--- javax.inject:javax.inject:1
| +--- aopalliance:aopalliance:1.0
| \--- org.sonatype.sisu.inject:cglib:2.2.1-v20090111
| \--- asm:asm:3.1
+--- com.google.protobuf:protobuf-java:2.5.0
+--- commons-io:commons-io:2.4
+--- com.google.inject:guice:3.0 (*)
+--- com.sun.jersey:jersey-server:1.9
| +--- asm:asm:3.1
| \--- com.sun.jersey:jersey-core:1.9
+--- com.sun.jersey:jersey-json:1.9
| +--- org.codehaus.jettison:jettison:1.1
| +--- com.sun.xml.bind:jaxb-impl:2.2.3-1
| | \--- javax.xml.bind:jaxb-api:2.2.2 (*)
| +--- org.codehaus.jackson:jackson-core-asl:1.8.3 -> 1.8.8
| +--- org.codehaus.jackson:jackson-mapper-asl:1.8.3 -> 1.8.8 (*)
| +--- org.codehaus.jackson:jackson-jaxrs:1.8.3 -> 1.8.8 (*)
| +--- org.codehaus.jackson:jackson-xc:1.8.3 -> 1.8.8 (*)
| \--- com.sun.jersey:jersey-core:1.9
+--- com.sun.jersey.contribs:jersey-guice:1.9
| +--- javax.inject:javax.inject:1
| +--- com.google.inject:guice:3.0 (*)
| +--- com.google.inject.extensions:guice-servlet:3.0 (*)
| \--- com.sun.jersey:jersey-server:1.9 (*)
\--- log4j:log4j:1.2.17
参考 How to declare your dependencies
可以声明不同类型的依赖,例如: External module dependency, Project dependency, File Dependency 等等。
dependencies {
// `compile` phase 依赖的JAR包
compile 'commons-beanutils:commons-beanutils:1.8.3'
// `archives` phase 所依赖的JAR包
archives 'org.apache.hadoop:hadoop-yarn-client:2.6.0-cdh5.5.1'
}
可以为每一个依赖包指定其scope,对于Java plugin而言,有6种scopes:
- compile
- default
- testCompile
- testRuntime
- archives
- runtime
下载的依赖包被存放在本地的什么地方呢?
在默认情况下,存储在本地的 ~/.gradle/caches/modules-2/files-2.1
目录中,可以通过下面的task显示出来看:
task showMeCache << {
configurations.compile.each { println it }
}
如果将Artifact上传至Maven仓库,需要使用maven plugin
,build.gradle文件可以如下:
apply plugin: 'scala'
apply plugin: 'maven'
group = 'cn.gridx.win'
archivesBaseName = 'example_graddle'
version = 'SNAPSHOT_0.2'
repositories {
mavenCentral()
}
dependencies {
compile 'org.scala-lang:scala-library:2.10.4'
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "http://host:8081/nexus/content/repositories/thirdparty/") {
authentication(userName: "admin", password: "admin123")
}
}
}
}
运行命令:
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/),也没关系,参考这里
可以用命令 "gradle dependencies" 列出项目的所有依赖。这个命令不仅能列出所有的top-level dependencies,还可以把下面的全部sub dependencies都列出来,并显示它们之间的关系。
参考 Gradle Build Language Reference
正常构建过程完成后,生成的JAR包是不含依赖的。我们可以将所有的依赖都放入到生成的JAR包中,也可以控制只将制定的部分依赖放入生成的JAR包中。
如果想把runtime phase
的依赖都打到JAR包中,则可以用下面的方法:
group 'cn.gridx'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'scala'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'org.scala-lang:scala-library:2.10.4',
'com.amazonaws:aws-java-sdk:1.9.6'
runtime 'joda-time:joda-time:2.9.1'
}
jar {
from configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
}
注意:通过命令tar -tf [fat.jar]
可以看出,runtime phase
的依赖中,也默认包含了compile phase
的依赖。
打入*.class文件
如果我们想将指定的依赖包 'org.scala-lang:scala-library:2.10.4'
中的 .class 文件都打到生成的JAR中,则可以用下面的方法:
group 'cn.gridx'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'scala'
repositories {
mavenCentral()
mavenLocal()
}
configurations {
singleJar
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'org.scala-lang:scala-library:2.10.4',
'com.amazonaws:aws-java-sdk:1.9.6'
singleJar 'org.scala-lang:scala-library:2.10.4'
}
jar {
from configurations.singleJar.collect {
it.isDirectory() ? it : zipTree(it)
}
}
打入整个*.jar文件
在这种方式下,"org.scala-lang:scala-library:2.10.4"对应的JAR包文件会被直接打入到我们生成的目标Jar包中。
configuration {
singleJar
}
dependencies {
singleJar 'org.scala-lang:scala-library:2.10.4'
}
jar {
from configurations.singleJar
}
configurations {
all {
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
}
}
或者
configurations {
compile.exclude group:'ch.qos.logback'
}
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 |
不同的plugins可以增加其他的标准configurations,我们也可以定义自己的custom configurations(例如,添加在build和test阶段并不需要的依赖,比如将JDBC drivers放入发行包中)。
configuration {
compile
custom
}
println "configurations.compile.name = " + configurations.compile.name
println "configurations['compile'].name = " + configurations['compile'].name
println "configurations.custom.name = " + configurations.custom.name
println "configurations['custom'].name = " + configurations['custom'].name
运行输出为
configurations {
compile {
description = 'compile classpath'
transitive = true
}
custom {
extendsFrom compile
}
}
configurations.custom {
description = 'custom classpath'
}
println "configurations.custom.description = " + configurations.custom.description
运行输出为: