How to define conditional properties in maven?

天大地大妈咪最大 提交于 2019-12-10 01:07:55

问题


For instance, I would like to have property Configuration set to ${env:AAA} if there is an environment variable AAA and to some other constant value if there is no such environment variable.

How do I do it in maven 2?


回答1:


It appears as though you activate a profile conditionally...

<profiles>
  <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>test</value>
      </property>
    </activation>
    ...
  </profile>
</profiles>

The profile will be activate when the environment variable is defined to the value test as in the following command:

mvn ... -Denvironment=test




回答2:


On the off-chance that a system property is acceptable, you can simply define the property in your POM file and override when required:

<project>
...
  <properties>
     <foo.bar>hello</foo.bar>
  </properties>
...
</project>

You can reference this property elsewhere in your POM by referring to ${foo.bar}. To override on the command line, just pass a new value:

mvn -Dfoo.bar=goodbye ...



回答3:


You can set a property conditionally using maven-antrun-plugin. Example setting install.path + echoing the value:

<plugin>
    <!-- Workaround maven not being able to set a property conditionally based on environment variable -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <exportAntProperties>true</exportAntProperties>
                <target>
                    <property environment="env"/>
                    <condition property="install.path" value="${env.INSTALL_HOME}" else="C:\default-install-home">
                        <isset property="env.INSTALL_HOME" />
                    </condition>
                    <echo message="${install.path}"/>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>


来源:https://stackoverflow.com/questions/14430122/how-to-define-conditional-properties-in-maven

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!