How to wrap an Ant build with Maven?

31,059

Solution 1

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.

In the example below, the ant build.xml is assumed to be in src/main/ant, have a compile goal, and output to ant-output.jar.

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
      <phase>process-resources</phase>
      <configuration>
        <tasks>
          <ant antfile="src/main/ant/build.xml" target="compile"/>
        </tasks>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>add-jar</id>
      <phase>package</phase>
      <goals>
        <goal>attach-artifact</goal>
      </goals>
      <configuration>
        <artifacts>
          <artifact>
            <file>${project.build.directory}/ant-output.jar</file>
            <type>jar</type>
          </artifact>
        </artifacts>
      </configuration>
    </execution>
  </executions>
</plugin>

Solution 2

You can actually wrap an ANT project with Maven by using multiple ant run goals as I wrote in a different question. Assuming your existing ant project has clean and build tasks, this might be a useful way of wrapping the project so you can use maven goals and have it map to existing ant code.

Solution 3

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
        <execution>
            <id>install-library</id>
            <phase>install</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>x.x</groupId>
                <artifactId>ant-out-atifacts</artifactId>
                <version>${project.version}</version>
                <file>ant-output.jar</file>
                <packaging>zip</packaging>
            </configuration>
        </execution>
    </executions>
</plugin>
Share:
31,059
digitaljoel
Author by

digitaljoel

Professional Java programmer since 2000 working with Spring, JPA, hibernate, maven, and other related technologies. Also an AKKI Kenpo black belt.

Updated on January 13, 2020

Comments

  • digitaljoel
    digitaljoel over 4 years

    We use maven for our large-ish product. All of our artifacts are deployed to a shared archiva repository using the maven deploy goal. I am now integrating a third party product that has an ant build. I know how to call ant targets from maven using the antrun plugin, but I'm not sure how to setup the pom in this instance. I don't want maven to actually generate an artifact, but I do want it to pull the artifact that was built by ant when the maven deploy goal is run.

    I am planning on having the pom adjacent to build.xml. The pom will use the antrun plugin in the package goal to call the ant target at the appropriate time to build the .war artifact.

    Questions:

    a) I am creating a .war file but it is created via ant, not Maven, so having a war packaging type in the pom doesn't make sense. What should my packaging type be?

    b) How do I cause maven to pull the artifact from my ant output directory for the deploy goal?

    c) If there are no good answers to A and B, then are there ant tasks that replicate the maven deploy functionality for getting my .war artifact into the shared repository?

  • digitaljoel
    digitaljoel over 14 years
    Very helpful answer Rich, Once I get it working in my project I'll accept your answer.
  • digitaljoel
    digitaljoel over 14 years
    Thanks Sal, good answer. Doesn't quite address my a, b, and c above, but helpful anyway.
  • mvmn
    mvmn about 11 years
    Great advice. Unfortunately it doesn't work for WAR files - maven war plugin doesn't seem to care about the attached WAR artifacts.
  • mvmn
    mvmn about 11 years
    P.S. Pardon me, it actually works fine once I change packaging specification to <packaging>pom</packaging> - the attached WAR no longer interferes with generated WAR, and thus ends up added in repo properly.