yuicompressor maven插件和maven-war-plugin

时间:2012-07-15 19:29:53

标签: maven plugins yui-compressor sonatype

我一直在努力让这个插件与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文件时。我真的需要文件名保持不变但是...有没有人知道我需要改变什么才能使用相同的文件名并仍然将缩小版本带入最后的战争?

7 个答案:

答案 0 :(得分:26)

行。我终于想通了。您需要在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>

答案 1 :(得分:8)

只需在WAR插件上配置'warSourceExcludes'。

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
    </configuration>
</plugin>

答案 2 :(得分:3)

我想添加适合我的配置:

首先,修复m2e抱怨生命周期未涵盖的&#39;插件执行&#39;我在从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文件,同时排除原始文件。

我希望这能节省时间。

答案 3 :(得分:2)

这是我的配置,它在我的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>

答案 4 :(得分:0)

我使用的方法有点不同。

首先,我已将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>

答案 5 :(得分:0)

正如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>

答案 6 :(得分:0)

没有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