Sample for creating multiple jars


(cguillemot) #1

Hi, I created a java project with eclipse. I export my work by creating 2 jars for 2 distinct purpose. Build these jars are very simple, using Eclipse it is only selections of package or files and set a manifest files. I’m trying to use gradle. I succeed to compile sources but I dont’ know how to do to create these specifics jars. I read many docs, but none clear (for me) I would like some help Chris


(Peter Niederwieser) #2

You could accomplish this with two projects that both apply the ‘java’ plugin, or a single project that applies the ‘java’ plugin and adds another ‘Jar’ task. I recommend to check out the many sample builds in the full Gradle distribution.


(cguillemot) #3

Please a simple sample… How to set name jar and include 2 package for example and a file

I read and i read… I can’t more …


(Peter Niederwieser) #4

It really depends on what you have/need. Here is one example:

task jar1(type: Jar) {
    baseName = "jar1"
    // add all classes and resources produced from main source set
     // (e.g. src/main/java, src/main/resources)
    from(sourceSets.main.output) {
        // filter to only include certain class files (Ant glob pattern)
        include "some/package/**"
     }
    // add a single file; path is relative to project dir
    from "some/file.txt"
}

(cguillemot) #5

Thanks a lot. I’ll come back to write my solution


(cguillemot) #6

Hi,

It works when i call “gradle jar1” but is it possible that all jars are built by calling ‘gradle jar’ ?


(Peter Niederwieser) #7

‘jar’ is a single Jar task like ‘jar1’ (at least when you apply the ‘java’ plugin). Once you’ve declared ‘jar1’ as an artifact of the project (‘artifacts { archives jar1 }’), ‘gradle assemble’ will build it as well. Alternatively, you could declare an ‘allJars’ task that depends on all other Jar tasks.


(cguillemot) #8

Thanks.

So I wrote

artifacts {

archives jar1, jar2 }

And when I run ‘gradle assemble’, all jars are created.

Thanks a lot