Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle - copy file after its generation

Tags:

java

gradle


I try to build jar and after that copy it to another folder.

task createJar(type: Jar) {     archiveName = "GradleJarProject.jar"     manifest {         attributes 'Implementation-Title': 'Gradle Jar File Example',               'Implementation-Version': version,             'Main-Class': 'me.test.Test'     }     baseName = project.name     from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }     with jar  }  task copyJarToBin {     copy {         from 'build/libs/GradleJarProject.jar'         into "d:/tmp"     } }  task buildApp (dependsOn: [clean, createJar, copyJarToBin]) 

But I can't figure out one problem. copyJarToBin task try to copy old jar. If I delete /build folder in the project and run buildApp() task, task createJar() will generate .jar file, but copyJarToBin() won't find that .jar file.

Could you help me?
Thanks.

like image 282
wazz Avatar asked Jun 04 '15 06:06

wazz


People also ask

What does a Gradle wrapper not do?

The Gradle wrapper is part of the Gradle build system and is also not Android specific. It also does not make the building easier, except that you don't need to have Gradle installed and then your build runs with the exact Gradle version the build is designed for.

What is Gradle doLast?

The doLast ensures that this code block is only executed during task execution. It basically tells Gradle during configuration to run this code block last while executing this task 1. We can also use the doFirst code block to run something first while executing the task.

Where are Gradle dependencies stored?

Gradle declares dependencies on JAR files inside your project's module_name /libs/ directory (because Gradle reads paths relative to the build.gradle file). This declares a dependency on version 12.3 of the "app-magic" library, inside the "com.example.android" namespace group.


1 Answers

The culprit is your copyJarToBin task. when doing

task copyJarToBin {     copy {         from 'build/libs/GradleJarProject.jar'         into "d:/tmp"     } } 

you copy the jar during the configuration time by using the copy method. (see the gradle user guide at https://docs.gradle.org/current/userguide/userguide_single.html#sec:build_phases to understand the build lifecycle) You want to run the actual copy operation during the execution phase (the execution of the task).

One way to solve that is to move the call of the copy method into a doLast block:

task copyJarToBin {     doLast {         copy {             from 'build/libs/GradleJarProject.jar'             into "d:/tmp"         }      } } 

The problem with this approach is that you won't benefit of gradles incremental build feature and copy that file every single time you execute the task even though the file hasn't changed.

A better and more idiomatic way of writing your copyJarToBin task is to change your task implementation to use the Copy task type:

task copyJarToBin(type: Copy) {     from 'build/libs/GradleJarProject.jar'     into "d:/tmp" }    

We can even improve this snippet by taking advantage of gradle's autowiring feature. You can declare the output of one task as input to another. So instead of writing `build/libs/GradleJarProject.jar' you can simply do:

task copyJarToBin(type: Copy) {     from createJar // shortcut for createJar.outputs.files     into "d:/tmp" }    

Now you don't need to bother about task ordering as gradle know that the createJar task must be executed before the copyJarToBin task can be executed.

like image 103
Rene Groeschke Avatar answered Oct 03 '22 06:10

Rene Groeschke