Mohan
Mohan

Reputation: 8863

Gradle: multiple JARs without multiple projects

I have a library which is used to build a number of CLI tools using Gradle. Each CLI tool is a separate JAR. At the moment every tool requires a separate Gradle project, with an associated set of directories, like this:

enter image description here

Having all of this structure is resulting in the whole collection of tools becoming very unwieldy and difficult to work with. Is there any way to collect all of the different Mains into a single folder (suitably renamed) and configure Gradle to turn each one into a separate JAR?

FWIW, the JARs are currently created using https://github.com/johnrengelman/shadow . JAR size doesn't matter.

Thanks in advance.

Upvotes: 1

Views: 2415

Answers (2)

lance-java
lance-java

Reputation: 28101

I'm not sure if it's a good fit but you might be interested in my gradle-java-flavours plugin.

eg:

apply plugin: 'com.lazan.javaflavours'
javaFlavours {
    flavour 'tool1'
    flavour 'tool2'
}
dependencies {
    compile      'a:a:1.0' // common to all tools
    compileTool1 'b:b:2.0' // compile deps for tool1 only
    runtimeTool2 'c:c:2.0' // runtime deps for tool2 only
}

Directories

  • src/main/java, src/test/java, src/main/resources, src/test/resources - common code & tests
  • src/tool1/java, src/testTool1/java, src/tool1/resources, src/testTool1/resources - tool1 only sources
  • src/tool2/java, src/testTool2/java, src/tool2/resources, src/testTool2/resources - tool2 only sources

Jars

  • projectName.jar
  • projectName-tool1.jar
  • projectName-tool2.jar

Upvotes: 1

Sergey Fedorov
Sergey Fedorov

Reputation: 2169

Jars are just zip files with META-INF folder inside. Use Zip tasks to create them and dependsOn to run tasks as part of your build sequence.

I had the code like below for changing jar files:

task changeJar (type: Zip) {
    baseName project.name
    extension 'jar'
    destinationDir new File('build')
    entryCompression ZipEntryCompression.STORED
    from { zipTree(new File(core.libsDir, core.name + '.jar')) }
    from ( <somewhere else> ) {
        exclude 'META-INF/'
    }
}

Upvotes: 1

Related Questions