How to add multiple jars to the jar task in gradle?


(kant) #1

currently I have the following which generates the SDK.jar under project_name/build/libs

jar {
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    from sourceSets.test.output
    baseName = 'SDK'
    exclude 'META-INF/.RSA', 'META-INF/.SF','META-INF/*.DSA'
}

I want to be able to create multiple jars from the same project and I would like all the jars to end up in build/libs just like one jar above?


(Stefan Oehme) #2

Just create another Jar task

task myOtherJar(type: Jar) {
 from ...
}

(kant) #3

Hi I did do that and I also did the following.

jar.dependsOn(myOtherJar) // This is creating another jar in build/libs

However here is the tricky part. I have the following function. which collects all the jars besides the myOtherJar and I dont know why? This one configurations.testRuntime.allArtifacts collects all the jar besides myOtherJar however I made sure that all the jars are in src/build/libs. It only picks one but not the other

task linkToBin(dependsOn: jar) {
    doLast{
        File installDir = new File(rootProject.projectDir.getPath() + "/install/bin")
        installDir.mkdirs()
        configurations.testRuntime.allArtifacts.files.forEach { // Every jar reaches here besides myOtherJar
            Path targetPath = Paths.get(installDir.getPath() + '/' + it.getName())
            if(new File(targetPath.toString()).exists())
               Files.delete(targetPath)
            Files.createSymbolicLink(targetPath, Paths.get(it.getPath()))
        }
    }
}

(Stefan Oehme) #4

That is not necessary:

jar.dependsOn(myOtherJar)

What you probably want is to tell Gradle that your extra artifact is part of some configuration.

artifacts {
  archives myOtherJar
}

(kant) #5

Hi Thanks for the response. That helped to some extent. It did move all the jars to linkToBin Area however it also created another problem which is when I run my anchorHttpServerJar by running the following command java -jar anchor_echo_http_server-1.0.jar it still runs the main class under the jar block (com.hello.echoserver.AnchorEchoServer) instead of running com.hello.httpserver.AnchorHttpServer? any idea? I also dont know why anchor_echo_http_server-1.0.jar is getting created instead anchor_echo_http_server-1.0-SNAPSHOT.jar. There seems to be some conflict. isn’t it?

Here is my build.gradle

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'

group 'com.hello'
version '1.0'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    flatDir {
        dirs '/usr/local/hello/lib'
    }
}

dependencies {
    compile 'commons-cli:commons-cli:1.3.1'
    compile 'org.slf4j:slf4j-log4j12:1.7.12'
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile "com.sparkjava:spark-core:2.5"
    compile group: 'com.google.code.gson', name: 'gson', version: '2.6.2'
   compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.8.11.2'
   compile project(':anchor_service_api_thrift')
}

jar {
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    from sourceSets.test.output
    manifest {
        attributes 'Main-Class': 'com.hello.echoserver.AnchorEchoServer'
    }
    exclude 'META-INF/.RSA', 'META-INF/.SF','META-INF/*.DSA'
}

task (anchorHttpServerJar, dependsOn: 'classes', type: Jar) {
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    from sourceSets.test.output
    archivesBaseName = 'anchor_echo_http_server'
    version = '1.0-SNAPSHOT'
    manifest {
        attributes ('Main-Class': 'com.hello.httpserver.AnchorHttpServer')
    }
    exclude 'META-INF/.RSA', 'META-INF/.SF','META-INF/*.DSA'
}

artifacts {
   archives anchorHttpServerJar
}

build.dependsOn(linkToBin)