如何使用maven缩小过滤后的javascript文件?
我有一个javascript文件,用于使用Google analytics跟踪事件。我为登台和生产环境创建了不同的帐户。调用GA代码的脚本有一个我的帐户Id的占位符。已在筛选文件中指定了帐户Id。使用maven war插件中的webResources元素,我们能够成功地替换最终war文件中的属性 现在,我们还使用maven yuicompressor插件来缩小和聚合所有javascript文件。问题是,如果我将minify目标的执行附加到包阶段,那么WAR将在Javascript缩小之前创建。如果我将缩小目标附加到之前的任何内容,则直到缩小生成无效文件时,才应用过滤器 因此,我正在寻找一种在WAR插件复制过滤后的JS文件和创建WAR文件之间运行minify目标的方法。以下是my pom.xml的相关部分:如何使用maven缩小过滤后的javascript文件?,javascript,maven,filter,minify,Javascript,Maven,Filter,Minify,我有一个javascript文件,用于使用Google analytics跟踪事件。我为登台和生产环境创建了不同的帐户。调用GA代码的脚本有一个我的帐户Id的占位符。已在筛选文件中指定了帐户Id。使用maven war插件中的webResources元素,我们能够成功地替换最终war文件中的属性 现在,我们还使用maven yuicompressor插件来缩小和聚合所有javascript文件。问题是,如果我将minify目标的执行附加到包阶段,那么WAR将在Javascript缩小之前创建。如
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp/js</directory>
<targetPath>js</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<linebreakpos>-1</linebreakpos>
<nosuffix>true</nosuffix>
<force>true</force>
<aggregations>
<aggregation>
<output>${project.build.directory}/${project.build.finalName}/js/mysite-min.js</output>
<inputDir>${project.build.directory}/${project.build.finalName}/js</inputDir>
<includes>
<include>jquery-1.4.2.js</include>
<include>jquery-ui.min.js</include>
<include>ga-invoker.js</include>
</includes>
</aggregation>
</aggregations>
</configuration>
</plugin>
org.apache.maven.plugins
maven战争插件
2.1.1
src/main/webapp/js
js
真的
net.alchim31.maven
yuicompressor maven插件
1.1
压缩
包裹
-1
真的
真的
${project.build.directory}/${project.build.finalName}/js/mysite-min.js
${project.build.directory}/${project.build.finalName}/js
jquery-1.4.2.js
jquery-ui.min.js
ga-invoker.js
你能把过滤后的js文件放在src/main/resources/filtered js中吗,改用资源插件,它绑定到进程资源,然后将maven war插件的webresources配置指向目标/类/filtered js
?我投票支持的artbristol的伟大问题,伟大的anwser。这对我帮助很大。为了将来的参考,我发布了一个完整的解决方案:
- 将Javascript文件放入
src/main/resources/js
(或任何适合您需要的文件)
- 将CSS文件放入
src/main/resources/CSS
(或任何适合您需要的文件)
- 资源插件正在筛选javascript资源并将其复制到
target/classes/js/
- Yui正在target/classes/js/上提取文件,并将它们聚合到
src/main/webapp
- 您可以使用
jetty:run
测试设置。Jetty正在src/main/webapp
- war插件在打包阶段将在
src/main/webapp
- 可选将一个SCM忽略文件(如
.svnignore
或.cvsignore
文件)放入src/main/webapp
中,以从您喜爱的SCM工具中排除all.js
excludes部分是避免压缩src/main/resources和src/main/webapp中的任何内容所必需的。yui应该只拾取已筛选的文件进行聚合
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>*/*</exclude>
</excludes>
<force>true</force>
<nosuffix>true</nosuffix>
<removeIncluded>true</removeIncluded>
<aggregations>
<aggregation>
<insertNewLine>true</insertNewLine>
<output>${project.basedir}/src/main/webapp/js/all.js</output>
<includes>
<include>${project.build.directory}/classes/js/file1.js</include>
<include>${project.build.directory}/classes/js/file2.js</include>
</aggregation>
<aggregation>
<insertNewLine>true</insertNewLine>
<output>${project.basedir}/src/main/webapp/css/all.css</output>
<includes>
<include>${project.build.directory}/classes/css/file1.css</include>
<include>${project.build.directory}/classes/css/file2.css</include>
</aggregation>
</aggregations>
</configuration>
</plugin>
Maven将我的项目版本过滤到我的css文件中。我们的imageserver上的任何传入请求都会像这样进行过滤
protected void doHttpFilter ( HttpServletRequest request, HttpServletResponse response, FilterChain chain ) throws IOException, ServletException
{
String uri = request.getRequestURI();
if (isStaticRequest(uri))
{
String uriWithoutVersion = stripVersionString(uri);
String versionRequested = getVersionString(uri);
if (version.equals(versionRequested))
{
response.setHeader(CACHE_CONTROL_HEADER_NAME, CACHE_CONTROL_HEADER_VALUE);
response.setHeader(EXPIRES_HEADER_NAME, EXPIRES_HEADER_VALUE);
}
RequestDispatcher dispatcher = request.getRequestDispatcher(uriOhneVersionsnummer);
dispatcher.forward(request, response);
} else {
chain.doFilter(request, response);
return;
}
}
这样,浏览器将永远缓存每个文件。但当我上传一个新版本时,所有文件引用都有一个新的版本号,所以浏览器再次获取图像或css。
这大大减少了流量。我正在使用Maven Minify插件压缩javascript和css文件。它还将这些文件合并到一个文件中
我使用maven时间戳插件将unque后缀作为浏览器缓存解决方案添加到这些文件中。这也很好
我遇到的问题是如何用构建执行时新创建的引用替换所有js和css引用
我也在研究maven替换插件。它可以删除所有实例。它可以替换为新值。但我需要保留一个新合并的缩小js和css的实例
<plugins>
<plugin>
<groupId>com.keyboardsamurais.maven</groupId>
<artifactId>maven-timestamp-plugin</artifactId>
<version>1.0</version>
<configuration>
<propertyName>timestamp</propertyName>
<timestampPattern>ddMMyyyyHHmm</timestampPattern>
</configuration>
<executions>
<execution>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/resources/themes</outputDirectory>
<resources>
<resource>
<includes>
<include>**/*.vm</include>
</includes>
<directory>${basedir}/src/main/resources/themes</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>maven-minify-plugin</artifactId>
<version>1.3.5</version>
<executions>
<execution>
<id>default-minify</id>
<phase>process-resources</phase>
<configuration>
<cssSourceDir>../resources/themes/XXXX/default/template-resources/stylesheet</cssSourceDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssFinalFile>style.css</cssFinalFile>
<jsSourceDir>../resources/themes/XXXX/default/template-resources/js</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>*.js</jsSourceInclude>
</jsSourceIncludes>
<jsSourceFiles>
<jsSourceFile>/libs/json.js</jsSourceFile>
</jsSourceFiles>
<jsFinalFile>script.js</jsFinalFile>
<suffix>-${timestamp}</suffix>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>target/resources/themes/XXXX/default/templates/velocity/**/*.vm</include>
</includes>
<replacements>
<replacement>
<token><![CDATA[<link rel="stylesheet" href="/storefront/template-resources/stylesheet/]]>([a-zA-Z0-9\-\_]*.css)<![CDATA[" type="text/css"/>]]>([])</token>
<value></value>
</replacement>
<replacement>
<token><![CDATA[<link rel="stylesheet" href="/storefront/template-resources/stylesheet/powerreviews]]>([a-zA-Z0-9\-\_]*.css)<![CDATA[" type="text/css"/>]]></token>
<value></value>
</replacement>
<replacement>
<token><![CDATA[<script type="text/javascript" src="/storefront/template-resources/js/]]>([a-zA-Z0-9\-\_\.]*.js)<![CDATA["></script>]]></token>
<value></value>
</replacement>
<replacement>
<token><![CDATA[<script type="text/javascript" src="/storefront/template-resources/js/libs/]]>([a-zA-Z0-9\-\_\.]*.js)<![CDATA["></script>]]></token>
<value></value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>dist</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
com.keyboardsamurais.maven
maven时间戳插件
1
时间戳
ddmmyyyyhmm
创造
maven资源插件
2.5
复制资源
验证
复制资源
${basedir}/target/resources/themes
**/*.vm
${basedir}/src/main/resources/themes
真的
com.samaxes.maven
maven缩小插件
1.3.5
默认缩小
过程资源
../resources/themes/XXXX/default/template resources/stylesheet
**/*.css
样式表
../resources/themes/XXXX/default/template resources/js
*.js
/libs/json.js
script.js
-${timestamp}
缩小
com.google.code.maven-replacer-plugin
替代者
1.5.0
准备包装
代替
target/resources/themes/XXXX/default/templates/velocity/***.vm
]]>([])
]]>
]]>
]]>
org.apache.maven.plugins
maven汇编插件
距离
单一的
包裹
src/main/assembly
<plugins>
<plugin>
<groupId>com.keyboardsamurais.maven</groupId>
<artifactId>maven-timestamp-plugin</artifactId>
<version>1.0</version>
<configuration>
<propertyName>timestamp</propertyName>
<timestampPattern>ddMMyyyyHHmm</timestampPattern>
</configuration>
<executions>
<execution>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/resources/themes</outputDirectory>
<resources>
<resource>
<includes>
<include>**/*.vm</include>
</includes>
<directory>${basedir}/src/main/resources/themes</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>maven-minify-plugin</artifactId>
<version>1.3.5</version>
<executions>
<execution>
<id>default-minify</id>
<phase>process-resources</phase>
<configuration>
<cssSourceDir>../resources/themes/XXXX/default/template-resources/stylesheet</cssSourceDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssFinalFile>style.css</cssFinalFile>
<jsSourceDir>../resources/themes/XXXX/default/template-resources/js</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>*.js</jsSourceInclude>
</jsSourceIncludes>
<jsSourceFiles>
<jsSourceFile>/libs/json.js</jsSourceFile>
</jsSourceFiles>
<jsFinalFile>script.js</jsFinalFile>
<suffix>-${timestamp}</suffix>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>target/resources/themes/XXXX/default/templates/velocity/**/*.vm</include>
</includes>
<replacements>
<replacement>
<token><![CDATA[<link rel="stylesheet" href="/storefront/template-resources/stylesheet/]]>([a-zA-Z0-9\-\_]*.css)<![CDATA[" type="text/css"/>]]>([])</token>
<value></value>
</replacement>
<replacement>
<token><![CDATA[<link rel="stylesheet" href="/storefront/template-resources/stylesheet/powerreviews]]>([a-zA-Z0-9\-\_]*.css)<![CDATA[" type="text/css"/>]]></token>
<value></value>
</replacement>
<replacement>
<token><![CDATA[<script type="text/javascript" src="/storefront/template-resources/js/]]>([a-zA-Z0-9\-\_\.]*.js)<![CDATA["></script>]]></token>
<value></value>
</replacement>
<replacement>
<token><![CDATA[<script type="text/javascript" src="/storefront/template-resources/js/libs/]]>([a-zA-Z0-9\-\_\.]*.js)<![CDATA["></script>]]></token>
<value></value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>dist</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>