net.sf.alchim.mojo.yuicompressor.YuiCompressorMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yuicompressor-maven-plugin Show documentation
Show all versions of yuicompressor-maven-plugin Show documentation
To compress (Minify + Ofuscate) Javascript files and CSS files (using YUI Compressor from Julien Lecomte) and/or
to check Javascript files with jslint.
package net.sf.alchim.mojo.yuicompressor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.IOUtil;
import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
/**
* Apply compression on JS and CSS (using YUI Compressor).
*
* @goal compress
* @phase process-resources
*
* @author David Bernard
* @created 2007-08-28
*/
// @SuppressWarnings("unchecked")
public class YuiCompressorMojo extends MojoSupport {
/**
* Read the input file using "encoding".
*
* @parameter expression="${file.encoding}" default-value="UTF-8"
*/
private String encoding;
/**
* The output filename suffix.
*
* @parameter expression="${maven.yuicompressor.suffix}" default-value="-min"
*/
private String suffix;
/**
* If no "suffix" must be add to output filename (maven's configuration manage empty suffix like default).
*
* @parameter expression="${maven.yuicompressor.nosuffix}" default-value="false"
*/
private boolean nosuffix;
/**
* Insert line breaks in output after the specified column number.
*
* @parameter expression="${maven.yuicompressor.linebreakpos}" default-value="0"
*/
private int linebreakpos;
/**
* [js only] Minify only, do not obfuscate.
*
* @parameter expression="${maven.yuicompressor.nomunge}" default-value="false"
*/
private boolean nomunge;
/**
* [js only] Preserve unnecessary semicolons.
*
* @parameter expression="${maven.yuicompressor.preserveAllSemiColons}" default-value="false"
*/
private boolean preserveAllSemiColons;
/**
* force the compression of every files,
* else if compressed file already exists and is younger than source file, nothing is done.
*
* @parameter expression="${maven.yuicompressor.force}" default-value="false"
*/
private boolean force;
/**
* show statistics (compression ratio).
*
* @parameter expression="${maven.yuicompressor.statistics}" default-value="true"
*/
private boolean statistics;
private long inSizeTotal_;
private long outSizeTotal_;
@Override
protected String[] getDefaultIncludes() throws Exception {
return new String[]{"**/**.css", "**/**.js"};
}
@Override
public void beforeProcess() throws Exception {
if (nosuffix) {
suffix = "";
}
}
@Override
protected void afterProcess() throws Exception {
if (statistics && (inSizeTotal_ > 0)) {
getLog().info(String.format("total input (%db) -> output (%db)[%d%%]", inSizeTotal_, outSizeTotal_, ((outSizeTotal_ * 100)/inSizeTotal_)));
}
}
@Override
protected void processFile(SourceFile src, File destRoot) throws Exception {
if (getLog().isDebugEnabled()) {
getLog().debug("compress file :" + src.toFile()+ " to " + src.toDestFile(destRoot, suffix));
}
File inFile = src.toFile();
File outFile = src.toDestFile(destRoot, suffix);
if (!force && outFile.exists() && (outFile.lastModified() > src.toFile().lastModified())) {
if (getLog().isInfoEnabled()) {
getLog().info("nothing to do, " + outFile + " is younger than original, use 'force' option or clean your target");
}
return;
}
InputStreamReader in = null;
OutputStreamWriter out = null;
try {
in = new InputStreamReader(new FileInputStream(inFile), encoding);
if (!outFile.getParentFile().exists() && !outFile.getParentFile().mkdirs()) {
throw new MojoExecutionException( "Cannot create resource output directory: " + outFile.getParentFile() );
}
out = new OutputStreamWriter(new FileOutputStream(outFile), encoding);
if (".js".equalsIgnoreCase(src.getExtension())) {
JavaScriptCompressor compressor = new JavaScriptCompressor(in, jsErrorReporter_);
compressor.compress(out, linebreakpos, !nomunge, jswarn, preserveAllSemiColons);
} else if (".css".equalsIgnoreCase(src.getExtension())) {
CssCompressor compressor = new CssCompressor(in);
compressor.compress(out, linebreakpos);
}
} finally {
IOUtil.close(in);
IOUtil.close(out);
}
if (statistics) {
inSizeTotal_ += inFile.length();
outSizeTotal_ += outFile.length();
getLog().info(String.format("%s (%db) -> %s (%db)[%d%%]", inFile.getName(), inFile.length(), outFile.getName(), outFile.length(), ((outFile.length() * 100)/inFile.length())));
}
}
}