2012-07-04 48 views
0

我有一個使用gmaven腳本做一些東西父POM:Maven的POM:如何堅持財產不被覆蓋

<plugin> 
     <groupId>org.codehaus.gmaven</groupId> 
     <artifactId>gmaven-plugin</artifactId> 
     <version>1.4</version> 

     <configuration combine.children="override"> 
     <providerSelection>2.0</providerSelection> 
     <scriptPath>${basedir}/build/groovy</scriptPath> 
     </configuration> 
     <executions> 
     <execution> 
      <id>groovy-properties-script</id> 
      <phase>validate</phase> 
      <goals> 
      <goal>execute</goal> 
      </goals> 
      <configuration> 
      <source>computeProperties.groovy</source> 
      </configuration> 
     </execution> 
      <!-- ... --> 

所有的孩子都應該運行該腳本,但它們嘗試根據他們的OWN basedir來解析腳本路徑。通常情況下,這正是你想要的屬性,但在這裏它不起作用,我無法解決任何問題。解決此

+0

爲什麼不硬編碼路徑?這些東西在構建過程中不會經常變化。 –

回答

0

一種方法似乎是使用properties-maven-pluginset-system-properties目標,正是如此:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>properties-maven-plugin</artifactId> 
    <version>1.0-alpha-2</version> 
    <inherited>false</inherited> 
    <executions> 
     <execution> 
     <goals> 
      <goal>set-system-properties</goal> 
     </goals> 
     <configuration> 
      <properties> 
      <property> 
       <name>com.binu.core.parent.basedir</name> 
       <value>${basedir}</value> 
      </property> 
      </properties> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 

因爲它是不能繼承,這個插件將只在父POM運行。這看起來很詭異,但有沒有一種本土的maven方式來實現這一點?

+0

更新::這不起作用。即使set-system-properties已經添加了屬性,maven也不會看到它。看來,maven(3.0.2)必須在啓動時讀取系統屬性,並在今後忽略它們。 如果我改用 評估(新文件(System.getProperty( 「com.binu.core.parent.basedir」)+ 「/build/groovy/computeProperties.groovy」)) 爲源動力gmaven插件,它的工作原理。但是,這更加黑客。 幫助! –

0

做這些事情的最好方法是把你的groovy腳本放在它自己的jar文件中,然後maven-dependency-plugin爲你解開它。然後你的腳本可以指向解壓後的腳本。

喜歡這個例子:

directory layout

+- pom.xml 
    +- script 
    | +- pom.xml 
    | +- src 
    | +- main 
    |  +- resources 
    |  +-computeProperties.groovy 
    +- code 
    +- pom.xml 
    +- src 
     +- main 
     +- java 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.stackoverflow</groupId> 
    <artifactId>Q11321971</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>pom</packaging> 

    <name>${project.artifactId}-${project.version}</name> 

    <properties> 
    </properties> 

    <modules> 
     <module>script</module> 
     <module>code</module> 
    </modules> 

    <dependencyManagement> 
     <dependencies> 
      <!-- Inter-Module dependencies --> 
      <dependency> 
       <groupId>com.stackoverflow</groupId> 
       <artifactId>Q11321971-script</artifactId> 
       <version>${project.version}</version> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 

    <build> 
     <pluginManagement> 
      <plugins> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-dependency-plugin</artifactId> 
        <version>2.4</version> 
        <executions> 
         <execution> 
          <id>unpack</id> 
          <phase>validate</phase> 
          <goals> 
           <goal>unpack</goal> 
          </goals> 
          <configuration> 
           <artifactItems> 
            <artifactItem> 
             <groupId>com.stackoverflow</groupId> 
             <artifactId>Q11321971-script</artifactId> 
             <overWrite>false</overWrite> 
             <outputDirectory>${project.build.directory}/build/groovy</outputDirectory> 
             <includes>**/*.groovy</includes> 
            </artifactItem> 
           </artifactItems> 
          </configuration> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </pluginManagement> 
    </build> 
</project> 

script/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <parent> 
     <groupId>com.stackoverflow</groupId> 
     <artifactId>Q11321971</artifactId> 
     <version>1.0-SNAPSHOT</version> 
    </parent> 

    <artifactId>Q11321971-script</artifactId> 

    <name>${project.artifactId}-${project.version}</name> 

</project> 

code/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <parent> 
     <groupId>com.stackoverflow</groupId> 
     <artifactId>Q11321971</artifactId> 
     <version>1.0-SNAPSHOT</version> 
    </parent> 

    <artifactId>Q11321971-code</artifactId> 

    <name>${project.artifactId}-${project.version}</name> 

    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-dependency-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

如果運行此之上POM,你會發現,在target目錄code模塊現在有build/groovy/computeProperties.groovy,你可以參考你的插件。

這將從所有模塊的工作,即使父POM通過將此父的<build/>標籤:

<build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-dependency-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

家長現在有一個target目錄與它build/groovy/computeProperties.groovy

在你的插件,你可以改變<scriptPath/>這樣:

<scriptPath>${project.build.directory}/build/groovy</scriptPath> 

而且,由於maven-dependency-plugin現在在validate階段運行,則gmaven-plugin必須在後面的階段運行。這是你可以像你想要的那樣闡述的東西。

這樣做的好處在於,如果有人只想檢出一個子模塊,那麼他們可以運行它,腳本jar將從回購站下載並使用。

不需要相對路徑...