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. Required fields are marked *

Related Blog Posts
Performance Test Liquibase Update
When doing a liquibase update to a database if you’re having performance issues, it can be hard to find out which updates are causing problems. If you need to measure the time to apply each […]
TICK Stack Monitoring for the Non-Technical
TICK – Telegraf, Influx, Chronograf, and Kapacitor – is a method of monitoring your systems and applications. In this article, I discuss in non-technical terms what the difference is between TICK and Prometheus Grafana A […]
Design Systems, Part 1 • Introduction
Business leaders need a practical guide to plan and execute Design System Initiatives. The aim of this series is to be that guide. This installment introduces terms and definitions as a primer on Design Systems.
ML for Translating Dysarthria Speech (Pre-Part 1)
What is Dysarthria? Per the Mayo Clinic, Dysarthria occurs when the muscles you use for speech are weak or you have difficulty controlling them. Dysarthria often causes slurred or slow speech that can be difficult […]