Ant migration problem

Hi, I am fairly new to Gradle and are trying to migrate from Ant. The Ant script is messy, so a complete move has to be done over time.

I tried at least half a dozen strategies, but ended up just running the entire ant build from Gradle, as a start with

build.dependsOn ‘ant-warbuild’"

The “easiest” solution, I think, would be to replace an ant task at a time with its corresponding gradle task. But lifecycles dont exactly match and there are some weird steps in the ant script. Since the Gradle lifecycle would control everything. I dont know exactly how to do that.

The Ant script consists of the following tasks:

  • clean.and.init
  • gen.hibernate.mappings
  • copy.server.code (this is the really weird one)
  • “java compile”
  • “copy other files” (also weird)
  • warbuild

I have manage to get ant to run the hibernate mappings from Gradle and Gradle to compile the java code. For the rest of the tasks listed above, it would be nice to run it as the Ant tasks, after the compile and as part of the Gradle lifecycle.

Here is the script so far:

plugins {
      id 'java-library'
}

project.version = '6.0.3-SNAPSHOT'
project.archivesBaseName = 'patient'

compileJava.dependsOn 'ant-gen.mappings'

dependencies {
     implementation project(':patient-interface')

     implementation fileTree(dir: 'lib', include: '*.jar')
     implementation fileTree(dir: 'lib-build', include: '*.jar')    
}

How can I inject the “copy.file…” tasks and “warbuild” task after “compileJava” as the final tasks of the build, replacing the default tasks for this part?
Any suggestions on how to proceed, is much appreciated.

Based on information from the document Migrating Builds From Apache Ant, I worked out the following solution to this kind of problem.

When importing Ant tasks, they are imported as separate tasks in Gradle, becoming gradle tasks, of sorts. That allowed me to link together the central parts from ant in Gradle instead, to form at new and complete build cycle.

The steps surrounding the “java compile” task was then linked together in gradle, so as to interject gradle’s “compileJava” task, where previously the ant compile task existed. Though not a native Gradle build cycle, its a good way to step by step replace ant with gradle tasks. Finally, when everything is controlled by Gradle, its easier to rewrite the build script to utilise Gradles feature completely or to make a simpler build script.

To import

// Tasks get the same name as in ant
ant.importBuild(‘build-impl.xml’)

// tasks get prefixed with “ant-” so as not to conflict with gradles task of potentially same name
ant.importBuild(‘build-impl.xml’) { antTaskName →
“ant-${antTaskName}”.toString()
}

The linking was done as follow:

tasks.named(‘compileJava’) { dependsOn = [ ‘copy.all.servercode’ ] }
tasks.named(‘copy.other.files’) { dependsOn = [ ‘compileJava’ ] }

Since output directories are not exactly the same, I had to add a task to move files about, so the resulting war file was as identical to the old one as possible, at least for now.