Opinion on defining source sets for different compilation target in same module

This for discussion only. As I don’t have any issues to solve.

I work on a few projects, that require to have classes compiled to target specific version of Java.

Also, this is not about multi-release jar.

At this time I’ve seen these two approaches :

  1. the “language folder” is located under the “main source set” which seems fair given, the compiled classes will land in the same artifact.
val javaXMain by sourceSets.creating {
    java { 
        srcDirs("src/main/java8")
    }
}
  1. an additional “main source set”, with a java language folder
val javaXMain by sourceSets.creating {
    java {
        srcDir("src/javaXMain/java")
    }
}

(Replace X by a java version, that is different form the main java version)

In both cases the source set methods are available and needed to wire things.

I don;t see any real drawback either way, but I’d like to discuss this as I may have missed some angle there.

I’d say the second is the cleaner one.
src/main/ contains the source directory sets (java, groovy, kotlin, resources, …) of the main source set.
If you have a source set called javaXMain the files should imho go to src/javaXMain/....

If you want src/main/javaX, maybe you should not create a new source set.
But don’t ask me how to add a source directory set properly, never did, but the other language plugins can probably give a hint. :smiley:

Thanks for the feedback!

If you want src/main/javaX, maybe you should not create a new source set.

I tend to agree, but it appears to require a bit more “ceremony” to have everything wired.

Yes, that’s right of course :smiley:

1 Like