2012-07-15 93 views
17

我一直在努力获得这个插件与maven-war-plugin很好地玩几个小时,我认为是时候寻求帮助。我有插件定义如下:yuicompressor maven插件和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(我假设)在构建战争文件时。我真的需要文件名保持不变,尽管...有人有一个想法,我需要改变,以使用相同的文件名,仍然得到缩小版本进入最后的战争?

回答

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> 
+2

谢谢,谢谢,谢谢!我有同样的问题,直到现在还找不到明智的解决方案。 YUI插件词汇非常混乱imo:/ – Rolf 2013-02-07 00:49:40

+0

同样在这里:)非常感谢! – 2014-01-13 16:55:07

+0

不适用于我。战争插件将首先复制网络资源(缩小文件),用战争源代码目录的内容覆盖。 – 2014-07-08 13:34:19

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> 
8

只是配置上的WAR插件 'warSourceExcludes'。

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

简单的解决方案,工作 – 2014-07-08 13:37:08

+0

谢谢。对我的项目很好。 – Pavlo 2016-09-08 09:00:05

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> 

然后在战争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文件,同时排除原始文件。

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

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> 
0

至于雅各布克鲁泽说,你必须处理的* .js文件,但没有* .min.js,所以我的配置是下面,请注意使用正则表达式%[]的:

\t \t \t <plugin> 
 
\t \t \t  <groupId>net.alchim31.maven</groupId> 
 
\t \t \t  <artifactId>yuicompressor-maven-plugin</artifactId> 
 
\t \t \t  <version>1.4.0</version> 
 
\t \t \t  <executions> 
 
\t \t \t   <execution> 
 
\t \t \t    <id>compressyui</id> 
 
\t \t \t    <phase>process-resources</phase> 
 
\t \t \t    <goals> 
 
\t \t \t     <goal>compress</goal> 
 
\t \t \t    </goals> 
 
\t \t \t    <configuration> 
 
\t \t \t     <nosuffix>true</nosuffix> 
 
\t \t \t     <warSourceDirectory>${basedir}/WebContent</warSourceDirectory> 
 
\t \t \t     <webappDirectory>${project.build.directory}/min</webappDirectory> 
 
\t \t \t     <jswarn>false</jswarn> \t \t 
 
\t \t \t \t   <excludes> 
 
\t \t \t \t    <exclude>**/*-min.js</exclude> 
 
\t \t \t \t    <exclude>**/*.min.js</exclude> 
 
\t \t \t \t    <exclude>**/*-min.css</exclude> 
 
\t \t \t \t    <exclude>**/*.min.css</exclude> 
 

 
\t \t \t \t    <exclude>**/jquery.window.js</exclude> 
 
\t \t \t \t    ...... 
 
\t \t \t \t    <exclude>**/compile.js</exclude> 
 
\t \t \t \t   </excludes> 
 
\t \t \t    </configuration> 
 
\t \t \t   </execution> 
 
\t \t \t  </executions> 
 
\t \t \t </plugin> 
 

 
\t \t \t <plugin> 
 
\t \t \t \t <groupId>org.apache.maven.plugins</groupId> 
 
\t \t \t \t <artifactId>maven-war-plugin</artifactId> 
 
\t \t \t \t <version>2.4</version> 
 
\t \t \t \t <configuration> 
 
\t \t \t \t \t <warSourceDirectory>WebContent</warSourceDirectory> \t \t \t \t \t 
 
\t \t \t \t \t <packagingExcludes>servlet-api*.jar,target/test-classes/*</packagingExcludes> 
 
\t \t \t \t \t <warSourceExcludes>test/**,%regex[.*(!min).js],%regex[.*(!min).css]</warSourceExcludes> 
 
\t \t \t \t 
 
\t \t \t \t \t <webResources> 
 
    \t \t \t \t \t    <resource> 
 
\t \t \t \t \t     <directory>${project.build.directory}/min</directory> 
 
\t \t \t \t \t    </resource> 
 
\t \t \t \t \t </webResources> 
 
\t \t \t \t 
 
\t \t \t \t </configuration> 
 
\t \t \t </plugin>