2012-04-19 38 views
3

手指交叉可以幫助我!在構建WAR之前在Maven中重命名生成的文件

我正在使用SmartSprites將我的着陸頁上的PNG合併爲一個,以便加載速度更快。

SmartSprite將檢查您的CSS文件,生成CSS Sprite圖像,並創建一個新的CSS文件,該文件將使用此精靈圖像而不是原件。我想在我的maven WAR版本中自動替換原始CSS文件和SmartSprite。

因此,這裏是我想發生什麼:

  1. SmartSprite掃描我的CSS文件:mystyle.css
  2. SmartSprite創建一個精靈的形象,並創建一個新myStyle的-sprite.css文件,它引用了新的精靈圖像。
  3. 我要複製myStyle的-sprite.css戰爭內置超過mystyle.css之前,讓我沒有改變我的任何JSP文件中引用的。

這兩個文件都位於輸出目錄(target/myproj/css)中。在SmartSprite中似乎沒有任何標誌覆蓋原始文件,所以我想它必須在後處理完成。

下面是我用於SmartSprite的maven插件配置。

 <plugin> 
      <groupId>org.carrot2.labs</groupId> 
      <artifactId>smartsprites-maven-plugin</artifactId> 
      <version>1.0</version> 
      <executions> 
       <execution> 
        <phase>prepare-package</phase> 
        <goals> 
         <goal>spritify</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

回答

2

可以使用Maven WAR plugin

<plugin> 
    <artifactId>maven-war-plugin</artifactId> 
    <configuration> 
     <webResources> 
      <resource> 
       <directory><!-- Your output directory --></directory> 
       <targetPath><!-- The target location of the files below --></targetPath> 
       <includes> 
        <include><!-- file pattern --></include> 
        <include><!-- file pattern --></include> 
       </includes> 
      </resource> 
     </webResources> 
    </configuration> 
</plugin> 

您還應該配置SmartSprites使用不同的輸出目錄,以保留原始文件名的CSS。嘗試使用空值css-file-suffixoutput-dir-path選項。

+0

嗨阿泰,我將如何重新命名myStyle的-sprite.css文件爲myStyle的.css使用資源?我認爲只能包含或排除文件,但在傳輸過程中不能更改其名稱。 – 2012-04-19 14:54:20

+0

@JohnFarrelly更新了我的答案。嘗試使用不同的輸出目錄(與WAR的默認暫存區域不同)。 – 2012-04-19 14:56:01

+0

而且,哦,是的,這將工作。我使用這種方法稍微不同的方案(RequireJS優化JS + CSS文件)。 – 2012-04-19 14:58:56

18

恐怕你不會找到任何簡單的或比Maven的AntRun插件更多地飄逸着這樣的事:

<build> 
    <plugins> 
    <plugin> 
     <artifactId>maven-antrun-plugin</artifactId> 
     <version>1.7</version> 
     <executions> 
     <execution> 
      <phase>prepare-package</phase> 
      <configuration> 
      <target> 
       <copy file="${project.build.directory}/mystyle-sprite.css" 
       tofile="${project.build.directory}/mystyle.css" /> 
      </target> 
      </configuration> 
      <goals> 
      <goal>run</goal> 
      </goals> 
     </execution> 
     </executions> 
    </plugin> 
    </plugins> 
</build> 
+1

也許你應該移動文件而不是複製。您不希望在最終的WAR中留下構建工件。 – 2012-04-19 15:01:39

+0

沒問題。將''任務替換爲'<移動文件=「file.orig」tofile =「file.moved」/>'。問題是關於複製。 – 2012-04-19 15:04:05

+0

關於重命名文件而不使用程序集,這應該是被接受的答案 - 只保存我的一天:-) – Gunnar 2015-12-16 14:05:40

相關問題