代码之家  ›  专栏  ›  技术社区  ›  vaughan

如何使用maven antrun插件有条件地执行任务?

  •  6
  • vaughan  · 技术社区  · 15 年前

    我需要根据作为maven build命令的参数传入的环境变量执行一些ant命令。

    目前,我有3个任务块,只执行没有条件的任务块。

    <tasks name="isProdCheck">
      <condition property="isProd">
        <equals arg1="${environment}" arg2="PROD" />
      </condition>
    </tasks>
    
    <tasks if="isProd" depends="isProdCheck">
    ...
    </tasks>
    
    <tasks>
    ... I am the only block executed
    </tasks>
    

    我做错了什么,有更好的方法吗?

    4 回复  |  直到 9 年前
        1
  •  12
  •   Pascal Thivent    15 年前

    Using Attributes 这也是关于Maven 2的。

    所以,我可能遗漏了一些东西,但是,这非常令人困惑,你应该在你的问题中澄清这些要点。

    无论如何,正如你明确提到的Maven 1,我将尝试回答。如果我记得很清楚的话,我会写一个自定义目标并使用Jelly的 core:if core:when . 为此,请在中提供类似的内容 maven.xml :

    <project xmlns:j="jelly:core" xmlns:ant="jelly:ant">
      <goal name="my-goal">
        <j:if test="${environment == 'PROD'}">
          <ant:xxx .../>
        </j:if>
      </goal>
    </project>
    

    我真的不确定语法,所有这些Maven 1的东西都太遥远了,我没有测试它(我太懒了,无法安装Maven 1)。但我想你会的。这个 scripting reference

    老实说,我真的希望你有一个很好的理由选择Maven 1.x而不是Maven 2.x:)

    更新: 看起来OP实际上使用的是Maven 2,所以我将相应地更新我的问题。要实现所需的行为,可以使用Ant contrib的 if 任务如下所示:

      <build>
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.3</version>
            <executions>
              <execution>
                <phase>compile</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <tasks>
                    <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                      classpathref="maven.plugin.classpath" />
                    <if>
                      <equals arg1="${foo}" arg2="bar" />
                      <then>
                        <echo message="The value of property foo is bar" />
                      </then>
                      <else>
                        <echo message="The value of property foo is not bar" />
                      </else>
                    </if>
                  </tasks>
                </configuration>
              </execution>
            </executions>
            <dependencies>
              <dependency>
                <groupId>ant-contrib</groupId>
                <artifactId>ant-contrib</artifactId>
                <version>20020829</version>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
      </build>
    

    然后打电话 mvn compile -Dfoo=bar (这只是一个例子)。

    但是,所有这些都不是“马文式”做事方式。现在我对你想要做的事情有了更好的理解(但不完全是因为你没有解释你的最终目标),我认为使用 build profiles 更恰当的做法是,阅读了你自己的答案后,我认为你把事情复杂化了(而且你走错了路)。

    我知道你是一个Maven初学者,但我建议你尝试使用它,而不是依赖Ant,否则你将无法从中获益。此外,当你开始提问时,与其问具体的解决方案,不如解释你的一般问题,这样你会得到更好的答案。在这里,我不能提供更多的指导,因为我不知道你真正想要实现什么。

        2
  •  7
  •   nightshiba R. Karlus    4 年前

    AntContrib 之后 maven-antrun-plugin 1.5 使用 <target> <tasks> 根据 plugin usage

    <properties>
        <execute.my.target>true</execute.my.target>
    </properties>
    
    <build>
        ...
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>config</id>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <!-- this target will be executed always -->
                            <target>
                                <echo message="Hello there, I'm a simple target" />
                            </target>
                            
                            <!-- 
                                This target will be executed if and only if
                                the property is set to true
                            -->
                            <target name="my-target" if="execute.my.target">
                                <echo message="Conditional target..." />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        ...
    </build>
    

    上面的代码始终执行第一个目标,但第二个目标取决于属性值。您也可以为父模块和子模块项目配置它,在上定义插件 <pluginsManagement>

    更新 一定要使用 if 无参数 ${}

        3
  •  0
  •   vaughan    15 年前

    通过在项目根目录中的build.xml文件中创建具有“if”属性和条件属性的多个命名目标,解决了此问题,如下所示。

    <target name="prod" if="isProd" depends="isProdCheck">
        // do something
    </target>
    

    传递我所需的命令行开关的属性,并从Maven POM的tasks部分调用build.xml文件中的ant目标,如下所示:

    <tasks>
     <ant antfile="${basedir}/build.xml">
        <property name="environment" value="${environment}"/>        
        <target name="prod"/>
     </ant>          
    </tasks>
    
        4
  •  0
  •   Surasin Tancharoen    7 年前

    这里有一个可运行的示例 https://www.surasint.com/run-ant-with-if-from-maven/

    另一个解决方案是:将ant-contrib-1.0b3.jar保留为一个路径,然后像这样定义它

    <property name="runningLocation" location="" />
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="${runningLocation}/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>
    

    <target name="doSomething">
        <if>
            <equals arg1="${someProp}" arg2="YES" />
            <then>
                <echo message="It is YES" />
            </then>
            <else>
                <echo message="It is not YES" />
            </else>
        </if>
    </target>
    

    我把完整的代码示例放在这里,您可以下载 https://www.surasint.com/2017/04/09/run-ant-with-if-from-maven/