Sourcesets vs. jar & artifacts

I have a project with different source sets. One of them is a common part, and others are different functions based on the this common.

Something like that

  • Project
    • main => common part
    • srcSet1 => func1
    • srcSet2 => func2

To complicate matters, each of the functions depends on different external libraries.

I would like to produce multiple jar with dedicated classifiers i.e.

  • G:I:V:C1 containing sources and binaries of srcSet1 and main
  • G:I:V:C2 containing sources and binaries of srcSet2 and main

Obviously, I would like external application declaring G:I:V:C1 as dependencies retrieve by transitive depencies resolution the other jar needed for func1 and not those for func2.

How should I handle this with Gradle ?

Is it a better way to handle sources set as different sub projects gather into a multi Gradle project ?

Should I use the feature variant ?


C1 and C2 should not contain the main sources, otherwise you for example have problems if you want to use both. Both should have a dependency on main though.

Both of the ways you mentioned (separate projects vs. feature variants) are possible and have their pros and cons.

If you use different projects, you should also have different coordinates, so G:I-C1:V and G:I-C2:V.

If you use feature variants, you have one set of coordinates G:I:V and the jars have the classifiers you asked for.

With the feature variants solution a Gradle consumer can then request the capability of the feature variant (not the classifier) and then also properly get the dependencies for that feature variant.
A consumer that does not understand Gradle Module Metadata will only get the main dependencies and the feature dependencies are in the POM listed as optional.

The separate projects approach would also have the dependency information for the variants for other build tools, as then each variant is its own artifact with its own coordinates.