Convert Ivy to Gradle

Sudeep Moothedath
4 min readJan 15, 2018

Apache Ivy is a Dependency Management framework. Combining it with Apache Ant gives a complete Build and Dependency Management system.

Ant+Ivy has it’s own Pros and Cons. Ironically, one of the main Pro and Con is Ant.

Ant adds a lot of flexibility to the system. You can write any custom task. Ant can also be extended through Java. But the same strengths also contribute to it’s weakness. Ant is written in XML. As your system grows, so does the build script. It becomes very tedious to manage large Ant XMLs.

Gradle

Gradle has become a matured build automation system. It has taken the good features of both Ivy and Maven. Gradle uses Domain Specific Language (DSL) instead of XMLs used by Ant and Maven. Apart from Groovy, Gradle also support Kotlin as the DSL for writing scripts.

Gradle is used across major companies and products such as Android, Netflix, LinkedIn etc. With the explosion of Microservices in the Cloud world, Gradle has become a de facto build automation tool for developers.

Conversion of Ant+Ivy to Gradle

As you re-platform existing applications to run in Cloud, it is a good idea to convert the existing Ant+Ivy to Gradle. I will be covering this conversion in the below sections. For this document, I have used Groovy as the language. The same can be written using Kotlin also.

Ivy file to Gradle

Ivy file contains the information about the artifacts to be published, configurations to be used and the dependencies of the artifact. Depending on size of the application, the list of dependencies could be very large. If we are dealing with multi-project applications, there will be “n” number of Ivy files that have to be converted to Gradle. It could soon become tedious to do this manually.

We can automate this using a script. I have written a sample script which converts an Ivy xml to a Gradle script. This script and the sample Ivy file used for conversion are given in my GitHub page.

The IvyToGradle.groovy file given in the page iterates through the list of dependencies and convert each of them into the Gradle format. This is a sample script and hence uses several assumptions. Each assumption is given clearly in the script itself.

Execute the script with the below command

gradle -b IvyToGradle.gradle executeIvyToGradle

There are several different combinations that can be present in an Ivy file. They include, but not limited to,

  • Dependencies may have a root conf declared at the <dependencies> tag level. This could mean that all dependencies may not have conf attribute.
  • Target conf could be declared at the <dependencies> level. This avoids having to declare it for each dependency.
  • You can avoid giving org attribute for a dependency, if it is in the same org as that of the artifact.
  • Artifacts can be included/excluded from a dependency
  • Transitive dependencies can be included/excluded
  • Custom configurations.
  • Versions can be declared using keywords such as “latest.integration” or using version ranges such as + or ) etc.

All these combinations are not covered in the script as they all may not be applicable for everyone. When you are trying to adopt this script, please check your current Ivy files and determine the features that should be included in your Groovy file. This is a simple script and it is very easy to extend it to include any of the above said combination.

Ant file to Gradle

A typical Ant file associated with Ivy will have at least the below tasks

  • Initialize Ivy
  • Resolve and Retrieve the dependencies
  • Compile and build the artifact(s)
  • Publish to a repository

For each of these, we have to write custom code in Ant files. For Ivy related tasks, the Ivy jar has to be present in Ant lib and the taskdef has to be declared.

It is easy to convert this to Gradle as it provides Out of the Box tasks for performing most of these actions. The developer has to add little to none code for performing these tasks.

Gradle Wrapper

Usage of Gradle Wrapper avoids the need for developers to install Gradle separately. The IvyToGradle.groovy script provided in the GitHub page creates a Gradle Wrapper.

Once the Wrapper is available, the developer has to just run the build command.

./gradlew clean build

The above command (Unix/Mac) will use the Gradle Wrapper properties to download Gradle, initialize it, clean existing build directory, if any, and builds the artifact. It also executes J Unit by default.

As you can see, the developer had to write 0 lines of code to implement clean, compile, test and jar commands, each of which require custom code in Ant. Of course, Gradle uses several assumptions such as the Gradle file is named as build.gradle and it is available in the same place where gradlew file is available etc. But, once a standard is established for the structure of the project, it will be easy for developers to follow.

Cleanup

Once the project is converted to Gradle, we can perform cleanup tasks such as deleting the residual files. The script in my page does not handle this but it can be easily extended to delete ivy.xml, build.xml and any property files used for Ant build.

We can also write code to delete any Ivy nature and add Gradle nature in IDE files which will help the IDE to load them correctly.

As I find time, the script in the GitHub page will be extended to include some of these features.

--

--