2011-06-01 70 views
1

我目前正在研究一個已經從Ant遷移到Maven的相當大的項目。實際的構建過程沒有問題(它編譯和打包源代碼的罰款)。將螞蟻資源生成目標集成到Maven版本

的問題是,我也有很多產生額外資源爲項目目標(編譯LessCSS,產生&上傳文檔,生成tld文件自定義標籤和功能等)。我不知道我應該如何處理這些任務。讓我們以構建CSS的目標爲例(其他人或多或少相似,但未連接)。它看起來像這樣(簡化):

<target name="build.css_js"> 
    <concat destfile="${webapp.dir}/scripts/main.js"> 
     <fileset dir="${webapp.dir}/scripts/src" includes="*.js"/> 
    </concat> 

    <!-- build css files from less sources --> 
    <taskdef name="lesscss" classname="com.asual.lesscss.LessEngineTask" classpathref="libraries" /> 
    <lesscss input="${webapp.dir}/syles/src/input.less" output="${webapp.dir}/styles/output.css" /> 
</target> 

pom.xml我有以下插件設置:

<plugin> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <executions> 
     <execution> 
      <phase>generate-resources</phase> 
      <configuration> 
       <tasks> 
        <echo message="Hello World from pom.xml"/> 
        <ant target="build.css_js"/> 
       </tasks> 
      </configuration> 
      <goals> 
       <goal>run</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

我使用的依賴性不再在我們的SVN倉庫(因爲它們是Maven的管理),所以我切換變量指向Maven的回購庫:

<property name="lib.dir" location="${env.HOMEPATH}/.m2/repository" /> 

這是不好的,因爲這條道路可能只是我的機器上是有效的。我不知道任何其他的方式來從Maven倉庫引用庫,我需要它們來運行ant目標。

  • 我的方法好嗎,還是有更好的做事方法?
  • 如何解決圖書館問題?
  • 包裝項目時需要一些資源,但有些則不需要。 是否存在超出編譯/包的範圍的生命週期階段? 我發現我認爲符合我需求的site生命週期。
  • 理想情況下,我應該完全放棄ant構建文件,但我不確定這是值得努力使腳本作爲maven插件運行(我目前不知道該怎麼做)。你怎麼看待這件事?

我是Maven的新手,所以有什麼建議值得讚賞。

回答

1

通常嵌入antrun調用並不理想,但是如果你沒有找到合適的插件來做你需要的東西,那麼我不會擔心它。如果處理非常簡單,那麼將它自己嵌入Maven插件實際上很容易,請參閱this example以獲取入門幫助。

如果你打算使用antrun,並且依賴jar已經被安裝到你的Maven倉庫中,你可以配置antrun插件在它的執行過程中使用這些jar作爲插件配置的依賴關係。這意味着依賴項將被解析並可供使用,但對您的項目不可見(有助於避免意外包含)。然後,爲了訪問它們在便攜方式,您可以使用:

<property name="lib.dir" location="${settings.localRepository}" /> 

或者你可以使用一些可用於暴露的Maven類路徑的antrun插件的其他屬性,例如${maven.compile.classpath}antrun documentation瞭解更多詳情。

如果您有多個針對ant的離散執行,您可以在antrun插件中單獨配置它們,併爲每個插件指定合適的id。下面的示例顯示了兩個執行,都綁定到流程資源階段。當然,你需要提供一些實際的目標。

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <executions> 
    <execution> 
     <id>build-css</id> 
     <phase>generate-resource</phase> 
     <configuration> 
     <target> 
      ... 
     </target> 
     </configuration> 
     <goals> 
     <goal>run</goal> 
     </goals> 
    </execution> 
    <execution> 
     <id>build-js</id> 
     <phase>generate-resource</phase> 
     <configuration> 
     <target> 
      ... 
     </target> 
     </configuration> 
     <goals> 
     <goal>run</goal> 
     </goals> 
    </execution> 
    </executions> 
    <dependencies> 
    <dependency> 
     <groupId>some.group.id</groupId> 
     <artifactId>artifactId</artifactId> 
     <version>1.4.1</version> 
    </dependency> 
    <dependency> 
     <groupId>another.group.id</groupId> 
     <artifactId>anotherId</artifactId> 
     <version>1.0.1</version> 
    </dependency> 
    </dependencies> 

+0

謝謝你的提示! – 2011-06-03 16:20:29