Set build conditions based on task


(gjgradle) #1

Hi all,

I’m looking to replace what seems like a fairly simple Ant build for Gradle. The big complication is seemingly twofold:

  1. Our source directory and intended output directory isn’t the standard ones that Gradle expects
  2. Built classes and jars are output in separate directories depending on whether we compile in debug mode or not

I’ve managed to get things largely building in the correct places (source directory set, classes in a classes/debug or classes/release directory depending on the called task, jars in a jar directory in debug or release)

However, I have a few issues.

Classes are compiled into both release and debug directories when the debug task is called.
Compilation seems to occur twice. If I remove most of the configuration from the release task, we only get the debug directory created, but the classes still are compiled twice.
Resources are not added to the JAR. Originally the separate resources directory did not exist, but I created it to try and separate out the resources as they were being stripped out by the include("com/**/*.class") filter, even when I added additional includes.

In addition, I also feel like I’m having to configure the source and destination directories multiple times when I shouldn’t have to?

Any advice would be great, thank you.

Here’s a simplified version of what I have so far:
plugins {
id “java”
id “eclipse”
}
sourceCompatibility = “10”

ext {
    buildRootDir = System.getenv(...)
}

repositories {
    flatDir {
        dirs = [...]
    }
}

dependencies {
    compile name: ....
}

sourceSets {
    main {
        java {
            srcDirs = ["${buildRootDir}/${sourceDir}"]
        }
    }
}

tasks.withType(Jar) {
    archiveName = "${baseName}.${extension}"
    from (sourceSets.main.output) {
        include("com/**/*.class")
    }

    from (sourceSets.main.output.resourcesDir) {
        include("**/*")
    }

    from ("${buildRootDir}/${sourceDir}/com/build/CompileDate.templt") {
        rename { "${baseName}.CompileDate.txt" }
    }
}

task debugJar(type: Jar, dependsOn: "debug") {
    destinationDir = file("${buildRootDir}/${debugTargetDir}/${jarDir}")
}

task releaseJar(type: Jar, dependsOn: "release") {
    destinationDir = file("${buildRootDir}/${releaseTargetDir}/${jarDir}")
}

task debug(type: JavaCompile) {
    doFirst {
        options.debug = true
    }

    source = sourceSets.main.java.srcDirs
    classpath = sourceSets.main.runtimeClasspath

    destinationDir = file("${buildRootDir}/${debugTargetDir}")
    sourceSets.main.output.classesDir = "${buildRootDir}/${debugTargetDir}"
    sourceSets.main.output.resourcesDir = file("${buildRootDir}/${debugTargetDir}/resources")
}

task release(type: JavaCompile) {
    doFirst {
        options.debug = false
    }

    source = sourceSets.main.java.srcDirs
    classpath = sourceSets.main.runtimeClasspath

    destinationDir = file("${buildRootDir}/${releaseTargetDir}")
    sourceSets.main.output.classesDir = "${buildRootDir}/${releaseTargetDir}"
    sourceSets.main.output.resourcesDir = file("${buildRootDir}/${releaseTargetDir}/resources")
}

tasks.withType(JavaCompile) {
    processResources {
        from ("${buildRootDir}/${sourceDir}") {
            include "com/**/examples/*.java"
            include "com/.../ConfigFile.xsd"
            include "com/.../ConfigFile.xsd"
            include "com/.../jaxb.index"
        }

        fileMode = 0644
    }
}

tasks.withType(Copy) {
    includeEmptyDirs = false
}

(Chris Doré) #2

For the double compile issue, I believe this is what’s happening; By applying the java plugin, you already get a jar and compile task associated with the main sourceset. The compile task was registered by the java plugin as a task required to create the output files. When you use from( sourceSets.main.output ) you are creating a dependency between the tasks required to generate the output and your jar tasks. This causes the default supplied compile task to run.