简体   繁体   中英

Gradle - copy jar to another folder as part of the 'jar' task

I have a multi project gradle setup. I want to simply copy the generated jar file of any project anytime the jar is rebuild and thought this would work in my root project's subprojects closure:

task copyJarToGenerated(type: Copy) {
    from jar
    into "../my-generated-jars/"
}
copyJarToGenerated.mustRunAfter jar

But if I run the 'clean' task then 'jar' task of any sub project, my jar is generated under build/libs as usual but not copied.

Looking at the gradle output, it only runs compileJava, processResources, classes then jar. It isn't running copyJarToGenerated.

The method mustRunAfter does not define a task dependency, it just enforces a specific order. It basically says 'if both tasks are executed in a build (for whatever reason), then they are executed in the specified order'.

So you need to define the task dependency on your own:

jar.finalizedBy copyJarToGenerated

You could also just add copyJarToGenerated as a dependency of the lifecycle task build :

build.dependsOn copyJarToGenerated

Since you specify the task jar in the method from of your task, it is registered as a task input and therefor registered as a task dependency implicitly. So you won't need to define order with mustRunAfter anymore.

You may also consider using the property destinationDirectory of the task jar instead of creating a new task at all.

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