YUI压缩插件Maven插件和Maven WAR插件

18

我已经花费了几个小时来解决这个插件与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文件按预期进入war包,但是开启此标志时,maven-war-plugin(我假设)在构建war文件时正在覆盖它们。 然而,我真的需要文件名保持不变...有人知道我需要改变什么才能使用相同的文件名并仍然将缩小版本放入最终的war包中吗?

这个问题可能有解决方案,但插件版本1.5.1存在一个bug,因为它无法针对某些简单情况进行配置。比如我们想要配置webappDirectory和sourceDirectory。如果sourceDirectory与默认设置不同,则插件会执行两次压缩,一次使用配置的值,另一次使用默认值,这就破坏了sourceDirectory配置的目的。我无法从需要在压缩之前进行更改的目录中进行压缩。 - user250343
7个回答

26

好的,我终于搞清楚了。你需要在yuicompressor插件中定义一个<webappDirectory>,然后可以将其作为<resource>在maven-war-plugin中引用。 在下面的示例中,我使用的是<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>

2
谢谢,谢谢,谢谢!我也遇到了同样的问题,直到现在都找不到合理的解决方案。我认为YUI插件词汇非常令人困惑 :/ - fbiville
对我不起作用。War插件将首先复制Web资源(缩小文件),然后覆盖War源目录的内容。 - Jakob Kruse
是的,这就是为什么你要把它们放到 /min 目录中,然后在 maven-war-plugin 中将 /min 目录定义为 <resource>,就像上面的例子一样。 - Dave Maple
你好,我使用了你的方法,但是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
听起来像是权限问题@armnotstrong——mvn的运行用户是谁,他在那个目录下有写入权限吗? - Dave Maple
嗨,@DaveMaple 这个插件将压缩src/main/webapp下的所有.js和.css文件。但在我的情况下,我的.js和.css文件位于src/main/resources/static下,那么我该如何更改默认路径到这里呢?提前致谢。 - Vibhav Chaddha

8
只需在WAR插件上配置“warSourceExcludes”即可。
<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
    </configuration>
</plugin>

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>

然后在我的战争POM中,我放置了:

<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文件,同时排除原始文件。

我希望这能为某些人节省时间。


2
这是我的配置,在我的Maven Web项目中可以正常工作:

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

0

我使用的方法有点不同。

首先,我已经配置了我的IDE在编译/打包之前运行mvn process-resources。这样,在组装war文件之前就创建了文件。

非常重要的是设置<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>

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>


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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接