Gradle: zip files in jar task - gradle

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.

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 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.

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

How to create a war file from a directory in gradle

I am writing a task to unzip a war file, remove some jars and then create a war from extracted folder.
task unzipWar(type: Copy){
println 'unzipping the war'
def warFile = file("${buildDir}/temp/libs/webapps/service-app.war")
def warOutputDir = file("$buildDir/wartemp")
from zipTree(warFile)
into warOutputDir
}
task deleteJars(type: Delete){
println 'deleting the logging jars'
file("$buildDir/wartemp/WEB-INF/lib/slf4j-api-1.7.5.jar").delete();
file("$buildDir/wartemp/WEB-INF/lib/logback-classic-1.1.7.jar").delete();
file("$buildDir/wartemp/WEB-INF/lib/logback-core-1.1.7.jar").delete();
}
task createWar(type: War){
destinationDir = file("$buildDir")
baseName = "service-app"
from "$buildDir/wartemp"
dependsOn deleteJars
}
For some reason, the jars are not getting deleted and the war file is getting created which only includes MANIFEST.MF and nothing else. What am I missing here?
First thing to note, is that your createWar task depends on deleteJarstask, but deleteJars doesn't depend on unzipWar. It seems, that if you call the createWar task it won't call unzipWar task and there will be nothing to copy or delete. Note that you have a MANIFEST.MF file, because it was generated by createWar task.
And the second thing is that you are trying to delete some files in the configuration stage of the build, though your unzipWar will do it's job in the execution phase. So your delete task will try to delete this files just before they are even unzipped. You can read about build lifecycle in the official userguide. So you need to rewrite your deleteJars task, to configure it properly. Take a look into the docs, it has an example how to do it.
So if you call a
file("$buildDir/wartemp/WEB-INF/lib/slf4j-api-1.7.5.jar").delete();
it tries to delete your files at the time it's called, because it's not a task property, but an action at the configuration.
To configure it you have to do something like:
task deleteJars(type: Delete) {
delete "$buildDir/wartemp/WEB-INF/lib/slf4j-api-1.7.5.jar", "$buildDir/wartemp/WEB-INF/lib/logback-classic-1.1.7.jar", "$buildDir/wartemp/WEB-INF/lib/logback-core-1.1.7.jar"
}

How to run a task after another using taskGraph.whenReady closure

I'm trying to include a ZIP file inside a TAR file in a gradle build. I'm not insane, this is to replicate an existing ant script and I cannot change the distribution layout for various business reasons.
I'm having to use a whenReady closure to collect dependencies without problems
whenReady means that the ZIP file is not built until after the buildTar task has completed, even though buildTar depends on buildZip.
I cannot call tar {} directly as gradle does not support this
Gradle does not appear to support calling tasks directly.
This is the general layout I have
task buildZip(type: Zip) {
gradle.taskGraph.whenReady {
// build zip file usual way with from blocks
from(...) {
}
from(...) {
}
}
doLast {
println "ZIP ready"
// could I call tar task from here??
}
}
task buildTar(type: Tar, dependsOn: buildZip) {
println "Building TAR"
from (buildZip.archivePath) {
}
... more stuff, installer script etc.
}
Output I see with gradle :buildTar, i.e. the TAR builds before the ZIP is built.
Building TAR
ZIP ready
Update.
Perryn Fowler comment below identifies the issue correctly, it is based on my misunderstanding of execution vs configuration in gradle.
The Tar is not being built before the Zip, the Tar task is being
configured before the Zip task is executed
Update.
This question is no longer necessary as the option duplicatesStrategy can be used in the ZIP task to avoid the problem being 'fixed' with gradle.taskGraph.whenReady
The answer to this question was actually provided by Perryn Fowler in the top comments, it was was based on my misunderstanding of execution vs configuration in gradle. I've created this answer so the question is marked as answered. The other answer simply paraphrases the original question with a link to the userguide.
The Tar is not being built before the Zip, the Tar task is being
configured before the Zip task is executed
i.e. any nested commands within a special task, e.g. Zip, Tar etc. are run and configuration time, the from blocks are executed later.
Here You have a sample working solution:
build.gradle:
task buildZip(type: Zip) {
from 'dir'
destinationDir project.file('build/zip')
archiveName 'lol.zip'
}
task buildTar(type: Tar, dependsOn: buildZip) {
from 'build/zip'
include '*.zip'
destinationDir project.file('build/tar')
archiveName 'lol.tar'
}
Is that clear for You?
P.S. I think that's a good idea for You to read userguide.

Resources