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'])

One thought on “Easily Convert from Ant to Gradle

Leave a Reply

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