Transform content of war file

(Thibault Delor) #1

Hi, I am using lesscss in a Java web project and my goal is to :

  • compile the less files into css (the less files are in my src, but I want to put the generated files in the build dir)

  • remove the less files from my war and to put the compiled css instead

  • change some jsp files to reflect the changes

All changes must not affect the sources, only the generated war. My problem is that I don’t know how to modify the files of the war. Is there a moment in the build process where I can access the exploded version of the war?

(Luke Daley) #2

You never want to transform something in place with Gradle, it kills build incrementalness.

What you want to do is not have the original less files be copied into the war…

war {
  exclude "less/**"

But you also want to include the output of your task that processes the less files. It’s hard to give the code snippet to do this without knowing how you’ve implemented the less task. But if the task is implemented correctly and declares its output files, it would be…

war {
  into "css", { from lessCssTask }

(Thibault Delor) #3

This is what I have done :

def processFileInplace(file, Closure processText) {
    def text = file.text
task explodedWar(type: Sync) {
    into "$buildDir/explodedWar"
    File sourceCss=new File(destinationDir,"styles")
    File fileToUpdate=new File(destinationDir,"WEB-INF/tags/util/load-scripts.tagx")
    with war
        new File(destinationDir,"js").eachFileMatch(~/^less-.*\.js$/){ it.delete() }
            File destinationCss= new File(it.parent,\.less$/,".css"))
            new LessCompiler().compile(it,destinationCss);
        processFileInplace(fileToUpdate){ text ->
  task packWar(type: Zip, dependsOn: explodedWar) {
    from "$buildDir/explodedWar"
task deploy(type:Copy) {
    into tomcatdir
    deploy.dependsOn war;
    deploy.from war;
} else {
    deploy.dependsOn packWar;
    deploy.from packWar;

This far from being perfect, I don’t declare the input/output, this is dirty… But, It works! My main issue was to update the jsp file with the css files, since I don’t want to modify the content of the source, I needed to have a copy in the build directory. If someone has a better solution, let’s share!

Thanks Luke for your response. I am really glad to see that each time I have posted to this forum, a core dev has answered. Gradle rocks in all ways!

(Thibault Delor) #4

Weird bug with the code tag… Starting at “#46;js$/){ it.delete() }”, It should not be in the post, and when I try to edit, this code doesn’t appear…

(Thibault Delor) #5

weird bug with the code tag… the code you see has been transformed…