background:
动手写一个去掉js comment的Maven Plugin, 用英文写练习练习,哈哈
steps:
1. New a maven project, the pom is like below. At the beginning I do not add the maven project api to the project, it is exhausting to find the project strutcture, build path etc and filter the resources that i want. It is better to include it in my project,
<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>
2. Js comment is deleted by using regular expression. the code maybe like as below. The single line comment as "//...." without existing in string literal is hard to match. And a big js file is loaded into memory at one time and is treated as a really string by regular expression may cause java fatal error. So it is not very prefect, but it is enough to be fullfill my situation.
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();
}
}
}
3. Mojo is like as below, the annotation is required by Maven plugin to dependecy inject the parms value at runtime. It is really very convenient.
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 {
/**
* Single directory for extra files to include in the WAR.
*
* @parameter expression="${basedir}/src/main/webapp"
*/
private File warSourceDirectory;
/**
* The folder where the webapp is built.
*
* @parameter expression="${project.build.directory}/${project.build.finalName}"
*/
private File webappDirectory;
/**
* excludes file
*
* @parameter
*/
private List<String> excludes;
/**
* javascript file extension
*
* @parameter default-value=".js"
*/
private String extension;
/**
* Whether to skip execution.
*
* @parameter expression="${maven.jsmin.skip}" default-value="false"
*/
private boolean skip;
/**
* total count of processed javascript file
*
*/
private int executedCount;
public void execute() throws MojoExecutionException, MojoFailureException {
if(skip) {
getLog().info("Run of javascript files process skipped");
return;
}
getLog().info("Run of javascript files process is starting...");
DirectoryScanner directoryScanner = new DirectoryScanner();
directoryScanner.setBasedir(warSourceDirectory);
if ( excludes != null && !excludes.isEmpty() ) {
directoryScanner.setExcludes( excludes.toArray(new String[0]) );
}
directoryScanner.setIncludes(new String[]{"**/*" + extension});
directoryScanner.addDefaultExcludes();
directoryScanner.scan();
for(String relativePath : directoryScanner.getIncludedFiles() ) {
try {
processFile(
new File(warSourceDirectory, relativePath),
getTargetFile(relativePath));
} catch (IOException e) {
getLog().error(warSourceDirectory + relativePath + " failed to be processed");
getLog().error(e);
throw new MojoExecutionException(e.getMessage());
}
}
getLog().info("Total " + executedCount + " javascript files processed");
}
private File getTargetFile(String relativePath) throws MojoExecutionException {
File file = new File(webappDirectory.getPath() + File.separator +
relativePath);
file.getParentFile().mkdirs();
if(getLog().isDebugEnabled()) {
getLog().debug("src file [ " +
relativePath + " ] ==>> target [ " +
file.getPath() + " ]");
}
return file;
}
private void processFile(File inputFile, File outputFile) throws IOException {
JsFileProcessor.process(inputFile, outputFile);
executedCount++;
getLog().info("process file : " + inputFile.getPath() );
}
}
作为记录, 以资参考
分享到:
相关推荐
Maven 插件是 Maven 构建系统中的一个重要组成部分,它极大地扩展了 Maven 的功能,使其能够处理各种特定的任务,如编译、测试、打包、部署等。在 Maven 中,插件是通过 Maven 插件管理系统(scm)进行管理和使用的...
Maven Plugin则是Maven中的一个重要组成部分,用于执行特定的任务,如编译、测试、打包、部署等。本篇文章将深入探讨Maven Plugin及其打包下载的相关知识点。 一、Maven Plugin简介 Maven Plugin是Maven中的插件,...
exec-maven-plugin是Maven生态系统中的一个插件,它允许用户在Maven构建过程中执行外部命令或脚本。这使得Maven项目可以集成更多的自定义操作,比如运行特定的脚本、调用系统命令等。本文将详细介绍exec-maven-...
Tomcat 是一个流行的 Web 服务器,Maven 提供了一个插件 tomcat8-maven-plugin 来实现 Maven 项目与 Tomcat 服务器的集成。下面我们将详细介绍如何使用 tomcat8-maven-plugin 插件。 tomcat8-maven-plugin 插件的...
maven-tomcat-plugin让maven与tomcat配合得很好。它可以把应用部署到Tomcat服务器,也可以把tomcat作为内嵌服务器启动,就像jetty一样。 使用JPDA启动tomcat的远程调试功能。这样就能与eclipse配合起来,轻松地实现...
标题“maven plugin”指的是Maven中的插件系统,这是一个关键功能,允许开发者扩展Maven的功能以满足特定的项目需求。Maven插件通常由一组目标组成,每个目标执行一个特定的任务。例如,`maven-compiler-plugin`用于...
总之,`yuicompressor-maven-plugin`为Maven项目提供了一个便捷的方式来进行前端资源的优化,提升了Web应用的加载速度和用户体验。通过合理的配置和使用,开发者可以有效地管理项目中的JavaScript和CSS文件,同时...
当我们谈论`tomcat7-maven-plugin-2.2.jar`时,这实际上是Maven的一个插件,它允许开发者直接在Maven环境中集成和管理Tomcat服务器,从而实现快速部署和调试。 这个"修改版"的`tomcat7-maven-plugin-2.2.jar`可能...
dockerfile-maven-plugin 支持 maven 直接发布项目至 docker 镜像库
本项目是基于Java的smart-doc-maven-plugin官方maven插件设计源码,包含43个文件,其中包括27个Java...该项目是一个smart-doc官方maven插件,通过Java编程语言实现,旨在为用户提供一个高效、便捷的maven插件解决方案。
Maven Jetty Plugin是一款强大的工具,它将Jetty服务器集成到了Maven的构建流程中。这个插件允许开发者在开发过程中快速、便捷地运行和测试Java Web应用程序,而无需进行完整的部署过程。它的主要功能包括: 1. **...
**eclipse-maven3-plugin** 是一个专门为 Eclipse IDE 设计的插件,它允许开发者在 Eclipse 开发环境中无缝集成 Maven 构建工具。Maven 是一个项目管理和综合工具,广泛用于 Java 应用程序的构建、依赖管理和项目...
Apache Tomcat Maven Plugin - About Apache Tomcat Maven Plugin
yuicompressor-maven-plugin, 用于压缩 (Minify/Ofuscate/Aggregate) Javascript文件和使用 YUI 压缩器的CSS文件的Maven 插件 [[Flattr this git repo] ( http://api.flattr.com/button/flattr-badge-large.png)]...
`maven-jetty-plugin`是Maven的一个插件,它的主要作用是在Maven构建过程中,帮助我们运行和测试基于Servlet的Web应用程序。它允许开发者在开发环境中快速启动Jetty服务器,自动部署项目,并且能够随着代码的修改...
maven-plugin-api-3.0.jar
载links目录下创建一个maven.txt(名字可以自定义)。在文件中添加:path=D:\eclipse\myplugins\maven,保存并关闭maven.txt,并将后缀改为maven.link,重启eclipse,打开windows-->Preferences,查看死否安装成功
maven的scala编译包
解决tomcat8-maven-plugin-3.0-r1655215.jar阿里云同有的问题。放到路径org\apache\tomcat\maven\tomcat8-maven-plugin\3.0-r1655215\就可以了
1. 在你的 eclipse 安装的根目录下创建两个文件夹:links,plugins(名字可以随便取),我的这两个文件夹位于:path=D:/Development/eclipse-JavaEE/... Preferences 会发现一个多了一个选项Maven,说明安装成功了。