I’d like to integrate JavaScript file concatenation and minification as part of my java war project build and I’d appreciate some pointers on the best way to do this.
In the java plugin I understand that it is the processResources task that copies the files over from the sources to the build and I know how to concatenate and minify the files as a separate task but what I’m a bit stuck on is how I can prevent the copying of the *.js file as part of processResources and also do the concatenation+minification as part of a regular build.
My mistake though I see it’s actually the war task that copies my JavaScript files from the webapp dir, however the war task doesn’t seem to have a staging area where I can copy the minified files to so they will get incorporated in to the resulting war.
In that case I would suggest to create an extra task for tweaking your js sources. After you’ve created that task you can modify the given war task to ignore your js folder and add a from clause, that takes the output of your jstweaking task:
class JsTweak extends DefaultTask{
@InputDirectory inputFolder;
@OutputDirectory outputFolder;
@TaskAction
public void doTweak(){
//do some js minimization and optimization
}
}
task tweakJs(type:JsTweak){
inputFolder = file("$webAppDirName/js")
outputFolder = file("$buildDir/tweakedJs")
}
war {
exclude '**/js'
from tweakJs{
into("js")
}
}
Another even more concise solution might be to extend your JsTweak Task from SourceTask. This task type provides some convenient methods for dealing with sources. Have a look at javadoc:org.gradle.api.tasks.SourceTask for further information. If you have any further questions, don’t hesitate to ask.
I’m nearly there, just one last thing, the following copies all of my webapp files into the ‘js’ directory and not just the output of jsMini. How do I keep the defaults for all files except the jsMini output?
war {
exclude '**/js/*'
from jsMini {
into("js")
}
}
I am trying to take the class CatPack extends DefaultTask and add it to my build.gradle and I cannot get this to run:
* What went wrong:
Could not compile build file 'C:\usr\SYNCH\PACKT166\Chapters_Code\build.gradle'.
Cause: startup failed:
build file 'C:\usr\SYNCH\PACKT166\Chapters_Code\build.gradle': 664: unable to resolve class JavaScriptCompressor
@ line 664, column 21.
def c = new JavaScriptCompressor(file.newReader(), new SystemOutErrorReporter())
^
05:50:34.559 [ERROR] [org.gradle.BuildExceptionReporter] build file 'C:\usr\SYNCH\PACKT166\Chapters_Code\build.gradle': 651: unable to resolve class JavaScriptCompressor
05:50:34.575 [ERROR] [org.gradle.BuildExceptionReporter] build file 'C:\usr\SYNCH\PACKT166\Chapters_Code\build.gradle': 651: unable to resolve class SystemOutErrorReporter
I then noticed that com.yahoo.platform.yui:yuicompressor was not in my maven local repo
When I navigated to my .gradle artifact repo, it lives there, but keeps downloading one that is 0kb and I have tried several times to delete this. C:\Users\mknuts6173c.gradle\caches\artifacts-7\artifacts\56865c09425cc6ecda8606d7e6035ceb\com.yahoo.platform.yui\yuicompressor\2.4.6\jar
But that seems to be a link to another file 5b8477e867040ba58b6c8a7ac6deb6da22109237 which is the same 82k as the one that should be in the mvnrepo.
I also cannot seem to figure out how to force gradle to download this artifact as I have it listed. Why wont the jar’s download into my mavenLocal ?
Here is the new bottom section :
classpath group: 'com.yahoo.platform.yui', name: 'yuicompressor', version: '2.4.6'
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath group: 'com.yahoo.platform.yui', name: 'yuicompressor', version: '2.4.6'
}
}
task jsMini(type: CatPack) {
inputFolder = file("src/main/webapp/js")
outputFile = file("$buildDir/jsmin/modules.min.js")
}
import com.yahoo.platform.yui.compressor.JavaScriptCompressor
class CatPack extends DefaultTask {
@InputDirectory
File inputFolder;
@OutputFile
File outputFile;
@TaskAction
public void doCatPack() {
def fw = new FileWriter(outputFile)
inputFolder.eachFileMatch(~/.*\.js/) { File file ->
def c = new JavaScriptCompressor(file.newReader(), new SystemOutErrorReporter())
// 0 = line breaks after each rule, -1 = no line breaks
c.compress(fw, -1, false, false, false, false)
}
fw.flush()
fw.close()
}
}