Delete directory content from Gradle - 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"
}
}

Related

processResources task in gradle doesn't copy a file

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")
}

Gradle, copy and rename file

I'm trying to in my gradle script, after creating the bootJar for a spring app, copy and rename the jar that was created to a new name (which will be used in a Dockerfile). I'm missing how to rename the file (I don't want to have the version in docker version of the output file).
bootJar {
baseName = 'kcentral-app'
version = version
}
task buildForDocker(type: Copy){
from bootJar
into 'build/libs/docker'
}
You could directly generate the jar with your expected name, instead of renaming it after it has been generated, using archiveName property from bootJar extension:
bootJar {
archiveName "kcentral-app.jar" // <- this overrides the generated jar filename
baseName = 'kcentral-app'
version = version
}
EDIT
If you need to keep the origin jar filename (containing version), then you can update your buildForDocker task definition as follows:
task buildForDocker(type: Copy){
from bootJar
into 'build/libs/docker'
rename { String fileName ->
// a simple way is to remove the "-$version" from the jar filename
// but you can customize the filename replacement rule as you wish.
fileName.replace("-$project.version", "")
}
}
For more information, see Gradle Copy Task DSL
You can also do something like this:
task copyLogConfDebug(type: Copy){
group = 'local'
description = 'copy logging conf at level debug to WEB-INF/classes'
from "www/WEB-INF/conf/log4j2.debug.xml"
into "www/WEB-INF/classes/"
rename ('log4j2.debug.xml','log4j2.xml')

Gradle JAR files in a folder recursively

How can I JAR a folder and all of it's sub directories and files in a simple JAR?
I tried something like this and it does not work.
task jarVrCore(type: Jar, description: 'JARs core part of the project') {
doLast {
archiveName = "vasasdasasdasd"
from "${projectDir}"
println "${vrCoreSourceDir}"
destinationDir = file("${dirTmpLibsVr4}")
}
}
I always get an empty JAR with only the manifest.
The problem is that all of your task configurations are applied too late. You are using a doLast closure, which is executed after the actual task action (for a Jar task: the creation of the .jar file).
Normally, all task configuration is done directly inside the task configuration closure:
task jarVrCore(type: Jar, description: 'JARs core part of the project') {
archiveName = "vasasdasasdasd"
from "${projectDir}"
println "${vrCoreSourceDir}"
destinationDir = file("${dirTmpLibsVr4}")
}
If you need to apply configuration after other tasks have been executed, use a doFirst closure.
Please note, that using the example above, the println statement will be executed during configuration phase and therefore on each Gradle invocation, whether the task jarVrCore is executed or not.

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.

Copy all jars from subprojects to root project’s bin directory

I'd like to copy every jar generated from subprojects from their /bin/libs dir to the root project's bin dir. Unfortunately, my current code just copies everything from the subprojects into that directory:
task muleapp(type: Copy) {
from '.'
include '**/bin/libs/*.jar'
into 'bin'
}
How can I achieve the desired functionality?
Try this type of task:
task muleapp(type: Copy) {
def jars=[]
subprojects.each {
jars+=it.libsDir
}
from jars
into 'bin'
}

Resources