2012-08-06 86 views
2

我在使用maven編譯的struts2項目上工作。我正在嘗試縮小位於不同位置的JavaScript文件。YUI壓縮器的使用Maven Mojo縮小javascript

<plugin> 
    <groupId>net.alchim31.maven</groupId> 
    <artifactId>yuicompressor-maven-plugin</artifactId> 
    <version>1.3.0</version> 
    <executions> 
     <execution> 
     <goals> 
      <goal>compress</goal> 
     </goals> 
     </execution> 
    </executions>   
    <configuration> 
     <nosuffix>true</nosuffix> 
    </configuration> 
    </plugin> 

我認爲通過這樣做,所有的JS文件將(根據定義nosuffix)進行微細化以及更換生產WAR文件的原始文件。

但似乎並非如此。我怎樣才能實現這個目標?其次,如果我選擇使用帶後綴的那一個,我假設我必須手動更改腳本引用,這在我的jsp文件中是正確的嗎?如果是這樣,我該如何設置,以便它會刪除沒有後綴的原件?

謝謝。

回答

3

我發現工作解決方案here。基本上你需要將以下到您的pom.xml(只更換路徑,你的js和css文件裏面的src/main/webapp的文件夾):

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-war-plugin</artifactId> 
      <configuration> 
       <webResources> 
        <!-- Add minified resources --> 
        <resource> 
         <directory>${project.build.directory}/minimized</directory> 
         <targetPath>/</targetPath> 
         <filtering>false</filtering> 
        </resource> 
       </webResources> 
      </configuration> 
     </plugin> 

     <plugin> 
      <groupId>net.alchim31.maven</groupId> 
      <artifactId>yuicompressor-maven-plugin</artifactId> 
      <executions> 
       <execution> 
        <goals> 
         <goal>compress</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <nosuffix>true</nosuffix> 
      </configuration> 
     </plugin> 
    </plugins> 

    <pluginManagement> 
     <plugins> 
      <!-- Javascript and CSS files compression --> 
      <plugin> 
       <groupId>net.alchim31.maven</groupId> 
       <artifactId>yuicompressor-maven-plugin</artifactId> 
       <version>1.1</version> 
       <configuration> 
        <!-- Don't output in the default webapp location, since the war plugin will overwrite the files in there 
        with the original, uncompressed ones. --> 
        <webappDirectory>${project.build.directory}/minimized</webappDirectory> 
        <jswarn>false</jswarn> 
        <!-- Overwrite existing files --> 
        <nosuffix>true</nosuffix> 
        <includes> 
         <include>%path to your js and css files inside src/main/webapp%/**/*</include> 
        </includes> 
       </configuration> 
      </plugin> 
     </plugins> 
    </pluginManagement> 
    </build> 
+0

+1的鏈接,但唯一你忘了包括這裏是: $ {project.build.directory}/minimized在yiucompressor的配置塊 – davs 2013-08-11 14:35:17

+0

你建議實際上無法工作 - 它會漏掉所有靜態資源,模板和web.xml來源src/main/web應用 – 2013-08-12 20:21:54