如何在maven中定义条件属性?

mar*_*ark 13 maven-2 maven

例如,如果没有这样的环境变量,我希望将属性Configuration设置为${env:AAA}有环境变量AAA和其他常量值.

我怎么在maven 2中做到这一点?

hd1*_*hd1 9

看起来好像你有条件地激活一个配置文件 ......

<profiles>
  <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>test</value>
      </property>
    </activation>
    ...
  </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

将环境变量定义为值时,将激活该配置文件,test如以下命令中所示:

mvn ... -Denvironment=test


Ali*_*aka 9

您可以使用 maven-antrun-plugin 有条件地设置属性。示例设置install.path+ 回显值:

<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>
Run Code Online (Sandbox Code Playgroud)


Dun*_*nes 6

如果系统属性不可接受,您只需在POM文件中定义属性并在需要时覆盖:

<project>
...
  <properties>
     <foo.bar>hello</foo.bar>
  </properties>
...
</project>
Run Code Online (Sandbox Code Playgroud)

您可以参考POM中的其他地方引用此属性${foo.bar}.要在命令行上覆盖,只需传递一个新值:

mvn -Dfoo.bar=goodbye ...
Run Code Online (Sandbox Code Playgroud)