2012-07-15 32 views
17

Tôi đã đấu tranh với việc sử dụng plugin này để chơi độc đáo với plugin maven-war trong một vài giờ và tôi nghĩ đã tới lúc yêu cầu trợ giúp. Tôi đã plugin quy định như sau:plugin yuicompressor maven và 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> 

Nếu tôi loại bỏ nosuffix = true sau đó tôi có thể nhìn thấy nén/minified file -min.js nhận được vào cuộc chiến như mong đợi, nhưng với lá cờ này họ đang bị ghi đè bởi maven-war-plugin (tôi giả sử) khi nó xây dựng tập tin chiến tranh. Tôi thực sự cần các tên tập tin để giữ nguyên mặc dù ... không ai có ý tưởng về những gì tôi cần phải thay đổi để sử dụng cùng tên tập tin và vẫn nhận được các phiên bản rút gọn vào cuộc chiến cuối cùng?

Trả lời

26

OK. Cuối cùng tôi đã tìm ra điều này. Bạn cần xác định <webappDirectory> trong plugin yuicompressor, sau đó có thể được tham chiếu dưới dạng <resource> trong plugin maven-war. Trong ví dụ dưới đây tôi đang sử dụng <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

Cảm ơn bạn, cảm ơn bạn, cảm ơn bạn! Tôi đã có cùng một vấn đề và không thể tìm thấy bất kỳ giải pháp hợp lý cho đến bây giờ. Từ vựng plugin YUI rất khó hiểu:/ – Rolf

+0

giống nhau ở đây :) rất biết ơn! –

+0

Không hiệu quả với tôi. Plugin chiến tranh sẽ sao chép tài nguyên web (tệp được rút gọn) trước tiên, ghi đè bằng nội dung của thư mục nguồn chiến tranh. –

2

đây là cấu hình của tôi, và nó hoạt động tốt trong dự án web maven tôi:

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

Chỉ cần cấu hình 'warSourceExcludes' trên các plugin WAR.

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

Giải pháp đơn giản hoạt động –

+0

Cảm ơn. Làm việc trên dự án của tôi tốt. – Pavlo

3

Tôi muốn thêm cấu hình mà làm việc cho tôi:

Trước tiên, để sửa chữa M2E phàn nàn về 'Plugin thực hiện không nằm trong vòng đời' Tôi được thêm vào sau trong pom mẹ lấy từ this post :

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

Sau đó, trong chiến tranh pom tôi đặt:

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

Điều này tạo ra các tệp css và js được rút gọn trong thư mục đích xây dựng dự án trong khi loại trừ các tệp gốc.

Tôi hy vọng điều này sẽ tiết kiệm thời gian của ai đó.

0

Cách tiếp cận tôi sử dụng hơi khác một chút.

Trước tiên, tôi đã định cấu hình IDE để chạy mvn process-resourcestrước khi biên soạn/đóng gói. Bằng cách này các tập tin được tạo ra trước khi chiến tranh được lắp ráp.

Đó là rất quan trọng để đặt <nosuffix>false</nosuffix><outputDirectory>${basedir}/src/main/resources/</outputDirectory> để các tệp có thể được tạo trong cùng thư mục mà không thay thế tệp nguồn gốc của bạn.

<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

Như Jakob Kruse nói, bạn phải đối phó với các * .js, nhưng không có .min.js *, vì vậy cấu hình của tôi là dưới đây, xin vui lòng nhận thấy việc sử dụng% regex []:

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