对于我的项目,我有不同的设置,需要将某个第三方库添加到类路径中。基于关键字,我需要将A.jar或B.jar添加到项目的类路径中。
我已经知道我可以使用-b
标志来指定不同的构建脚本。但是我不知道如何告诉gradle项目的基本路径是一级的。所以,说这个配置:
/src/main/java
/build_scripts/buildA.gradle
/buildB.gradle
现在,如果我执行gradle -b build_scripts/buildA.gradle
它会做正确的事情,但是所有生成的文件都会在build_scripts
中路由。
所以我想知道,在执行不同的脚本文件时,我可以更改gradle的根目录吗?
这是添加额外依赖项的正确方法吗?我试图避免在我的构建文件中执行if-else语句。我也可以只导入一个只定义依赖项的部分构建文件(如果可能)?
答案 0 :(得分:7)
听起来是使用配置的一个很好的理由!因此,让我们想象我们想要根据我们正在构建的内容使用三个不同版本的jar。对于此示例,我们将使用三个版本的gson
apply plugin: "java"
version = '0.1'
configurations {
// let configurations that extend compile all the compile classpath entries
compile.transitive = true
// define a configuration
one {
description = 'compile configuration one\'s classpath'
extendsFrom compile
}
// create another configuration
two {
description = 'compile configuration two\'s classpath'
extendsFrom compile
}
}
// let gradle know you want a jar from configuration named `one`
task('oneJar', type: Jar) {
// define what configuration our jar contains
from configurations.one
// set the name so we know which jar is which configuration
baseName = "$project.name-$configurations.one.name"
}
// let gradle know you want a jar from configuration named `two`
task('twoJar', type: Jar) {
// define what configuration our jar contains
from configurations.two
// set the name so we know which jar is which configuration
baseName = "$project.name-$configurations.two.name"
}
// make gradle build our other configuration jars anytime it runs the jar task
jar.dependsOn oneJar, twoJar
// boilerplate repository to look for dependencies
repositories {
jcenter()
}
// here we can define configuration specific dependencies
dependencies {
// will only appear in the "${project.name}-${project.version}.jar" file
compile "com.google.code.gson:gson:2.4"
// will only appear in the "${project.name}-one-${project.version}.jar" file
one "com.google.code.gson:gson:2.6.1"
// will only appear in the "${project.name}-two-${project.version}.jar" file
two "com.google.code.gson:gson:2.7"
}
接下来,我们可以通过gradle任务dependencies
$ ./gradlew dependencies --configuration compile
Configuration on demand is an incubating feature.
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
compile - Dependencies for source set 'main'.
\--- com.google.code.gson:gson:2.4
BUILD SUCCESSFUL
Total time: 1.969 secs
$ ./gradlew dependencies --configuration one
Configuration on demand is an incubating feature.
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
one - compile configuration one's classpath
+--- com.google.code.gson:gson:2.4 -> 2.6.1
\--- com.google.code.gson:gson:2.6.1
(*) - dependencies omitted (listed previously)
BUILD SUCCESSFUL
Total time: 1.828 secs
$ ./gradlew dependencies --configuration two
Configuration on demand is an incubating feature.
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
two - compile configuration two's classpath
+--- com.google.code.gson:gson:2.4 -> 2.7
\--- com.google.code.gson:gson:2.7
(*) - dependencies omitted (listed previously)
BUILD SUCCESSFUL
Total time: 0.764 secs
我们可以验证是否已为每个配置生成工件
$ ls -l build/libs/
total 824
-rw-r--r-- 1 some.user 63209268 207741 Nov 15 14:05 gradleConfigurations-one-0.1.jar
-rw-r--r-- 1 some.user 63209268 208665 Nov 15 14:05 gradleConfigurations-two-0.1.jar
-rw-r--r-- 1 some.user 63209268 930 Nov 15 14:05 gradleConfigurations-0.1.jar