Jonathan Jonathan - 4 months ago 26
Java Question

Gradle - A lot of modules

I currently have a Java project which (simplified) looks like this:
(They are not named like this, it is just an example)

core (project 1)
core.java
utils:
util.java
anotherutil.java

modules (project 2)
a
amodule.java
b
bmodule.java
c
cmodule.java
...
z
zmodule.java


I made a build.gradle which builds the core project, which works fine.

My question: Is there a way to make 26 different jars for all 26 different "modules", without creating 26 different build scripts?

When building my program I don't want to run 26 different build scripts every time. I am fine with running 2 scrips, one for the core and on for the modules. The modules all depend on the core and the core softdepends (depends without NEEDING it, but has certain extra functions when it is there) on the modules.

All modules should have their own jar which looks like this:

a.jar:
a (package)
amodule.java
b.jar:
b (package)
bmodule.java
etc...

Answer

Gradle itself supports customising multi-project builds, which is the ideal way.

All the module that are represented by separate Jar should be separated as projects.

But just in case you still want to have scenario in which you want to extract different components/modules with contents/source file from some projects , it is possible!

With minor tweak, you can create a customised task that create a Jar archive meeting all you conditions.

PS: Creating logical modules of project is not at all recommended. Use sub-projects instead and configure it using Gradle Multi-Project builds


Not Recommended :: GENERATING MULTIPLE JARS FROM A SOURCE FOLDER (PROJECT)

One of the easiest way to achieve this is create a custom task

For module A

task aJar(type: Jar) {  
    from(sourceSets.main.output) {  
        include "a/**" 
    }  
}

For module B

task bJar(type: Jar) {  
    from(sourceSets.main.output) {  
        include "b/**" 
    }  
}

and so on.

Now specify dependency over tasks

jar.dependsOn(aJar,bJar)

They finally you can execute gradle jar to get all required jars