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'
 
repositories{
 
    jcenter()
}
 
 
// I said this was an old project
tasks.withType(JavaCompile) {
    sourceCompatibility='1.3'
 
}
 
 
// 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.

Related Blog Posts
Android Development for iOS Developers
Android development has greatly improved since the early days. Maybe you tried it out when Android development was done in Eclipse, emulators were slow and buggy, and Java was the required language. Things have changed […]
Add a custom object to your Liquibase diff
Adding a custom object to your liquibase diff is a pretty simple two step process. Create an implementation of DatabaseObject Create an implementation of SnapshotGenerator In my case I wanted to add tracking of Stored […]
Keeping Secrets Out of Terraform State
There are many instances where you will want to create resources via Terraform with secrets that you just don’t want anyone to see. These could be IAM credentials, certificates, RDS DB credentials, etc. One problem […]
Validating Terraform Plans using Open Policy Agent
When developing infrastructure as code using terraform, it can be difficult to test and validate changes without executing the code against a real environment. The feedback loop between writing a line of code and understanding […]