代码之家  ›  专栏  ›  技术社区  ›  rektide

递归定义maven属性

  •  4
  • rektide  · 技术社区  · 14 年前

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

    这似乎是在给maven插件提供配置。例如,DBUnit需要用户名。从语义上讲,我们想到的解决方案如下所示,但是maven不允许以这种方式进行递归属性定义:

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

    对于参数化maven构建有什么想法,这样我们就可以保留庞大的属性定义中心列表了吗?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Eugene Kuleshov    14 年前

    您可以简单地使用相同的属性,而不是不同的属性名,而是在不同的配置文件中声明它们,无论是在pom.xml中还是在settings.xml中

        2
  •  1
  •   Romain Linsolas    14 年前

    你能对你遇到的问题再具体一点吗?你有什么错误吗?

    我已经在我的 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>
        ...