pandaadb
pandaadb

Reputation: 6456

Gradle setup multiple build scripts for the same project

For my project I have different setups that require a certain 3rd party library to be added to the classpath. Based on a keyword, I need to either add A.jar or B.jar to the classpath of the project.

I have worked out that I can use -b flag being able to specify a different build script. I however don't know how to tell gradle that the base path of the project is one level up. So, say this configuration:

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

Now, if I execute gradle -b build_scripts/buildA.gradle it does the right thing, however all generated files are routed in build_scripts.

  1. So I was wondering, can I change the root directory for gradle when executing a different script file?

  2. Is this the right approach to adding extra dependencies? I am trying to avoid having to do if-else statements in my build file. I could alternatively also just import a partial build file (if possible) that only defines the dependencies?

Upvotes: 3

Views: 6226

Answers (1)

JBirdVegas
JBirdVegas

Reputation: 11403

Sounds like a good reason to use configurations! So let's imagine we want to use three different versions of a jar depending on what we are building. For this example we will use three versions of 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"
}

Next we can verify our configurations via the gradle task 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

And we can verify an artifact has been produced for each configuration

$ 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

Upvotes: 9

Related Questions