How to run Ant targets with Maven?

The maven-antrun-plugin allows us to run ant targets with various maven build phases. I am going to explain very practical usage of maven-antrun-plugin specially for developers with development environment.apache-maven-logo

Normally with maven build, you will bundle your project either to a war file or ear file. You can directly copy this war or ear file into the server deployment folder by using maven-antrun-plugin. If your sever is tomcat, you can directly copy your archive file to ‘webapps‘ folder easily.  Some developers are used to copy the archive file to the server deployment folder manually even with their development. For them, this post will be very helpful.

If you want to use maven-antrun-plugin to copy your archive file into the server deployment folder every time when you build the project, you can add the following plugin into your pom.xml file and use what ever the ant targets as you wish.

Which pom.xml file, I am going to put this plugin to?

That is a good question. If you have multi module project, you should probably have either ear module or war module. Select the pom.xml file of that module and place the following plugin there. When you buld that project module, most of the time, it will be the last module bulding when you build your project in root level, maven will create either war file or ear file inside the target directory of your project module. We can configure maven-antrun-plugin so that it will copy that war file or ear file into server deployment folder.

In my case, I have multi module project and one module is a web module. I should place the the maven-antrun-plugin into web module’s pom.xml file.

<build>
    <finalName>shims-web</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <configuration>
                          <target>
                              <copy file="${project.build.directory}/shims-web.war" todir="${env.CATALINA_HOME}/webapps"/>
                          </target>
                    </configuration>
                    <goals>
                         <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

If you inspect the above snip of pom.xml file, I have given ‘install‘ as the phase of execution. It simply says that “execute this ant target just after the install life cycle phase” of the maven build. With the execution of ‘install‘ , maven will the package the whole project into the local repository as a war file or ear file, for use as a dependency in other projects locally. Also this will create the same file in the target directory of your workspace as well.

Our target is to copy that file from target director to server deployment folder with the build. The ${env.CATALINA_HOME} will refer to the our tomcat installation directory.

Now build the your project or project module. You can navigate to your project directory or project module directory where our modified pom.xml is. Run the following command.

$mvn clean install

The above command will traverse into all of the sub projects and run clean, then install (including all of the prior steps). You can run that command in root project level which build the entire project, or you can run it for specific project module, which build only that project.

.......................

[INFO] --- maven-antrun-plugin:1.7:run (default) @ shims-web ---

[INFO] Executing tasks

main:

[copy] Copying 1 file to /home/vinesh/apache-tomcat-7.0.25/webapps

[INFO] Executed tasks

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS ........................

If you are using m2eclipse to build your project, sometime, you may encounter strange scenario as follows. We hope maven will copy our archive file to the deployment folder of the server with the build completion. But sometime, maven will copy our archive file into the project workspace folder itself by creating the directories structure which we have mentioned as the destination directory in our pom.xml file of our ant copy target.

For example, in this case, it will create following directory structure in our local workspace folder rather than copying the file into the server folder. Just have a look on following image.

mvn

We are not expecting this scenario.

How can we overcome this?

You have to edit maven build target in eclipse by specifying the CATALINA_HOME environment variable. You can do this as follows.

Open “Run Configurations” window in eclipse and expand “Maven Build” category. You can see all the maven targets, you have created so far, have been listed there. Select the maven target which build your project with “clean install” goal.

And then open “Environment” tab. There you can add new variable for the CATALINA_HOME environmental variable as follows.

evn

Click on ‘Apply’ button and then run your maven target again. Look into your eclipse console carefully. You can see that maven is copying your archive file into server deployment folder.
 

Reference: How to run Ant targets with Maven? from our JCG partner Semika Kaluge at the Skillshared blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


7 × = fourteen



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close