I would like to change the destination dir for file copy depending on the chosen build.
This does not work since the task graph is executed in execution phase but the copyTask is set in configuration phase.
How can I achieve this?
gradle.taskGraph.whenReady { taskGraph ->
println('taskGraph')
if (taskGraph.hasTask(buildRelease)){
File toDir=file('test/r')
println('Copy to: ' + toDir.getName())
}else if (taskGraph.hasTask(buildDevel)) {
File toDir=file('test/d')
println('Copy to: ' + toDir.getName())
}
}
task buildDevel (dependsOn: ['copyTask']){}
task buildRelease (dependsOn: ['copyTask']){}
task copyTask(type: Copy) {
from "test"
into toDir
include 'a.txt'
}
This may help You (You need to create a.txt file on the same level as build.gradle is located:
gradle.taskGraph.whenReady { taskGraph ->
def cp = project.copyTask
if (taskGraph.hasTask(buildRelease)){
cp.into 'lol1'
} else if (taskGraph.hasTask(buildDevel)) {
cp.into 'lol2'
}
}
task buildDevel (dependsOn: ['copyTask']){}
task buildRelease (dependsOn: ['copyTask']){}
task copyTask(type: Copy) {
from file('.')
include 'a.txt'
}
This will also work:
task buildDevel
task buildRelease
buildDevel.doFirst {
cp('lol1')
}
buildRelease.doFirst {
cp('lol2')
}
def cp(to) {
copy {
from file('.')
into to
include 'a.txt'
}
}
Related
Given is the following Gradle task:
task copyResource(type: Copy) {
from('.') {
include '../anotherProject/password.txt'
}
into 'build/docker'
}
On task execution the include is ignored. What is the correct way to reference to a file from another project directory?
Try this :
task copyResource(type: Copy) {
from('../anotherProject') {
include 'password.txt'
}
into 'build/docker'
}
or even simpler :
task copyResource(type: Copy) {
from('../anotherProject/password.txt')
into 'build/docker'
}
I have a copy task for 1 file
task myCopyTask(type: Copy) {
copy {
from "/path/to/my/file"
into "path/to/out/dir"
}
}
How to do, so the task fails if the copy fails? Right now if the file does not exist, it does not give an error.
Fail Gradle Copy task if source directory not exist gives a solution. This does not work, because if everything is not inside of
copy { ... }
the task does not work at all.
I tried also
task myCopyTask(type: Copy) {
copy {
from "/path/to/my/file"
into "path/to/out/dir"
inputs.sourceFiles.stopExecutionIfEmpty()
}
}
The above would fail, as inputs.sourceFiles would be empty.
Why don't you specify your task as:
task myCopyTask(type: Copy) {
from "/path/to/my/file"
into "path/to/out/dir"
inputs.sourceFiles.stopExecutionIfEmpty()
}
This would work as expected during execution phase, while your solution would try to copy something during configuration phase of the build every time you call any task.
The very first definition of the tasks actually doesn't do what you expect from the task:
task myCopyTask(type: Copy) {
copy {
from "/path/to/my/file"
into "path/to/out/dir"
}
is acutually same as
task myCopyTask(type: Copy) {
project.copy {
from "/path/to/my/file"
into "path/to/out/dir"
}
}
And it will execute copy action during task configuration, no matter if the task is called or not.
What you need is:
task myCopyTask(type: Copy) {
from "/path/to/my/file"
into "path/to/out/dir"
doFirst {
if(inputs.empty) throw new GradleException("Input source for myCopyTask doesn't exist")
}
}
This solution for Kotlin DSL works for me:
tasks.register<Copy>("myCopyTask") {
val path = "/path/to/my/file"
from(path)
into("path/to/out/dir")
if (inputs.sourceFiles.isEmpty) {
throw GradleException("File not found: $path")
}
}
I've a build file that runs tasks like this.
Task 1 (unpackWar): Unzips war file to Temp folder
Task 2 (copyWarFilesToWebContent): Copies the files to WebContent folder with some exclusions
Task 3 (copyRequiredJarFilesToWebContent): Unzips a couple of jar files from Temp/WEB-INF/lib to TempJarDir
Task 4 (explodeProductJars): Copies files we want from TempJarDir to WebContent folder
There is a single prepare task that runs each of these tasks using dependsOn and I've added mustRunAfter commands to each of the tasks so they execute in order. Also set upToDateWhen = false for each task.
What seems to happen is that Task 1 runs fine and unzips the war. Task 2 then uses the files from Temp and adds the required ones to WebContent correctly.
Task 3 and Task 4 are always coming back as Up To Date because seemingly there are no files to work with in the directory specified.
If I re-run prepare when the Temp folder exists, Task 3 and 4 run correctly.
I'm not sure if this is due to how fileTree works or if I'm doing something wrong. I've picked up gradle about a week ago and am still getting to grips with it.
Tasks look like this:
task prepare(dependsOn: ['unpackWar', 'copyWarFilesToWebContent', 'copyRequiredJarFilesToWebContent'])
prepare.outputs.upToDateWhen {false}
task unpackWar(type: Copy) {
description = 'unzips the war'
outputs.upToDateWhen { false }
def warFile = file(warFileLocation)
from zipTree(warFile)
into "Temp"
}
task copyWarFilesToWebContent(type: Copy) {
mustRunAfter unpackWar
description = 'Moves files from Temp to WebContent Folder'
outputs.upToDateWhen { false }
from ('Temp') {
exclude "**/*.class"
}
into 'WebContent'
}
task explodeProductJars(type: Copy) {
outputs.upToDateWhen { false }
FileTree tree = fileTree(dir: 'Temp/WEB-INF/lib', includes: ['application*-SNAPSHOT-resources.jar', 'services*-SNAPSHOT-resources.jar'])
tree.each {File file ->
from zipTree(file)
into "TempJarDir"
}
}
task copyRequiredJarFilesToWebContent(type: Copy, dependsOn: explodeProductJars) {
mustRunAfter copyWarFilesToWebContent
outputs.upToDateWhen { false }
from ("TempJarDir/META-INF/resources") {
include '**/*.xml'
}
into "WebContent/WEB-INF"
}
I've a feeling its something to do with fileTree but not sure whats happening exactly.
The Copy task is tricky. A Copy task will only be executed when it finds something to copy in the configuration phase. If it does not find anything during that phase it will be skipped.
You could use the copy method instead of the Copy task.
prepare( dependsOn: 'copyRequiredJarFilesToWebContent' ) {}
task unpackWar( type: Copy ) {
def warFile = file( warFileLocation )
from zipTree( warFile )
into 'Temp'
}
task copyWarFilesToWebContent( dependsOn: unpackWar ) << {
copy {
from ( 'Temp' ) {
exclude '**/*.class'
}
into 'WebContent'
}
}
task explodeProductJars( dependsOn: copyWarFilesToWebContent ) << {
copy {
FileTree tree = fileTree( dir: 'Temp/WEB-INF/lib', includes: [ 'application*-SNAPSHOT-resources.jar', 'services*-SNAPSHOT-resources.jar' ] )
tree.each { File file ->
from zipTree( file )
into 'TempJarDir'
}
}
}
task copyRequiredJarFilesToWebContent( dependsOn: explodeProductJars ) << {
copy {
from ( 'TempJarDir/META-INF/resources' ) {
include '**/*.xml'
}
into 'WebContent/WEB-INF'
}
}
I need to create two different .properties files from two different .properties.dist if they don't exist, so I'm using a Copy task and I'm specifying the from and the into accordingly.
At the moment I had to create two different tasks, each of which is creating a file like this:
task copyAndRenameDialling(type: Copy){
if(!file("./properties/dialling.properties").exists()){
from './dist/dialling.properties.dist'
into './properties/'
rename{ String fileName ->
fileName.replace('.dist','')
}
}
}
task copyAndRenameFiles(type: Copy){
if(!file("./properties/file.properties").exists()){
from './dist/files.properties.dist'
into './properties/'
rename{ String fileName ->
fileName.replace('.dist','')
}
}
}
task copyAndRenameProperties {
dependsOn << copyAndRenameDialling
dependsOn << copyAndRenameFiles
}
and I run the task with gradle copyAndRenameProperties.
Is it possible to make the two Copy tasks parameterized based on the name of the file, so that I have only one generic copyAndRename?
If so, how can I pass the parameter to the task?
I'd do it like this:
task copyAndRenameProperties(type: Copy) {
from 'dist'
include '*.properties.dist'
into 'properties'
rename { it - ~/\.dist$/ }
eachFile { if (file("properties/$it.name").file) it.exclude() }
}
or if you really only want those two specific files
task copyAndRenameProperties(type: Copy) {
from 'dist/dialling.properties.dist'
from 'dist/file.properties.dist'
into 'properties'
rename { it - ~/\.dist$/ }
eachFile { if (file("properties/$it.name").file) it.exclude() }
}
or
task copyAndRenameProperties(type: Copy) {
from 'dist'
include 'dialling.properties.dist', 'file.properties.dist'
into 'properties'
rename { it - ~/\.dist$/ }
eachFile { if (file("properties/$it.name").file) it.exclude() }
}
Suppose this gradle script:
task copyGroovyScript(dependsOn: prepare, type: Copy) {
from "${scriptSrcLocation}/${scriptSrcName}"
into buildFolderZipSource
}
task copyDependenciesForGroovyScript(dependsOn: copyGroovyScript, type: Copy) {
from configurations.groovyScript.resolve()
into "${buildFolderZipSource}/groovy-plugin-lib"
}
task copyTestScripts(dependsOn: copyDependenciesForGroovyScript, type: Copy ) {
from "${scriptSrcLocation}/ReadClient.groovy"
into "${buildFolderZipSource}/test"
}
task copyTestScriptsBin(dependsOn: copyTestScripts, type: Copy ) {
from "${scriptSrcLocation}/bin"
into "${buildFolderZipSource}/test/bin"
}
task copyDependenciesForTestScripts(dependsOn: copyTestScriptsBin, type: Copy) {
from configurations.testScripts.resolve()
into "${buildFolderZipSource}/test/lib"
}
task packageAll(dependsOn: copyDependenciesForTestScripts, type:Zip) {
archiveName "output-${buildTime()}.zip"
excludes ['*.zip']
destinationDir buildFolder
from buildFolder
}
I need different Copy tasks before they have different output folders.
Is there a way to avoid having to have all those dependsOn statements and just have gradle execute things in order of declaration in the file somehow?
There's no way to execute in the way it's declared. But why don't you go this way:
task packageAll(dependsOn: copyDependenciesForTestScripts, type:Zip) {
doFirst {
copy {
from "${scriptSrcLocation}/${scriptSrcName}"
into buildFolderZipSource
}
}
//following doFirst and so on..
archiveName "output-${buildTime()}.zip"
excludes ['*.zip']
destinationDir buildFolder
from buildFolder
}
EDIT
After discussion in comments it turned out that the following piece of code should do the job
task prepare {
doFirst {
copy {
from "${scriptSrcLocation}/${scriptSrcName}"
into buildFolderZipSource
}
}
//following doFirst and so on..
}
task packageAll(dependsOn: prepare, type:Zip) {
archiveName "output-${buildTime()}.zip"
excludes ['*.zip']
destinationDir buildFolder
from buildFolder
}