Copy all created & third-party jars into a single folder with Gradle
Asked Answered
P

1

7

we have a multi-project gradle setup with one Java jar for each subproject:

- root-project
  |-sub-project-a
  |-sub-project-b
  |-sub-project-c

Now, because we are creating a Java webstart application, we need to sign all project jars as well as all third-party libraries (dependencies).

My approach was now to copy all built subproject jars and all third-party libraries into a seperate folder and execute a task for signing them. However I am not able to copy the jars.

This was my approach in the root build.gradle:

task copyFiles(type: Copy, dependsOn: subprojects.jar) {
    from configurations.runtime
    from("build/libs")
    into("webstart/lib")
    include('*.jar')
}

together with:

task signAll(dependsOn: [copyFiles]) << {
    new File('webstart/signed').mkdirs()
    def libFiles = files { file('webstart/lib').listFiles() }
    ...
}

Then I tried to execute gradle signAll. However, I can only find an empty jar with the name of the root project in the webstart/lib folder.

Maybe my approach is completely wrong. What do I have to do to copy all created & thrid-party jars into a single folder?

Pressurecook answered 21/7, 2014 at 15:16 Comment(0)
M
10

Add this piece of code to root build.gradle and it should work fine:

allprojects {
    apply plugin: 'java'
    repositories {
        mavenCentral()
    }
}

task copyJars(type: Copy, dependsOn: subprojects.jar) {
    from(subprojects.jar) 
    into project.file('dest')
}

task copyDeps(type: Copy) {
    from(subprojects.configurations.runtime) 
    into project.file('dest/lib')
}

task copyFiles(dependsOn: [copyJars, copyDeps])
Mathers answered 21/7, 2014 at 20:11 Comment(1)
Hi, thanks for the answer, this works great! Just one thing: Now the jars of all my own projects which are shared are copied both to dest and dest/lib. E.g. if sub-project-a requries sub-project-b, then sub-project-b.jar is copied both to dest and dest/lib. Any way to prevent shared projects from being copied to dest/lib as well?Pressurecook

© 2022 - 2024 — McMap. All rights reserved.