yuicompressor maven 插件和 maven-war-plugin
Posted
技术标签:
【中文标题】yuicompressor maven 插件和 maven-war-plugin【英文标题】:yuicompressor maven plugin and maven-war-plugin 【发布时间】:2012-07-14 17:56:57 【问题描述】:几个小时以来,我一直在努力让这个插件与 maven-war-plugin 很好地配合,我认为是时候寻求帮助了。我的插件定义如下:
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<id>compressyui</id>
<phase>process-resources</phase>
<goals>
<goal>compress</goal>
</goals>
<configuration>
<nosuffix>true</nosuffix>
<warSourceDirectory>$basedir/WebContent</warSourceDirectory>
<jswarn>false</jswarn>
</configuration>
</execution>
</executions>
</plugin>
如果我删除 nosuffix=true 那么我可以看到压缩/缩小的 -min.js 文件按预期进入了战争,但是有了这个标志,它们被 maven-war-plugin 覆盖(我假设) 当它构建war文件时。我真的需要文件名保持不变...有没有人知道我需要更改什么才能使用相同的文件名并仍然将缩小版本纳入最终战争?
【问题讨论】:
这种情况可能有解决办法,但是插件版本1.5.1有一个bug,因为一些简单的情况无法配置。假设我们要配置 webappDirectory 和 sourceDirectory。如果 sourceDirectory 配置与默认值不同,则插件使用配置值执行压缩两次,然后使用默认值执行压缩,这违背了 sourceDirectory 配置的目的。我无法从必须在压缩前进行更改的目录进行压缩。 【参考方案1】:好的。我终于想通了。您需要在 yuicompressor 插件中定义一个 <webappDirectory>
,然后可以在 maven-war-plugin 中将其引用为 <resource>
。在下面的示例中,我使用的是 <directory>$project.build.directory/min</directory>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<id>compressyui</id>
<phase>process-resources</phase>
<goals>
<goal>compress</goal>
</goals>
<configuration>
<nosuffix>true</nosuffix>
<warSourceDirectory>$basedir/WebContent</warSourceDirectory>
<webappDirectory>$project.build.directory/min</webappDirectory>
<jswarn>false</jswarn>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<warSourceDirectory>$basedir/WebContent</warSourceDirectory>
<encoding>UTF-8</encoding>
</configuration>
</execution>
</executions>
<configuration>
<warSourceDirectory>$basedir/WebContent</warSourceDirectory>
<encoding>UTF-8</encoding>
<webResources>
<resource>
<directory>$project.build.directory/min</directory>
</resource>
</webResources>
</configuration>
</plugin>
【讨论】:
谢谢谢谢谢谢!我有同样的问题,直到现在才找到任何明智的解决方案。 YUI 插件词汇在 imo 中非常令人困惑:/ 对我不起作用。 war插件会先复制web资源(压缩文件),覆盖war源目录的内容。 是的,这就是为什么您将它们放入 /min 目录,然后在 maven-war-plugin 中将该 /min 目录定义为mvn install
指控[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project gif-www: Execution default-war of goal org.apache.maven.plugins:maven-war-plugin:2.2:war failed: basedir /home/ximing/work/gifmiao/gif-www/target/min does not exist -> [Help 1]
听起来像是权限问题@armnotstrong -- mvn 以什么用户身份运行,它在该目录中是否具有写入权限?【参考方案2】:
只需在 WAR 插件上配置“warSourceExcludes”即可。
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
</configuration>
</plugin>
【讨论】:
【参考方案3】:我想添加对我有用的配置:
首先,为了解决 m2e 抱怨“生命周期未涵盖插件执行”的问题,我在来自 this post 的父 pom 中添加了以下内容:
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse
m2e settings only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>compress</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
然后在战争中我放了:
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
<configuration>
<linebreakpos>300</linebreakpos>
<excludes>
<exclude>**/*-min.js</exclude>
<exclude>**/*.min.js</exclude>
<exclude>**/*-min.css</exclude>
<exclude>**/*.min.css</exclude>
</excludes>
<nosuffix>true</nosuffix>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
</configuration>
</plugin>
</plugins>
</build>
这会在项目构建目标目录中生成缩小的 css 和 js 文件,同时排除原始文件。
我希望这可以节省一些时间。
【讨论】:
【参考方案4】:这是我的配置,它在我的 maven web 项目中运行良好:
<!-- js/css compress -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<excludes>
<exclude>**/*-min.js</exclude>
<exclude>**/*.min.js</exclude>
<exclude>**/*-min.css</exclude>
<exclude>**/*.min.css</exclude>
</excludes>
<jswarn>false</jswarn>
<nosuffix>true</nosuffix>
</configuration>
<executions>
<execution>
<id>compress_js_css</id>
<phase>process-resources</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- war -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<directory>$project.build.directory/$project.build.finalName/resources</directory>
<targetPath>/resources</targetPath>
<filtering>false</filtering>
</resource>
</webResources>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
【讨论】:
【参考方案5】:我使用的方法有点不同。
首先,我已将我的 IDE 配置为在编译/打包之前运行 mvn process-resources
。这样,文件是在组装战争之前创建的。
设置<nosuffix>false</nosuffix>
和<outputDirectory>$basedir/src/main/resources/</outputDirectory>
非常非常重要,这样文件就可以在同一目录中创建,而无需替换原始源文件。
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<preProcessAggregates>false</preProcessAggregates>
<excludes>
<exclude>**/*-min.js</exclude>
<exclude>**/*.min.js</exclude>
<exclude>**/*-min.css</exclude>
<exclude>**/*.min.css</exclude>
</excludes>
<jswarn>false</jswarn>
<nosuffix>false</nosuffix> <!-- VERY IMPORTANT WILL REPLACE YOUR FILES IF YOU SET nosuffix TO TRUE OR DONT SET IT AT ALL -->
<outputDirectory>$basedir/src/main/resources/</outputDirectory> <!-- by default the plugin will copy the minimized version to target directory -->
<failOnWarning>false</failOnWarning>
</configuration>
<executions>
<execution>
<id>compress_js_css</id>
<phase>process-resources</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
</plugin>
【讨论】:
【参考方案6】:正如 Jakob Kruse 所说,你必须处理 *.js,而不是 *.min.js,所以我的配置如下,请注意 %regex[] 的使用:
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>compressyui</id>
<phase>process-resources</phase>
<goals>
<goal>compress</goal>
</goals>
<configuration>
<nosuffix>true</nosuffix>
<warSourceDirectory>$basedir/WebContent</warSourceDirectory>
<webappDirectory>$project.build.directory/min</webappDirectory>
<jswarn>false</jswarn>
<excludes>
<exclude>**/*-min.js</exclude>
<exclude>**/*.min.js</exclude>
<exclude>**/*-min.css</exclude>
<exclude>**/*.min.css</exclude>
<exclude>**/jquery.window.js</exclude>
......
<exclude>**/compile.js</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<packagingExcludes>servlet-api*.jar,target/test-classes/*</packagingExcludes>
<warSourceExcludes>test/**,%regex[.*(!min).js],%regex[.*(!min).css]</warSourceExcludes>
<webResources>
<resource>
<directory>$project.build.directory/min</directory>
</resource>
</webResources>
</configuration>
</plugin>
【讨论】:
【参考方案7】:没有 pom.xml 更改
mvn net.alchim31.maven:yuicompressor-maven-plugin:compress
强制压缩每个 js 和 css 文件,如果出现警告则失败
mvn net.alchim31.maven:yuicompressor-maven-plugin:compress \
-Dmaven.yuicompressor.force=true \
-Dmaven.yuicompressor.failOnWarning=true \
更多选项: http://davidb.github.io/yuicompressor-maven-plugin/usage_compress.html
【讨论】:
以上是关于yuicompressor maven 插件和 maven-war-plugin的主要内容,如果未能解决你的问题,请参考以下文章
Maven:将生成的 tomcat-maven-plugin 资源放在哪里?