2011-12-02 68 views
0

我想爲maven使用YUI Compressor插件來壓縮我的CSS和JavaScript,但我遇到了兩個問題。YUI Compressor Maven插件在錯誤的時間執行和tomcat插件不使用yui壓縮器輸出

  1. 我的配置適當壓縮和聚合JavaScript文件,但如果我跑mvn package做出war文件,Maven將壓縮JavaScript文件之前,它的副本在src/main/webapp文件夾。這將覆蓋所有壓縮的CSS和JavaScript文件。我怎樣才能解決這個問題?

  2. 如何讓tomcat maven插件使用壓縮的JavaScript文件而不是我的src/main/webapp/scripts文件夾中的文件?當我的應用程序試圖讀取all.js時,它失敗,因爲它位於target/目錄中,而不在我的src/main/webapps文件夾中。

    <!-- Tomcat --> 
        <plugin> 
         <groupId>org.codehaus.mojo</groupId> 
         <artifactId>tomcat-maven-plugin</artifactId> 
         <version>1.1</version> 
         <configuration> 
          <warFile>target/myapp-1.0.war</warFile> 
         </configuration> 
        </plugin> 
    
        <!-- YUI Compressor --> 
        <plugin> 
         <groupId>net.alchim31.maven</groupId> 
         <artifactId>yuicompressor-maven-plugin</artifactId> 
         <executions> 
          <execution> 
           <goals> 
            <goal>compress</goal> 
           </goals> 
          </execution> 
         </executions> 
         <configuration> 
          <excludes> 
           <exclude>**/jwplayer.js</exclude> 
           <exclude>**/audio-player-noswfobject.js</exclude> 
           <exclude>**/audio-player-uncompressed.js</exclude> 
           <exclude>**/audio-player.js</exclude> 
           <exclude>**/jscharts.js</exclude> 
           <exclude>**/jquery-ui-1.8.16.custom.min.js</exclude> 
          </excludes> 
          <nosuffix>true</nosuffix> 
          <jswarn>false</jswarn> 
          <force>false</force> 
          <aggregations> 
           <aggregation> 
            <removeIncluded>false</removeIncluded> 
            <insertNewLine>true</insertNewLine> 
            <output>${project.build.directory}/${project.build.finalName}/scripts/all.js</output> 
            <includes> 
             <include>**/json/json2.js</include> 
             <include>**/jwplayer/jwplayer.js</include> 
             <include>**/font/font.js</include> 
             <include>**/underscore/underscore.js</include> 
             <include>**/jquery/jquery-1.7.1.js</include> 
             <include>**/jquery/jquery-ui-1.8.16.custom.min.js</include> 
             <include>**/jquery/jquery.cookie.js</include> 
             <include>**/jquery/jquery.fancybox.js</include> 
             <include>**/jquery/jquery.highlight.js</include> 
             <include>**/jquery/jquery.jcrop.js</include> 
             <include>**/jquery/jquery.tmpl.js</include> 
             <include>**/jquery/farbtastic.js</include> 
             <include>**/jscharts/jscharts.js</include> 
    
             <include>**/myapp/homepage.js</include> 
            </includes> 
           </aggregation> 
          </aggregations> 
         </configuration> 
        </plugin> 
    

或者,我只是要我的目標走錯了路?

這裏是打包戰爭時的maven輸出。你可以看到,即使我告訴yui:compress目標,在預包裝階段,仍然是資源的複製之前發生,因爲這發生在package階段:

[INFO] ------------------------------------------------------------------------ 
[INFO] Building My App 1.0 
[INFO] ------------------------------------------------------------------------ 
Downloading: http://repo1.maven.org/maven2/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml 
Downloading: http://snapshots.repository.codehaus.org/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml 
Downloading: http://download.java.net/maven/2/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml 
Downloading: http://oss.sonatype.org/content/groups/public/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml 
Downloaded: http://oss.sonatype.org/content/groups/public/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml (442 B at 2.1 KB/sec) 
Downloaded: http://repo1.maven.org/maven2/net/alchim31/maven/yuicompressor-maven-plugin/maven-metadata.xml (403 B at 0.9 KB/sec) 
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ myapp --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 26 resources 
[INFO] Copying 4 resources 
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ myapp --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default) @ myapp --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] 
[INFO] --- aspectj-maven-plugin:1.3:compile (default) @ myapp --- 
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ myapp --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 11 resources 
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ myapp --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] 
[INFO] --- aspectj-maven-plugin:1.3:test-compile (default) @ myapp --- 
[INFO] No modifications found skipping aspectJ compile 
[INFO] 
[INFO] --- maven-surefire-plugin:2.6:test (default-test) @ myapp --- 
[INFO] Tests are skipped. 
[INFO] 
[INFO] --- yuicompressor-maven-plugin:1.2:compress (default) @ myapp --- 
[INFO] 960.css (9989b) -> 960.css (5897b)[59%] 
[INFO] base.css (24210b) -> base.css (16437b)[67%] 
[INFO] reset.css (2062b) -> reset.css (1096b)[53%] 
[INFO] text.css (1270b) -> text.css (598b)[47%] 
[INFO] tinymce.css (1994b) -> tinymce.css (1277b)[64%] 
[INFO] jquery.fancybox-1.3.4.css (8852b) -> jquery.fancybox-1.3.4.css (6975b)[78%] 
[INFO] farbtastic.css (1362b) -> farbtastic.css (478b)[35%] 
[INFO] jquery.jcrop.css (748b) -> jquery.jcrop.css (582b)[77%] 
[INFO] base.css (34567b) -> base.css (25034b)[72%] 
[INFO] jquery-ui-1.8.11.custom.css (33994b) -> jquery-ui-1.8.11.custom.css (25351b)[74%] 
.... (tons of javascript files) 
[INFO] why.js (515b) -> why.js (354b)[68%] 
[INFO] underscore.js (26960b) -> underscore.js (9472b)[35%] 
[INFO] total input (1832512b) -> output (1198425b)[65%] 
[INFO] generate aggregation : C:\Users\egervari\IdeaProjects\myapp-development\target\myapp-1.0\scripts\all.js 
[INFO] all.js (564342b) 
[INFO] nb warnings: 0, nb errors: 0 
[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ myapp --- 
[INFO] Packaging webapp 
[INFO] Assembling webapp [myapp] in [C:\Users\egervari\IdeaProjects\myapp-development\target\myapp-1.0] 
[INFO] Processing war project 
[INFO] Copying webapp resources [C:\Users\egervari\IdeaProjects\myapp-development\src\main\webapp] 

即使改變階段所做的工作,那麼我也必須在tomcat:run之前得到這個壓縮文件。

是行家只爲你認爲錯誤的工作工具?或者是maven/java只是沒有達到與重JavaScript開發鼻菸?爲什麼這麼難?

+0

嘗試將yui壓縮機置於其他階段(例如準備包裝) – khmarbaise

+0

@khmarbaise我試過這個。問題在於這個階段仍然發生在包裝階段之前。複製發生在軟件包階段內部。我很茫然。我即將完全放棄這種方法。我認爲這種壓縮/聚合可能必須在服務器上傳後,我上傳的戰爭,其中......說實話...糟透了:(我也不會真的有一個方便的方式來測試我的生產JavaScript通過tomcat在我的開發機器之前,我只是運行tomcat:運行,就是這樣,我真的不想讓我的開發環境複雜化 – egervari

+0

@khmarbaise看來maven並不是這個意思,也不是春天。大多數現代Web框架現在都有這個特性,但是Java真的處於石器時代,這太糟糕了,這是一個大型的現有項目,我必須得到一些工作,我想我可以將生產JavaScript輸出回我的'src/main/webapp/script'目錄下,這是我唯一能想到的東西,在我的源代碼目錄下的每個文件的普通版本和.min版本中生活會很糟糕嗎?這看起來不太乾淨:/ – egervari

回答

3

我遇到類似的問題,我改變了我的階段包。希望這會幫助其他人。

這是棘手。這與nosuffix配置選項有關。如果您刪除nosuffix選項,縮小按預期工作。

如果你真的不得不nosuffix那麼你需要執行階段更改爲「包」。

<executions> 
     <execution> 
     <phase>package</phase> 
      <goals> 
    ...... 
    ..... 

任何階段之前準備資源不起作用,因爲戰爭是建立時,它從原來的源位置拿起JS,從而覆蓋縮小的JS(創建過程中準備資源相)在目標目錄。

縮小在刪除nosuffix時起作用,因爲在包階段,文件名稱不同,因此不會覆蓋文件,您將在目標目錄中看到縮小和非縮小的js文件。

0

要解決此問題,您可以事端等。

(1)放入的src/main/JS你的個人編碼的JavaScript沒有的src/main/webapp的。對JS放置使用mvn約定。 *注意:對於包含的圖書館,例如jQuery或其他任何東西,你可以將它們轉儲到src/main/webapp中,這很好。那些已經最小化了JavaScript,你永遠不會修改。(2)調整你的YUI插件(a)忽略你沒有編碼的javascript,(b)編譯壓縮的javascript到一個輸出文件夾。

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

       <excludes> 
        <exclude>**/ui-framework/**</exclude> 
       </excludes> 
       <outputDirectory>${yui.outputdir}</outputDirectory> 
      </configuration> 
     </plugin> 

所以用這個,我們可以做以下練習。

mvn process-resources 


└── target 
├── classes 
│   ├── default.logback.xml 
│   └── props 
│    ├── default.props 
│    └── production.default.props 
├── packageLinkDefs.properties 
└── yui-compressed 
    └── jasmineDummyExample_element_mover.js 

這裏的關鍵是,YUI正在編譯壓縮的JavaScript到自己的輸出文件夾,戰爭插件不知道它。 (3)最後你只需要調整你的war插件來複制yui編譯的源代碼。

只要你遵循JavaScript規範的maven,你的javascript應該在src/main/js /下編碼,默認情況下你的war插件不會複製你的javascipt(聽起來很奇怪吧?)。

默認情況下,你的war插件將複製src/main/webapp下的任何靜態資源。 這很方便,因爲它允許您指示war插件保持將src/main/webapp複製到最終war文件的默認行爲,此外還可以複製/ src/target/yui-compressed文件夾。以這種方式,兩個文件夾中都沒有重疊文件,輸出目錄中沒有文件覆蓋。

這下面的代碼片段所示:

$ mvn war:war 
[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building lift-someproject Project 1.0 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-war-plugin:2.5:war (default-cli) @ lift-someproject --- 
[INFO] Packaging webapp 
[INFO] Assembling webapp [lift-someproject] in [D:\scalaWorkspace\liftsomeprojectTwo\lift-someproject\target\lift-someproject] 
[INFO] Processing war project 
[INFO] Copying webapp webResources [D:\scalaWorkspace\liftsomeprojectTwo\lift-someproject\target\yui-compressed] to [D:\scalaWorkspace\liftsomeprojectTwo\lift-someproject\target\lift-someproject] 
[INFO] Copying webapp resources [D:\scalaWorkspace\liftsomeprojectTwo\lift-someproject\src\main\webapp] 
[INFO] Webapp assembled in [820 msecs] 
[INFO] Building war: D:\scalaWorkspace\liftsomeprojectTwo\lift-someproject\target\lift-someproject.war 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 12.968s 
[INFO] Finished at: Tue Dec 23 20:17:59 CET 2014 
[INFO] Final Memory: 11M/246M 
[INFO] ------------------------------------------------------------------------ 

你可以調整你的WAR文件要知道你不壓碎衣壓縮JS文件的特殊文件夾:

<plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>2.5</version> 
      <configuration> 
       <webResources> 
        <resource> 
         <directory>${yui.outputdir}</directory> 
         <targetPath>js</targetPath> 
        </resource> 
       </webResources> 
      </configuration> 
     </plugin> 

如果您使用Jetty進行快速開發,您需要進一步研究該插件,以確保它在src/main/js中找到您的手工編碼javascript。

這應該工作。