Last Updated:
Senior person with a computer
Photo credit: https://www.freepik.com/free-photos-vectors/people

Ant to maven conversion - The painless method

Java

If you've ever come across a situation to convert your grandfathers' java project  (which he used to build with ant) to maven, you know the pain. Maybe you are reading this because its time to do that. So, here is a definitive guide to doing that without getting lost in the dependency hell (RIP grandpa)

Create the directory structure

Execute the following commands in order to create the standard maven directory structure.

for web apps
mvn archetype:generate -DgroupId={project-packaging} \
-DartifactId={project-name} \
-DarchetypeArtifactId=maven-archetype-webapp \
-DinteractiveMode=false
for standalone apps
mvn archetype:generate -DarchetypeGroupId={project-packaging} \
 -DarchetypeArtifactId=={project-name} \
 -DarchetypeVersion=1.4

Declare dependencies

  1. Declare the groupId, arifactId and version for the project

    <project>
     <modelVersion> 4.0.0 </modelVersion>
     <groupId> com.mycompany.app </groupId>
     <artifactId> my-app </artifactId>
     <version> 1.0.1 </version>
    </project>
    
  2. Find the dependencies for each jar file linked to the project.

    • Jars having pom.xml files inside - Extract the  from the pom file.
    • Jars not having a pom.xml file - Refer to the signature based artifact search method explained below.
    • Jars which cannot find using the above 2 methods - Install to your local maven repository or your companies artifact repository.
    mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \
     -DartifactId=<myArtifactId> -Dversion=<myVersion> \
     -Dpackaging=jar
    
  3. Execute mvn clean package to see whether pom has been declared correctly. You can compare the new build against the old build to verify the accuracy of the conversion.

Signature-based artifact search

Now we have come into the best part.

  1. If you are on Windows, download the File Checksum Integrity Verifier utility from Microsoft site.
  2. Execute the following from command line to generate a SHA1 signature for the jar file which we need to find.
    For a single jar file
    fciv commons-io-1.3.2.jar -sha1
    
    checksum generation with fciv tool
    checksum generation using FCIV tool
    For a directory of jars
    fciv D:\\path\\to\\jars -type *.jar -sha1
    
  3. Go to Nexus repository manager and click Advanced search
    Nexus repository manager
    Search  Nexus repository using the generated checksum value
  4. Find the matching artifact details using the SHA1 hash obtained from the above step.

Now you should be able to find all the dependency information refered from old Ant project by this way. So go ahead and get the hands dirty. 

ps: I will write another article to show how this whole process can be automated.