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

Object Partners profile.

One thought on “Easily Convert from Ant to Gradle

Leave a Reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]