简体   繁体   中英

Why can’t I copy my dependencies before finishing the assemble task?

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(' ')
    }
}

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) }
    }
}

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