May 4, 2017

Easily Convert from Ant to Gradle

I stumbled onto an old application that hasn’t been touched for many years. The code was honestly well-written but it used Ant (without Ivy) to build. That is cumbersome in this day and age. I figured my first step would be to convert this to be driven with Gradle.

Using Ant with Gradle is actually pretty easy. Theoretically all you need in your build.gradle file is this:

ant.importBuild 'build.xml'

That just lets you run the Ant tasks with Gradle. So if you do gradle tasks you see the same tasks you use with that particular Ant build. That may be fine, but it’s not optimal – those names may not be the standard Gradle tasks names. More importantly, to me, is that you can’t use native Gradle plugins without lots of work writing custom configuration to get that to work with Ant. For example, to use the SonarQube plugin you have to tell the plugin that the Java classes are in src/java instead of the standard src/main/java. Instead of using the Gradle plugin, you could just add the configuration to the Ant’s build.xml file, but then you are adding more to the legacy build system. You could configure the Ant plugin in the Gradle build, but then you have the same problem.

Basically all you need to do is tell Gradle (or, actually, Gradle’s Java plugin) where the source files are. Once you do that, a well-written plugin will use those values instead of the standard ones. So it’s pretty easy to get a Gradle configuration to use Ant’s layout and still use Gradle’s powerful (and modern) plugins. Here is a sample of the build.gradle file that worked for me, including the SonarQube configuration.

plugins {
  id "org.sonarqube" version "2.3"
apply plugin: 'java'
// I said this was an old project
tasks.withType(JavaCompile) {
// This is really the important part
sourceSets {
    main {
        java {
            srcDirs = ['src']
        resources {
            srcDirs = ['resource']
       // add tests sources here (this project didn't have them!)
dependencies {
    // Add all the jar dependencies from the lib folder. 
    compile fileTree(dir: 'lib', include: ['*.jar'])

About the Author

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

One thought on “Easily Convert from Ant to Gradle

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Tracking Original URL Through Authentication
If you read my other post about refreshing AWS tokens, then you probably have a use case for keeping track of the original requested resource while the user goes through authentication so you can route […]
Using Spring Beans in a Kafka Streams ExceptionHandler
There are many things to know before diving into Kafka Streams. If you haven’t already, check out these 5 things as a starting point. Bullet 2 mentions designing for exceptions. Ironically, this seems to be […]