0

The assemble task includes the following tasks, as far as I know:

:compileJava
:processResources
:classes
:jar
:assemble

My jar task depends on a copy library task in which I copy all my libraries from my implementation configuration into a libs folder. To get a working executable jar currently I assemble my project, execute the copy libs task and then the jar task again (which iterates over the libs and adds their name to the classpath) as this works fine.

:compileJava
:processResources
:classes
:jar
:assemble
:copyLibs
:jar

But when I put the copy libs task before the jar task via dependsOn then when I execute my project the jar doesn’t find all libs, even though it seems like they have been copied successfully.

:compileJava
:processResources
:classes
:copyLibs
:jar
:assemble

EDIT: The copyLibs and jar task of my build.gradle

project.configurations.implementation.setCanBeResolved(true)

task copyLibs(type: Copy) {
    from configurations.implementation
    into "$buildDir/libs"
}
build.dependsOn copyLibs

jar {
    manifest {
        attributes 'Main-Class': 'dc.v077a.server.ServerMain',
                    'Class-Path': fileTree("$buildDir/libs").filter { 
it.isFile() && !it.name.startsWith("dc-v077a-server") }.files.name.join(' ')
    }
}
elsamuray7
  • 79
  • 1
  • 10
  • Your second snippet does not make sense because a task cannot be executed twice in a build. – Lukas Körfer Oct 29 '20 at 21:11
  • @Körfer I am executing the jar task separately. I execute the last two tasks separately. Packed all into a batch script. However I would like to just be able to run a single command that does it all. – elsamuray7 Oct 29 '20 at 22:49
  • Could you add (the relevant parts of) your `build.gradle` file to your question? – Lukas Körfer Oct 29 '20 at 22:50
  • @LukasKörfer I added the relevant parts. What I did in the seconds step was literally just replacing `build.dependsOn copyLibs` with `jar.dependsOn copyLibs`. – elsamuray7 Oct 30 '20 at 18:05

1 Answers1

0

Solved it by generating one fat executable jar that includes all dependencies. That is not exactly the same but in my case it is exactly what i needed:

project.configurations.implementation.setCanBeResolved(true)

/*
 * Generate a fat executable jar that includes all implementation dependencies
 */
jar {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
        attributes 'Main-Class': 'dc.v077a.server.ServerMain'
    }
    from {
        configurations.implementation.filter{ it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }
    }
}
elsamuray7
  • 79
  • 1
  • 10