yogsma
yogsma

Reputation: 10586

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?

Upvotes: 1

Views: 933

Answers (1)

Stanislav
Stanislav

Reputation: 28106

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

Upvotes: 1

Related Questions