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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With