Maven:从文件中读取属性(如果存在),否则设置默认属性 [英] Maven: read properties from file if it exists and set default properties otherwise

查看:124
本文介绍了Maven:从文件中读取属性(如果存在),否则设置默认属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know that in many cases, it is (clunky but) possible to use two profiles to result in properties being read from a file if present and to set default values otherwise, e.g.

<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"的值,但是概要文件激活是在生命周期的早期进行评估的.这意味着在评估过程中不会使用子项目的值,并且永远不会正确读取属性.

Unfortunately, this doesn't work for me: I need to override the value of "my.file.path" in various child projects, but profile activation is evaluated very early in the lifecycle. This means that the child project's value is not used during evaluation and the properties are never read properly.

对我来说,这似乎是一个足够普遍的要求,但Google却在告诉我其他情况.谁能告诉我我如何实现这个目标?谢谢.

It seems like a common enough requirement to me, but Googling is telling me otherwise. Can anyone please tell me how I can achieve this goal? Thanks.

推荐答案

要回答我自己的问题...我发现,与其使用配置文件,不如使用配置文件,我可以将两个插件结合使用,并在执行后触发一个其他处于同一阶段:

To answer my own question... I've found that rather than using a profile I can use a combination of two plugins with executions that fire one after the other in the same phase:

  1. properties-maven-plugin:read-project-properties,安静设置为true
  2. maven-antrun-plugin:使用<exportAntProperties>true</exportAntProperties>
  3. 运行
  1. properties-maven-plugin:read-project-properties with quiet set to true
  2. maven-antrun-plugin:run with <exportAntProperties>true</exportAntProperties>

由于仅在属性不存在时才进行设置,因此具有设置默认值的作用.

Since the properties are only set if they do not already exist, this has the effect of setting default values.

注意:这与设置一堆属性默认值(如果文件不存在)不太一样.而是根据每个属性完成此操作.这可能会导致混合使用不连贯的属性(例如,MySQL数据库驱动程序和PostgreSQL数据库URL).

Caveat: this isn't quite the same as setting a bunch of property defaults if the file does not exist. This is done on a per-property basis instead. This could potentially result in a mix of incoherent properties (e.g. MySQL database driver with a PostgreSQL database 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>

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

The key thing here is that there is no profile activation evaluation happening - which wouldn't work as it is happens too early. The ${my.properties.file} property can safely be used during the <execution> blocks as these happen after the child project has had the opportunity to correctly override the value.

这篇关于Maven:从文件中读取属性(如果存在),否则设置默认属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆