Build multiple jars from a single project source


(sjlee0) #1

I’d like to find out if it is possible to build multiple jars from the same project source. I’ve browsed previous similar questions, but my situation is a little different. I’d like to compile all the source files twice to produce two different jars, each with different dependencies. Conceptually it may be represented as follows:

project FooBase – src/main/java/… – depends on library Bar version 1.0.0 – compiles all the source and produces “org.foo/Foo1” (group/artifact)

project Foo2 – (inherits" or “extends” FooBase source, but might add its own) – depends on library Bar version 2.0.0 – compiles all the source in FooBase + Foo2 and produces “org.foo/Foo2” (group/artifact)

Is this sort of build possible? Thanks!


(René Groeschke) #2

We want to support this kind of requirement out of the box in the future. But for now you have to configure this completely on your own.

One option is to define two sourcesets (e.g. foobase and foo2) and let both sourcesets grab sources from src/main/java. Additional to that the foo2 sourceset would have foo2 specific sourceDirectory in src/foo2/java.

With these two sourcesets you can define different dependencies on bar in your dependency section:

dependencies{
compileFoobase "org.foo:bar:1.0.0"
compileFoo2 "org.foo:bar:2.0.0"
}

writing the according jar tasks is simple. I think to publish those different artifacts you might need to declare a pom filter in the uploadArchives task

hope that helped.