2010-10-25 99 views
4

我們試圖重新考慮我們的模塊化maven構建。我們已經引入了屬性DEPLOYMENT_ENV,它可能是「prod」,「dev」,「staging」,或者不是。我們進入的心態是,我們可以定義,說:遞歸定義maven屬性

dev.jdbc.username = yoyodyne 
dev.jdbc.password = 0verthruster 
staging.jdb.username = cavaliers 
staging.jdbc.password = 8thdim 

這似乎打破了餵養maven插件的配置。例如,DBUnit需要一個用戶名。在語義上,我們心目中的解決方案看上去像下面,但行家不允許以這種方式遞歸屬性定義:

<configuration> 
    <username>${${DEPLOYMENT_ENV}.jdbc.username}</username> 
</configuration> 

任何想法,參數化的Maven構建,使得我們能夠保持我們的大巨大的中央列表屬性定義?

+0

我敢肯定我已經做了這樣的事情,但它在Antrun插件的''節點中......這個''所在的插件?你有什麼行爲? – romaintaz 2010-10-25 19:33:15

回答

1

而是不同的屬性名稱,你可以簡單地使用相同的屬性,但在不同的配置文件中聲明它們,無論是在pom.xml中或Settings.XML中

+0

我們以前一直在做。我們基於配置文件的方法已經失控,不同的配置文件定義了非常不同的插件。我們完全同意,我們可以獨立解決意大利麪構建配置文件的問題,讓不同的配置文件聲明不同的變量。我們試圖儘可能避免與配置文件無關的配置,但這可能是一個好的甚至是必要的地方。 – rektide 2010-10-25 19:23:42

+0

我建議只用屬性創建多個配置文件。請注意,您可以同時激活多個配置文件,例如屬性+附加插件/ gials,或只是默認插件的屬性。 – 2010-10-25 19:40:00

1

你能成爲一個更具體一點您遇到的問題?你有什麼錯誤嗎?

我已經在我的pom.xml的一個使用這個遞歸屬性定義,在antrun插件<configuration>和它工作得很好:

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-antrun-plugin</artifactId> 
      <version>1.2</version> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>run</goal> 
        </goals> 
        <configuration> 
         <tasks> 
          ... 
          <ftp server="${my-ftp-url}" userid="${ftp-${appli}-${env}-username}" password="${ftp-${appli}-${env}-password}" 
           remotedir="${remoteDir}/sources" passive="yes"> 
           <fileset dir="../target/"> 
            <include name="*.tar.gz"/> 
           </fileset> 
          </ftp> 
          ... 

正如你可以在此代碼段中看到的,我用的是${ftp-${appli}-${env}-username}屬性,其中${appli},${env}${ftp-xxx-yyy-username}是來自命令行或settings.xml的屬性。

不管怎樣,尤金·庫列紹夫的建議,我會去一套<profiles>只有定義一些屬性,使用<properties>標籤,或外部屬性文件:

<build> 
    <plugins> 
     <!-- Properties loader --> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>properties-maven-plugin</artifactId> 
      <version>1.0-alpha-1</version> 
      <executions> 
       <execution> 
        <phase>initialize</phase> 
        <goals> 
         <goal>read-project-properties</goal> 
        </goals> 
        <configuration> 
         <files> 
          <file>${basedir}/${env-properties-file}</file> 
         </files> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 
... 
<profiles> 
    <!-- Development --> 
    <profile> 
     <id>dev</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
      <property> 
       <name>env</name> 
       <value>dev</value> 
      </property> 
     </activation> 
     <properties> 
      <env-properties-file>dev-environment.properties</env-properties-file> 
     </properties> 
    </profile> 

    <!-- Homologation --> 
    <profile> 
     <id>hom</id> 
     <activation> 
      <activeByDefault>false</activeByDefault> 
      <property> 
       <name>env</name> 
       <value>hom</value> 
      </property> 
     </activation> 
     <properties> 
      <env-properties-file>homologation-environment.properties</env-properties-file> 
     </properties> 
    </profile> 
    ...