简体   繁体   中英

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:

在此处输入图片说明

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 Main s 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.

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/'
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM