日期:2014-05-16 浏览次数:20548 次
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-project</artifactId> <version>2.0.9</version> <scope>provided</scope> </dependency> </dependencies>
package com.mycompany.mojo.jsmin;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Pattern;
public abstract class JsFileProcessor {
private static final int buffer_size = 512;
private static final Pattern block = Pattern.compile( "(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)" );
private static final Pattern singleLine = Pattern.compile( "(?<=.*)//[^\"\\n\\r]*(?=[\r\n])" );
public static void process(File inputFile, File outputFile) throws IOException {
FileInputStream fis = null;
FileWriter fw = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream(buffer_size * 2);
try {
fis = new FileInputStream(inputFile);
fw = new FileWriter(outputFile);
int length = -1;
byte[] buffer = new byte[buffer_size];
while( (length = fis.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
} catch (IOException e) {
throw e;
} finally {
fis.close();
baos.flush();
baos.close();
}
String text = singleLine.matcher(
block.matcher(baos.toString()).replaceAll("")
).replaceAll("");
try {
fw.write(text);
} finally {
fw.flush();
fw.close();
}
}
}
to deal with resources. The plugin is binded to the life cycle: process-resources. When the war file is packaged, the comment-deleted js files may be overwritten.
package com.mycompany.mojo.jsmin;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.DirectoryScanner;
/**
* delete the js file comment
* @goal minify
* @phase process-resources
*/
public class JsCommentMojo extends AbstractMojo {