java - Gradle 为同一个项目设置多个构建脚本

标签 java gradle build.gradle

对于我的项目,我有不同的设置,需要将某个第 3 方库添加到类路径中。基于关键字,我需要将 A.jar 或 B.jar 添加到项目的类路径中。

我发现我可以使用 -b 标志来指定不同的构建脚本。但是我不知道如何告诉 gradle 项目的基本路径是上一级。所以,说这个配置:

/src/main/java
/build_scripts/buildA.gradle
              /buildB.gradle

现在,如果我执行 gradle -b build_scripts/buildA.gradle 它会做正确的事情,但是所有生成的文件都在 build_scripts 中路由。

  1. 所以我想知道,我可以在执行不同的脚本文件时更改gradle的根目录吗?

  2. 这是添加额外依赖项的正确方法吗?我试图避免在我的构建文件中执行 if-else 语句。或者我也可以只导入一个只定义依赖项的部分构建文件(如果可能)?

最佳答案

听起来是使用配置的好理由!所以让我们想象一下,我们想要根据我们正在构建的内容使用三个不同版本的 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

关于java - Gradle 为同一个项目设置多个构建脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40593005/

相关文章:

java - 将变量传递给 Jersey 中的资源类

java - 64 位系统上 Java 的串行通信 API(调制解调器交互)

android - 无法解决依赖错误

java - 在 IntelliJ 中设置 Gradle 任务 `bootrun`

eclipse - 如何将 Java Gradle 项目转换为 Dynamic Web 项目?

java - build.gradle中的依赖是自动生成的吗?

java - 如何使这个 "cleaner"

java - 为什么 UmbrellaException 有那个名字?

android - 应用程序在构建时看不到模块类

java - 错误 : Manifest merger failed with multiple errors, 查看日志