processResources task in gradle doesn't copy a file - gradle

While processing resources in a gradle build, is it possible to copy certain special resources to a target directory?
processResources {
println "Project Dir : $projectDir"
copy {
from ('${projectDir}/../../../project-abc/src/main/resources/SkipColumns.properties')
into ('${projectDir}/target/classes/META-INF/project-abc')
}
}
gradle processResources
When I run above command, the build is successful. But it doesn't copy SkipColumns file.

Removing parentheses worked.
copy {
from ("$projectDir/../../../project-abc/src/main/resources/SkipColumns.properties")
into ("$projectDir/target/classes/META-INF/project-abc")
}

Related

Delete directory content from Gradle

I use this Gradle configuration to copy generated asciidoc files.
But I need to delete old outdated files also.
task copyDocument(type: Copy) {
dependsOn bootJar
from file("build/docs/asciidoc/")
into file("src/main/resources/static/")
}
How I can delete the content of directory src/main/resources/static/ before I paste the new generated files?
task copyDocument(type: Copy) {
dependsOn bootJar
from file("build/docs/asciidoc/")
into file("src/main/resources/static/")
doFirst {
delete "build/folder"
}
}

Download from a repository and execute the jar as first step in Gradle build

My gradle build file needs to do this:
1. Download a jar file from an Artifactory repo.
2. Execute that jar file with some specific command line arguments.
Is there any code example of how to accomplish this?
For downloading a jar you can use Ant 'get' task. For executing the jar after it has been downloaded, you can use Gradle 'javaexec' method from Project API.
An example of script that should work:
// A first task to download the needed Jar into target libs directory
task download {
doLast {
ant.get(dest: 'libs/lombok-1.18.2.jar', src: 'http://central.maven.org/maven2/org/projectlombok/lombok/1.18.2/lombok-1.18.2.jar')
}
}
// A second task that executes the Jar , with some parameters
task execJar {
dependsOn download
doLast {
javaexec {
main = "-jar";
args = [
"libs/lombok-1.18.2.jar",
"version"
]
}
}
}

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.

How to install/run a watch task in Gradle

I'd like to run a given task, every time a file in the folder src changes.
It seems that Gradle does not have a task like that, but there is the gradle-watch-plugin on github. Following the installation guide, I tried:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.bluepapa32:gradle-watch-plugin:0.1.5'
}
}
apply plugin: 'com.bluepapa32.watch'
task "sometask" << {
println "My Own task."
}
watch {
somename {
files files('src')
tasks 'sometask'
}
}
Unfortunately this results in an error:
Starting:watch FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':watch'.
> org.gradle.tooling.BuildLauncher.withArguments([Ljava/lang/String;)Lorg/gradle/tooling/BuildLauncher;
So what's wrong with my build.gradle?
This can be done without a plugin by enabling continuous mode in your build via the --continuous or -t command line argument. For example, given the following build script running gradle -t myTask will automatically watch for changes in the folder src and reexecute the task when those files change.
task myTask {
inputs.files 'src'
doLast {
// do some stuff with files in 'src' folder
}
}

Exclude .bak files from build

How can I exclude certain files and folders from build and other tasks like creating a war file, copying files ?
I would like certain files like '.bak' to be excluded from build
Is there an equivalent of ant defaultexcludes task ?
I know that I can use ant.defaultexcludes in gradle but I was looking for an alternative
You can configure all Copy, Zip and similar tasks at once with something like tasks.withType(AbstractCopyTask) { it.exclude '**/*.bak' } or if you have a multi-project build allprojects { tasks.withType(AbstractCopyTask) { it.exclude '**/*.bak' } }.

Resources