Friday, September 13, 2013

Migrating from Ant to Maven

Sample steps to migrate a java webapp from using Ant to Maven as the build tool.

Install maven


Learn about maven
Look up some maven tutorials and documentation e.g.


Reorganise source files
  • Create a skeleton maven directory structure for your code using an appropriate maven archetype command e.g run commands similar to the following on the command line.

For a war
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.mycompany -DartifactId=mywebapp


For a jar
mvn archetype:create -DgroupId=com.mycompany -DartifactId=myjar


  • Copy java source files directories from the ant project to the appropriate maven structure location e.g. C:\maven-migration\mywar\src\main\java\
  • Copy non-java files that are in the classes directory to the appropriate maven structure location e.g. C:\maven-migration\mywar\src\main\resources
  • Copy other web resources (everything else that is not within the classes directory) to the appropriate maven structure location e.g. C:\maven-migration\mywar\src\main\webapp
  • Delete the WEB-INF\lib directory under the maven webapp directory, if that was the location of libraries.


Create dependencies for libraries
  • From the ant project’s WEB-INF\lib directory, go through the libraries one at a time, determining if the library is used directly by your code. If so, add a dependency for the library to your maven project’s pom file. Use a maven central search website to determine dependency details like artefact id etc. e.g. http://www.maven-repository.com/. Mark or identify these as resolved e.g. on a piece of paper listing all the libraries.
  • Next, go through the remaining libraries and figure out which libraries are a dependency of one of the libraries already added. You may need to review the poms of the added libraries. You can see the library poms from the maven central search website. An IDE can also help to identify indirect dependencies e.g. Eclipse has a Dependency Hierarchy feature that shows these. Mark the indirect dependencies you’ve identified as resolved.
  • From the remaining libraries, identify those which are not used by your code directly but are required at runtime or for testing. Add these dependencies to your maven project pom with a runtime or test scope as appropriate. Mark these dependencies and the indirect dependencies that they bring, as resolved.
  • You’ll likely be left with non-maven libraries that aren’t built using maven or which aren’t available on a public maven repository. You’ll need to download packages for these libraries and see if they contain an indication of the libraries that they depend on. You’ll then need to create a pom for these libraries and deploy them to some maven repository e.g. an in-project one, see https://devcenter.heroku.com/articles/local-maven-dependencies
  • Once you have all the libraries matched, refine the dependencies in your pom e.g. to define exclusions for indirect dependencies that you don’t use. You can also override versions of libraries included.
  • Define other aspects of the pom similar to what is found in your ant project’s build.xml file e.g. configuration of the maven-compiler-plugin to generate java 1.6 compatible classes







1 comment: