17

几个小时以来,我一直在努力让这个插件与 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 (我假设)覆盖构建战争文件。我真的需要文件名保持不变......有没有人知道我需要更改什么才能使用相同的文件名并且仍然将缩小版本纳入最终战争?

4

7 回答 7

26

好的。我终于想通了。您需要<webappDirectory>在 yuicompressor 插件中定义 a ,然后可以<resource>在 maven-war-plugin 中将其引用为 a 。在下面的示例中,我正在使用<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>
于 2012-07-15T19:40:50.237 回答
8

只需在 WAR 插件上配置“warSourceExcludes”。

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
    </configuration>
</plugin>
于 2014-03-26T04:42:24.200 回答
4

我想添加对我有用的配置:

首先,为了修复 m2e 抱怨“生命周期未涵盖的插件执行”,我在从这篇文章中获取的父 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 文件,同时排除原始文件。

我希望这可以节省一些时间。

于 2014-07-03T15:27:53.163 回答
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>
于 2012-12-26T07:51:39.350 回答
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>
于 2016-04-12T16:53:31.200 回答
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>

于 2016-04-22T02:30:44.790 回答
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

于 2019-12-30T05:05:22.627 回答