【问题标题】:Maven: read properties from file if it exists and set default properties otherwiseMaven:如果文件存在则从文件中读取属性,否则设置默认属性
【发布时间】:2015-07-15 13:13:32
【问题描述】:

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

<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”的值,但配置文件激活是在生命周期的早期评估的。这意味着在评估期间不使用子项目的值,并且永远不会正确读取属性。

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

【问题讨论】:

  • 您能否详细说明这些配置文件的目的是什么?你想通过这个实现什么?
  • 主要是为了让构建与本地应用实例共享属性。例如,应用程序可能配置为使用名为 my_app_db 的数据库,而我希望构建(例如用于集成测试)采用相同的数据库配置。

标签: maven properties-file


【解决方案1】:

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

  1. properties-maven-plugin:read-project-properties,安静设置为true
  2. maven-antrun-plugin: 使用&lt;exportAntProperties&gt;true&lt;/exportAntProperties&gt; 运行

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

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

例子:

       <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} 属性可以在 &lt;execution&gt; 块期间安全地使用,因为这些发生在子项目有机会正确覆盖该值之后。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-31
    • 2013-09-05
    • 2013-01-20
    • 2020-05-10
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多