Java 为同一项目设置多个构建脚本
对于我的项目,我有不同的设置,需要将某个第三方库添加到类路径。根据关键字,我需要将a.jar或B.jar添加到项目的类路径中 我已经知道我可以使用Java 为同一项目设置多个构建脚本,java,gradle,build.gradle,Java,Gradle,Build.gradle,对于我的项目,我有不同的设置,需要将某个第三方库添加到类路径。根据关键字,我需要将a.jar或B.jar添加到项目的类路径中 我已经知道我可以使用-b标志来指定不同的构建脚本。然而,我不知道如何告诉gradle这个项目的基本路径是向上一级。因此,假设此配置: /src/main/java /build_scripts/buildA.gradle /buildB.gradle 现在,如果我执行gradle-b build\u scripts/buildA.gradle
-b
标志来指定不同的构建脚本。然而,我不知道如何告诉gradle这个项目的基本路径是向上一级。因此,假设此配置:
/src/main/java
/build_scripts/buildA.gradle
/buildB.gradle
现在,如果我执行gradle-b build\u scripts/buildA.gradle
,它会做正确的事情,但是所有生成的文件都在build\u scripts
中路由
听起来是使用配置的好理由!让我们想象一下,根据我们正在构建的内容,我们想要使用三种不同版本的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任务依赖项
$ ./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
这两个gradle文件之间是否只有一个jar不同?还是有很多不同之处?如果更改相当有限,我强烈建议使用If/then结构,而不是两个不同的构建文件。每次你对一个文件的公共部分进行更改时,你必须记住要更改另外两个文件,我向你保证,在将来的某个时候,当你忘记的时候,这会再次困扰你。为什么不把两个构建脚本都放在根目录中呢。您不必更改根目录,只需更改生成目录,即可更改生成文件的位置
project.buildDir='..\build'
@RaGe可能存在多个可能不同的依赖项。到目前为止,我使用的是if-else语句,但是这个语句不断增长,开始变得烦人。我想知道我是否也可以有一个依赖项的纯文本文件,我只需在依赖项部分中读取并添加这些文件。我将很快尝试buildDir属性,看看它是如何工作的。我同意,但是必须为每个build.gradle重复公共部分会很烦人,并且有丢失/忘记细节的风险。这看起来是一个优雅的解决方案。明天早上我将尝试一下:)我必须使用--按需配置,然后这真的很好-我喜欢这种配置。有没有一种方法也可以为eclipse插件启用该功能?目前(我认为)eclipse最终拥有所有依赖项,而jar做了正确的事情。您可以在gradle.properties
中启用按需配置。我已经尝试过这个解决方案-为什么构建一和构建二要比没有配置的情况下大得多?(在我的例子中,大小从197kB增加到9MB)@rudald,也许最好打开一个新问题来完全理解您的问题。