Gradle compile and jar a subset of java classes

gradle

Solution

I recommend to use a separate source set:

apply plugin: "java"

sourceSets {
    main {
        java {
            exclude "com/interface/A.java"
            exclude "com/interface/B.java"
        }
    }
    api {
        java {
            srcDir "src/main/java"
            include "com/interface/A.java"
            include "com/interface/B.java"
        }
    }
}

If `main` depends on `api`, add something like:

sourceSets.main.compileClasspath += sourceSets.api.output

Source/target compatibility can be adjusted on the compile task level:

compileApiJava {
    sourceCompatibility = "1.7"
    targetCompatibility = "1.7"
}

To use a compiler other than that belonging to the JDK that Gradle itself is run with, you'll have to set a compiler executable:

compileApiJava {
    options.fork = true
    options.forkOptions.executable = "/path/to/compiler/executable"
}

This may slow down the build to some extent.

For further API details, check out the Gradle Build Language Reference.

Problem

I want to compile (with a different targetCompatibility and sourceCompatibility) and jar a small subset of my whole main/src/java folder. So I have e.g. ``` main/src/java/com/interface/A.java main/src/java/com/interface/B.java main/src/java/com/logic/C.java ``` I want to compile A and B to a jar "projectname_projectversion_interface.jar" and using a different compiler version. Is this possible? How can I do this? Thanks

Original source