是否可以从蚂蚁设置maven属性?

时间:2011-02-21 09:45:47

标签: properties maven-2 maven-antrun-plugin

我尝试使用maven-antrun-plugin检查第一次执行是否存在文件,然后相应地设置属性。在antrun-plugin的另一个执行(另一个阶段)中,我想要使用该属性。但是在一次执行中设置的属性不能用于另一个执行,因为它是一个ant而不是maven属性,并且不会被传播。

是否可以将ant属性传播到maven,或者换句话说从ant设置maven属性?

使用this question中的其他Maven构建不是一种选择。

另一种可能以某种方式工作的方式是外部build.xml,但这也不是一个选项,因为我必须把东西放在一个pom中。

我读过有关使用GMaven设置Maven属性的内容,但我想留下蚂蚁。

3 个答案:

答案 0 :(得分:8)

从maven-antrun-plugin的1.7版开始,根据plugin documentation(参见exportAntProperties)是可能的。所以,我想,在早期版本中:它不是; - )。

答案 1 :(得分:1)

您可以将策略重定向到激活不同的配置文件,具体取决于文件的存在而不是antrun-plugin:

<profiles>
    <profile>
        <id>notExist</id>
        <activation>
          <file>
            <missing>target/maven-archiver/notExist.properties</missing>
          </file>
        </activation>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.3</version>
                <executions>
                  <execution>
                    <phase>package</phase>
                    <goals>
                      <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>not exist</echo>
                        </tasks>
                    </configuration>
                  </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

<profile>
    <id>exist</id>
    <activation>
      <file>
        <exists>target/maven-archiver/pom.properties</exists>
      </file>
    </activation>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.3</version>
                <executions>
                  <execution>
                    <phase>package</phase>
                    <goals>
                      <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>not exist</echo>
                        </tasks>
                    </configuration>
                  </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

您可以使用activation profiles maven features根据激活标准区分一种配置和另一种配置。

我在示例中仅使用antrun-plugin来执行echo

答案 2 :(得分:1)

是的,甚至是默认值。使用echo设置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>