在Maven中调用特定的ant目标

时间:2012-10-10 06:42:24

标签: maven-3 maven-antrun-plugin

请找到以下代码段: -

    <plugins>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
        <id>compile</id>
        <phase>compile</phase>
        <configuration>
          <target name="test2">
            <property name="test_classpath" refid="maven.test.classpath"/>
            <property name="plugin_classpath" refid="maven.plugin.classpath"/>

            <echo message="test classpath:    ${test_classpath}"/>
            <echo message="plugin classpath:  ${plugin_classpath}"/>
          </target>
     <target name="test1">
            <property name="compile_classpath" refid="maven.compile.classpath"/>
            <property name="runtime_classpath" refid="maven.runtime.classpath"/>

            <echo message="compile classpath: ${compile_classpath}"/>
            <echo message="runtime classpath: ${runtime_classpath}"/>
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

当我除了“mvn compile”时,输出最后一个目标即test1。我已经尝试过mvn compile -Dtarget =“test2”和mvn compile -DantTarget =“test2”但是无法调用目标“test2”。请帮忙

1 个答案:

答案 0 :(得分:1)

maven ant-run插件仅支持configuration中的单个目标,但使用maven profiles可以获得相同的效果。将以下内容添加到pom.xml

<profiles>
    <profile>
        <id>test1</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                        <execution>
                            <id>compile-test1</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target name="test1">
                                    <property name="compile_classpath" refid="maven.compile.classpath"/>
                                    <property name="runtime_classpath" refid="maven.runtime.classpath"/>

                                    <echo message="compile classpath: ${compile_classpath}"/>
                                    <echo message="runtime classpath: ${runtime_classpath}"/>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>test2</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                        <execution>
                            <id>compile-test2</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target name="test2">
                                    <property name="test_classpath" refid="maven.test.classpath"/>
                                    <property name="plugin_classpath" refid="maven.plugin.classpath"/>

                                    <echo message="test classpath:    ${test_classpath}"/>
                                    <echo message="plugin classpath:  ${plugin_classpath}"/>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

您可以调用maven作为mvn -Ptest1mvn -Ptest2来激活其中一个配置文件并执行所选的目标。