Main Tutorials

How to create an Ant build file from Maven (pom.xml)?

In some cases, you may need to generate Ant build file (build.xml) for your project integration purpose. For example, the klockwork code analysis tool is required Ant file to perform the static code analysis processes for java project.

Maven Ant plugin

Maven comes with a handy Ant plugin to generate Ant build file from Maven pom.xml easily. Locate your pom.xml file, and issue the command : mvn ant:ant.


D:\Project_ABC>mvn ant:ant

Output


D:\Project_ABC\SWP>mvn ant:ant
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'ant'.
[INFO] artifact org.apache.maven.plugins:maven-ant-plugin: checking for updates from central
...
[INFO] ------------------------------------------------------------------------
[INFO] Building abc
[INFO]    task-segment: [ant:ant]
[INFO] ------------------------------------------------------------------------
...
Downloading: http://repo1.maven.org/maven2/xalan/xalan/2.7.0/xalan-2.7.0.pom
491b downloaded
...
Downloading: http://repo1.maven.org/maven2/xalan/xalan/2.7.0/xalan-2.7.0.jar
2666K downloaded
[INFO] [ant:ant]
[INFO] Wrote Ant project for ABC to D:\Project_ABC
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 28 seconds
[INFO] Finished at: Thu Feb 12 16:16:15 SGT 2009
[INFO] Final Memory: 9M/16M
[INFO] ------------------------------------------------------------------------

Done

Maven just created 3 Ant files – build.xml, maven-build.xml and maven-build.properties.

Note
For more detail, please visit to this Maven’s Ant plugin official page

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
10 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Dinusha Nirmal
4 years ago

The hardest thing in ant to maven conversion is finding the correct dependency versions. Usually what everyone does is look inside jar META-INF folder and find the pom.xml. But there are cases you cannot find the exact version of a jar.
The most accurate method in this situation is to calculate the jar file hash and do a reverse search in Nexus repository. File hashes can be calculated with the help of Microsoft FCIV utility.

Everything is explained in this article step by step.
Ant to maven conversion – The painless method (https://devreads.xyz/ant-to-maven-conversion-the-painless-method/)

Ahmed
1 year ago
Reply to  Dinusha Nirmal

thanks for the insights dinusha, I am working on a similar thing but the difference is that java module system jdk 17 graalvm is involved and it makes stuff way different. Could you please send a working link to your article for ant to maven conversion as the one above doesn’t work.

NTS
9 years ago

Hi I created using your instructions for my Selenium Maven project and build.xml , pom-build.xml are created when I run build.xml , I get the error
BUILD FAILED
Target “build.xml” does not exist in the project , how to resolve this issue

Uncoverhowitispossibletofireup
11 years ago

I will immediately grasp your rss as I can’t to find your email subscription link or e-newsletter service. Do you have any? Please let me recognise in order that I may subscribe. Thanks.

Abhijit Deb
11 years ago

Dear Mr MYKong

I saw your article converting Maven to Ant. I need the reverse of this. We have a project which uses Ant for build. I need the pom.xml for the same.

Can you please let me know how do I achieve this?

Any help is greatly appreciated.

Thanks
Abhijit

Lucian Ochian
11 years ago
Reply to  Abhijit Deb

As far as I know that is impossible. Maven is a build system, Ant is a build tool. You can do the system->tool conversion, but not the other way around. Plus that Maven is more restrictive than Ant. If you want to convert an Ant script to Maven you’ll have to move the source code(java and resources) in certain folders, understand your dependencies and their scope, replace some Ant tasks with some Maven plugins, etc…

sunes
14 years ago

What could I do to change the local repository where ant makes maven download the artifacts??? I have the archives fetch.xml and get-m2.xml, and I have tried there, but it didn’t work because i didn’t do it properly, i suppose. Changing settings.xml in this case doesn’t work in any way.
Any idea??
Thank you!

jaya
6 years ago
Reply to  mkyong

Hello,
i have one ANT project and Maven Project. I want to refer Maven project in the ANT project.

What can be done in this case? Please let me know.
thanks