Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute multiple ant targets in maven

Yes, yes, this is a XY type of problem. (I wanted to present it nicer without loosing any information)

In my project when I tried to execute the ANT tasks in maven it gives me the same error that I have in this sample. I took this example from here.

I tried to execute the multiple ant targets using the maven-antrun-plugin as below. But, it always executes the below most target only (when I don't mention depends attibute). When I use it, it gives following exception :

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>

What is the reason for this? How can I execute multiple ant tasks in maven?

like image 290
namalfernandolk Avatar asked Aug 06 '14 09:08

namalfernandolk


People also ask

Can we use maven and Ant together?

You can use the maven-antrun-plugin to invoke the ant build. Then use the build-helper-maven-plugin to attach the jar produced by ant to the project. The attached artifact will be installed/deployed alongside the pom. If you specify your project with packaging pom , Maven will not conflict with the ant build.

What is AntRun in maven?

This plugin provides the ability to run Ant tasks from within Maven. You can even embed your Ant scripts in the POM! It is not the intention of this plugin to provide a means of polluting the POM, so it's encouraged to move all your Ant tasks to a build.

What is maven Ant Tasks?

The Maven Ant Tasks allow several of Maven's artifact handling features to be used from within an Ant build. These include: Dependency management - including transitive dependencies, scope recognition and SNAPSHOT handling. Artifact deployment - deployment to a Maven repository (file integrated, other with extensions)


2 Answers

The answer not work for me in the current version of Maven AntRun plugin (1.8) and I miss how would be the build.xml file.

So, in the 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>

And the build.xml file:

<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>

The above example execute the follow tasks:

  1. Check if firefox exists on src/test/resources;
  2. If not exists:
    1. Download firefox;
    2. bunzip2 firefox bzip2 file;
    3. untar the firefox tar file;
    4. Adjust the folder permission using chmod command.

Works perfect!

like image 83
Dherik Avatar answered Oct 15 '22 19:10

Dherik


We can do it by using a seperate build.xml file

<target name="anytarget">
     <ant antfile="build.xml"/>
</target>
like image 28
namalfernandolk Avatar answered Oct 15 '22 18:10

namalfernandolk