Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run an ant task in maven build phase before war is packaged?

When deploying a webapp I need to update some variables in UI resources, unzip some assets and concat some files, currently this is achieved via an ant task. I'm trying to run this task in the maven build process using something like this...

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>deploy-ui</id>
            <phase>prepare-package</phase>
            <inherited>false</inherited>
            <configuration>
                <target>
                    <property name="buildDir" value="${project.build.directory}/${project.build.finalName}" />
                    <ant antfile="build.xml" target="static-assets" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The above fails because the files have not yet been copied into target directory. If I set the phase to "package" the ant task runs fine and all the files are created/amended, but it's no help as the .war has already been built before the ant target is run.

Basically, I need to run my ant target near the end of the prepare-package phase.

Having looked though the Lifecycle Reference I can't workout how to expose the more granular Goals to the antrun plugin.

Any ideas?

like image 496
Patrick Clancey Avatar asked Oct 01 '12 10:10

Patrick Clancey


People also ask

How do I run Ant tasks in Maven?

Run. The maven-antrun-plugin has only one goal, run . This allows Maven to run Ant tasks. To do so, there must be an existing project and maven-antrun-plugin must have its <target> tag configured (although it would still execute without the <target> tag, it would not do anything).

Can you use Ant and Maven together?

Although most of the time we'll only use one of these, there are cases when using the two together makes sense. A common use case is when working on a legacy project that uses Ant, and we want to introduce Maven gradually while still keeping some existing Ant tasks in place.

What is Maven Ant task?

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)


1 Answers

Since I did not get any answer on my comment I guess that you want to stay using maven-antrun-plugin.

From what I've learned and experienced, if two plugins are to be executed on the same phase, then they will be executed in the order they are declared in pom.xml.

For this to work you will have to add the maven-war-plugin in the <plugins/> list after the maven-antrun-plugin.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>deploy-ui</id>
            <phase>package</phase>
            <inherited>false</inherited>
            <configuration>
                <target>
                    <property name="buildDir" value="${project.build.directory}/${project.build.finalName}" />
                    <ant antfile="build.xml" target="static-assets" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <!-- First step is to disable the default-war build step. -->
            <id>default-war</id>
            <phase>none</phase>
        </execution>
        <execution>
            <!-- Second step is to create an exploded war. Done in prepare-package -->
            <id>war-exploded</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>exploded</goal>
            </goals>
        </execution>
        <execution>
            <!-- Last step is to make sure that the war is built in the package phase -->
            <id>custom-war</id>
            <phase>package</phase>
            <goals>
                <goal>war</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Added some more executions so that the default-war is first disabled, then the war is exploded and lastly the war is packaged.

like image 150
maba Avatar answered Sep 21 '22 09:09

maba