2012-07-15 90 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>