Maven:如果文件存在则从文件中读取属性,否则设置默认属性

Maven: read properties from file if it exists and set default properties otherwise

我知道在很多情况下,(笨拙但)可以使用两个配置文件来导致从文件中读取属性(如果存在)并设置默认值,例如

<profile>
    <id>my-default-props</id>
    <activation>
        <file>
            <missing>${my.file.path}</missing>
        </file>
    </activation>
    <properties>
        <my.prop1>Blah</my.prop1>
        <my.prop2>Another</my.prop2>
    </properties>
</profile>

<profile>
    <id>read-my-props</id>
    <activation>
        <file>
            <exists>${my.file.path}</exists>
        </file>
    </activation>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0-alpha-2</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>${my.file.path}</file>
                            </files>
                            <quiet>false</quiet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

不幸的是,这对我不起作用:我需要在各种子项目中覆盖 "my.file.path" 的值,但是在生命周期的早期评估配置文件激活。这意味着在评估期间未使用子项目的值,并且永远不会正确读取属性。

这对我来说似乎是一个足够普遍的要求,但谷歌搜索却告诉我并非如此。谁能告诉我如何实现这个目标?谢谢

回答我自己的问题...我发现我可以使用两个插件的组合,而不是使用一个配置文件,在同一阶段一个接一个地执行:

  1. properties-maven-plugin:read-project-properties with quiet set to true
  2. maven-ant运行-插件:运行 和 <exportAntProperties>true</exportAntProperties>

由于属性仅在它们不存在时才设置,因此这具有设置默认值的效果。

警告:这与设置一堆 属性 默认值 如果文件不存在 不太一样。这是在每个 属性 的基础上完成的。这可能会导致混合不连贯的属性(例如 MySQL 数据库驱动程序与 PostgreSQL 数据库 URL)。

示例:

       <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
            <execution>
                <id>read-my-properties</id>
                <phase>initialize</phase>
                <goals>
                    <goal>read-project-properties</goal>
                </goals>
                <configuration>
                    <files>
                        <file>${my.properties.file}</file>
                    </files>
                    <quiet>true</quiet>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <id>set-default-properties</id>
                <phase>initialize</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <exportAntProperties>true</exportAntProperties>
                    <target>
                        <property name="db.driver" value="org.postgresql.Driver"/>
                        <property name="db.name" value="my_db_name"/>
                        <!-- etc. -->
                    </target>
                </configuration>
            </execution>
        </executions>
    </plugin>

这里的关键是没有配置文件激活评估发生——这是行不通的,因为它发生得太早了。 ${my.properties.file} 属性 可以在 <execution> 块期间安全地使用,因为这些发生在子项目有机会正确覆盖该值之后。