Dynamically import ant build script


(Aver Mimas) #1

Hi,

I have been going through Gradle user guide and some online posts about importing ANT script into Gradle so that those ANT targets are available as tasks. But all the examples looks similar and I couldn’t use that in my case.

I would like to download an ANT build script and then import it. Can this be done or does Gradle require the build script to be available during evaluation/configuration phase?

First, I started with Michael Kramer’s download plugin (https://github.com/michel-kraemer/gradle-download-task). Here’s what I have in my build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'de.undercouch:gradle-download-task:3.1.1'
    }
}

apply plugin: 'de.undercouch.download'
import de.undercouch.gradle.tasks.download.Download

defaultTasks 'importANTBuild'

// Fetch the build ant file to the file system
task downloadANTBuildFile(type: Download) {

    src 'http://mysite.com/path/to/ant/build.xml'
    dest new File(buildDir, "build.xml")
}

task importANTBuild(dependsOn: downloadANTBuildFile) {
	ant.importBuild(downloadANTBuildFile.dest) { antTaskName ->
		"mine-${antTaskName}".toString()
	}
	
	// Set group property for all Ant tasks.
	tasks.matching { task ->
		task.name.startsWith('mine-')
	}*.group = 'MY Tasks'

}

The first task can download the xml file successfully. But when I add the 2nd task to the build script, it complains on the following line, because the destination/target xml file does not yet exist.

ant.importBuild

Is it possible to import an ANT build script dynamically?


(Schalk Cronjé) #2

Split your build into two Gradle files - build.gradle, ant.gradle:

/* .. Every thing you had except for importANTBuild .. */

// The add this task
task runAntBuild( type : GradleBuild ) {
    dir projectDir
    buildFile 'ant.gradle'
    startParamater.projectProperties['antFile'] = downloadANTBuildFile.dest.absolutePath
    tasks ['mine-foo','mine-bar']
    dependsOn downloadANTBuildFile
}

In ant.gradle

ant.importBuild(project.properties.antFile) { "mine-${it}".toString() }

HTH. (See the docs for more details on GradleBuild)


(Aver Mimas) #3

Thanks for your reply and the suggestion.

Unfortunately I couldn’t get that to work the way I was hoping. I wanted to have the ant file imported so that I could just run the ant targets using gradle similar to if it was a static ant build script that gets imported into a gradle build script.