Transform content of war file

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?

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

This is what I have done :

def processFileInplace(file, Closure processText) {
    def text = file.text
    file.write(processText(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
    doLast{
        new File(destinationDir,"js").eachFileMatch(~/^less-.*\.js$/){ it.delete() }
        sourceCss.eachFileMatch(~/.*\.less$/){
            File destinationCss= new File(it.parent,it.name.replaceAll(/\.less$/,".css"))
            new LessCompiler().compile(it,destinationCss);
            it.delete();
        }
        processFileInplace(fileToUpdate){ text ->
            text.replaceAll(/\.less/,".css")
                    .replaceAll(/rel="stylesheet\/less"/,"rel=\"stylesheet\"")
                    .replaceAll(/.*lesscss.*/,"")
        }
    }
}
  task packWar(type: Zip, dependsOn: explodedWar) {
    archiveName=war.archiveName
    destinationDir=war.destinationDir
    from "$buildDir/explodedWar"
}
task deploy(type:Copy) {
    into tomcatdir
    rename(war.archivePath.name,warName+'.war')
}
  if(env=='test'){
    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!

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…

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