gradle simple file copy is not working - gradle

I'm very new to Gradle and am running this code to copy files from one dir to another ..but it does not work..
task copyTask (type: Copy) {
doLast{
from 'source'
into 'dest'
}
}
The gradle build output
λ gradle copyTask
:copyTask UP-TO-DATE
BUILD SUCCESSFUL
Total time: 1.244 secs
I'm not sure what is it that I'm doing wrong. The files under source dir is not copied to dest dir. Even if there is something wrong with my config why is the copyTask showing as up-to-date ?

You should remove the doLast block; it will be executed after the task has been executed. In other words, you are configuring the task after it has run.
Gradle is howing UP-TO-DATE as there is nothing to copy.
Try with:
task copyTask (type: Copy) {
from 'source'
into 'dest'
}

Related

gradle copy file task not working in build

I am new to gradle, I want copy the jar file generated by gradlew build to another dir.
task myCopyTask(type: Copy) {
from "build/libs/gs.jar"
into "D:/bin/gs"
}
I add above task to the build.gradle which belong to gs module which will generate gs.jar.
The problem is the command gradlew build will not do the copy and this task indeed executed(I add println in myCopyTask). However, the command gradlew myCopyTask works.
First I thought maybe the copy task running too early, so I change it to
task myCopyTask(type: Copy) {
doLast {
from "build/libs/gs.jar"
into "D:/bin/gs"
}
}
This is not working even by gradlew myCopyTask. Only first version can work by command gradlew myCopyTask, the terminal will show: 1 actionable task: 1 executed
What is the problem?
You haven't wired the task into Gradle's DAG so currently it will only executed when you do gradlew myCopyTask
You'll probably do something like
apply plugin: 'base' // adds build and assemble lifecycle tasks
task myJarTask(type:Jar) {...}
task myCopyTask(type: Copy) {
dependsOn myJarTask
...
}
assemble.dependsOn myCopyTask
See https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:task_dependencies

Gradle : UP-TO-DATE task

I have task 'compileProfileObj' which mainly has to create a jar file.
Even in fresh environment setup, gradle logs outputs 'UP-TO-DATE' for this.
task compileProfileObj(type: JavaCompile, dependsOn: [generateArtifacts]) {
classpath = configurations.deployment
source fileTree(
dir: "${objDir}/app",
includes: ['com/main/**'])
destinationDir file("${proDir}/profileobjects-biz.jar")
doLast {
copy {
from "${objDir}"
include '*.xml'
exclude 'server.xml'
into "${proDir}/profileobjects-biz.jar/META-INF"
}
copy {
from "${objDir}/profileobjects"
into "${proDir}/profileobjects-biz.jar/META-INF"
}
}
}
I understand the gradle doesnot executed tasks in some cases if the tasks has successfully executed in previous build runs.
But in my case even in 1st run gradle doesn't execute this task and prints 'UP-TO-DATE'
Googling didnt help me much here.
Any suggestions in what scenarios this might happen will be really helpful.

Gradle: zip files in jar task

I tried to zip files in the jar task but my Zip task is executed during gradle configuration phase. This is my simplified code:
task libZip(type: Zip) {
from configurations.runtime
archiveName 'lib.zip'
println "zip was created"
}
jar {
dependsOn libZip
...
doLast {
// suggested place to zip files
}
All works fine but the zip operation takes a "long" time. It should not be executed in configuration phase. Ok, no problem with a "doLast" in libZip task but I wanted the Zip file to be created when I do a "gradle :project:jar". I can`t get it to work to get both.
Please help
Ok, I leave it like it is because the zip is not created in configuration phase (I thought so) but only the println "zip is created" was printed to console.

Incorrect UP-TO-DATE with gradle copy task

I have copy task configured like this:
tmp = "$project.buildDir/tmp"
tmpClassesDir = "$tmp/WEB-INF/classes"
task copyFiles(type: Copy) {
from sourceSets.main.java.srcDirs
include '*properties'
into tmpClassesDir
}
After than clean task deletes build directory, if I run copyFiles once again it's UP-TO-DATE
It works if I add --rerun-tasks or set outputs.dir to tmp, but when outputs.dir is set as tmpClassesDir gradle says it's UP-TO-DATE
Any ideas what causes that strange behavior?
UPDATE: Problem exists only on remote test servers - when running local it's working well - any ideas?

Zip task marked as up-to-date

Hi I try to collect plugins from sub-folder, zip them and copy to my export folder.
task buildPlugin {
dependsOn ':empty-plugin:build'
}
task exportPlugin(type: Zip) {
dependsOn buildPlugin
// create new export folder as destination for nightly build
def folder = '/export';
def file = "${project.name}-sdk-${project.version}";
// collect all plugins into cwc-sdk zip file
baseName = file
fileTree("cwc-plugin").each({
if (it.name.endsWith(".zip")) {
from it.absolutePath
}
})
// move cwc-sdk zip file into export destination folder
copy { into folder from file }
delete file
}
I run clean task first. The gradle logs:
:api:compileJava
:api:processResources
:api:classes
:api:jar
:empty-plugin:compileJava
:empty-plugin:processResources
:empty-plugin:classes
:empty-plugin:jar
:empty-plugin:assemble
:empty-plugin:compileTestJava UP-TO-DATE
:empty-plugin:processTestResources UP-TO-DATE
:empty-plugin:testClasses UP-TO-DATE
:empty-plugin:test UP-TO-DATE
:empty-plugin:check UP-TO-DATE
:api:javadoc
:empty-plugin:zip
:empty-plugin:build
:buildPlugin
:exportPlugin UP-TO-DATE
BUILD SUCCESSFUL
Total time: 2.097 secs
While first run :exportPlugin is marked as UP-TO-DATE and I don't get the zipped file from build. When I run :exportPlugin again everything is fine. It's also fine when I chain both tasks manually (rungradle clean, next run gradle buildPlugin, run gradle exportPlugin by doublclick to tasks at IDEA)
I think the order of tasks are still ok. I don't need to work with mustRunAfter.
I also played around with copySpec, buildplugin.outputs.files. But nothing helps.
Can anybody help me to solve this issue for initial build run?
Thanks!
Update:
A Zip task is an abstracted Copy task
AbstractCopyTask is the base class for all copy tasks. (Docu)
I found this comment from Peter Niederwieser
A Copy task only gets executed if it has something to copy. Telling it what to copy is part of configuring the task, and therefore needs to be done in the configuration phase, rather than the execution phase.
How do I change from it.absolutePath code line inside fileTree loop to be part during configuration phase?

Resources