2010-10-25 101 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> 
    ...