1

请找到以下代码片段:-

    <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”。请帮忙

4

1 回答 1

1

maven ant-run 插件在其配置中仅支持单个目标,但您可以使用maven 配置文件获得相同的效果。将以下内容添加到您的 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激活配置文件之一并执行所选的 ant 目标。

于 2012-10-10T15:31:54.777 回答