如何在Maven中执行多个Ant目标

8
是的,这是一种XY类型的问题。(我想以更好的方式呈现它,而不失去任何信息)
在我的项目中,当我尝试在maven中执行ANT任务时,它给了我与此示例相同的错误。我从这里获取了此示例。
我尝试使用maven-antrun-plugin执行多个ant目标,如下所示。 但是,它总是只执行最下面的目标(当我不提及depends属性时)。当我使用它时,它会产生以下异常:
Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (default) on project jibx-mvn-demo: An Ant BuildException has occured: Target "compile" does not exist in the project "maven-antrun-". It is used from target "myDAO". -> [Help 1]

pom.xml

    <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <configuration>
                        <target name="clean">
                            <echo>Clean up my working directory to be nice and sparkly</echo>
                        </target>
                        <target name="initDAO">
                            <echo>Initialize stuff for my DAO build</echo>
                            <echo>Maybe setup some properties?</echo>
                        </target>
                        <target name="makedir" depends="initDAO">
                            <echo>I need my directories for building.</echo>
                            <echo>But first, I need to setup stuff"</echo>
                        </target>
                        <target name="compile" depends="makedir">
                            <echo>I need to compile my dao source"</echo>
                            <echo>But first, I need to make the necessary directories</echo>
                        </target>
                        <target name="myDAO" depends="compile">
                            <echo>Here's where I package up my DAO</echo>
                            <echo>But I have to compile stuff before I can package it</echo>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

这是什么原因呢?我该如何在Maven中执行多个ant任务?
2个回答

6

在当前版本的Maven AntRun插件(1.8)中,答案对我无效,我不知道build.xml文件应该是什么样子。

所以,在pom.xml文件中:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>download-files</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <ant antfile="build.xml">
                        <target name="go"/>
                    </ant>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

还有 build.xml 文件:

<project name="Selenium" basedir=".">

    <target name="check-files">
        <available file="src/test/resources/firefox/firefox" property="firefox.exists"/>
    </target>

    <target name="download-firefox" depends="check-files" unless="firefox.exists">
        <!-- download Firefox for selenium -->
        <get src="https://ftp.mozilla.org/pub/firefox/releases/57.0/linux-x86_64/pt-BR/firefox-57.0.tar.bz2"
             dest="src/test/resources"
             verbose="false"
             usetimestamp="true"/>
        <bunzip2 src="src/test/resources/firefox-57.0.tar.bz2"
                 dest="src/test/resources"/>
        <untar src="src/test/resources/firefox-57.0.tar"
               dest="src/test/resources"/>
        <chmod file="src/test/resources/firefox/firefox/" perm="700"/>
    </target>

    <target name="go" depends="check-files, download-firefox"/>

</project>

上面的例子执行以下任务:
  1. 检查 src/test/resources 中是否存在 Firefox;
  2. 如果不存在:
    1. 下载 Firefox;
    2. bunzip2 解压 Firefox 的 bzip2 文件;
    3. untar 解压 Firefox 的 tar 文件;
    4. 使用 chmod 命令调整文件夹权限。
完美地工作!

1
经过近3个小时的努力,这个完美地运行了。 - Bob 1174

3
我们可以通过使用单独的build.xml文件来完成它。
<target name="anytarget">
     <ant antfile="build.xml"/>
</target>

你能更好地解释一下吗?并且请给我们一个例子。 - R. Karlus
他基本上是将所有所需的目标放入“build.xml”中;Maven将调用“build.xml”脚本,该脚本将使用默认目标。 - Iwan Satria
typo: seperate => separate - julodnik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接