I have a build script written in Groovy using antbuilder. I’m trying to convert this to gradle (to prove to my manager that Gradle is better). The tasks I am using are to get some build dependecies from Ivy and then execute a groovy task on my source. It looks like this…
def ant = new AntBuilder()
File scriptFile = new File(this.getClass().protectionDomain.codeSource.location.path)
File scriptDir = scriptFile.parentFile
File srcDir = new File(scriptDir, 'src')
File binDir = new File(scriptDir, 'bin')
if (binDir.isDirectory()) ant.delete(dir: binDir.path, failonerror: true)
// get build dependencies with Ivy
File buildIvyFile = new File(scriptDir, 'build.ivy.xml')
ant.echo "Resolving dependencies with Ivy using ${buildIvyFile}"
ant.taskdef(name: 'ivy_resolve',
classname: 'org.apache.ivy.ant.IvyResolve')
ant.taskdef(name: 'ivy_cachepath', classname: 'org.apache.ivy.ant.IvyCachePath')
ant.ivy_resolve(file: buildIvyFile.path, log: 'quiet')
ant.ivy_cachepath(pathid: 'ivy.path')
ant.echo
"Ivy classpath"
ant.path(refid: 'ivy.path').each { ant.echo "
$it" }
ant.echo "Compiling src to [$binDir]"
ant.mkdir(dir: binDir.path)
ant.taskdef(name: 'groovyc', classname: 'org.codehaus.groovy.ant.Groovyc')
ant.groovyc(srcdir: srcDir.path, destdir: binDir.path, listfiles: false , classpathref: 'ivy.path')
When I run this as a groovy script it works and i get output like this…
now I know I can just throw all that code into a task as ant builder is available to me via the ant var. When I that i get this…
$ gradle compile_groovy
:compile_groovy
[ant:echo] Resolving dependencies with Ivy using /home/tauser02/.gradle/caches/2.0/scripts/build_1cfc3qinok0ilc9r72t8ho4h59/ProjectScript/no_buildscript/build.ivy.xml
:compile_groovy FAILED
FAILURE: Build failed with an exception.
* Where:
Build file '/home/tauser02/build/test/build.gradle' line: 22
* What went wrong:
Execution failed for task ':compile_groovy'.
> taskdef class org.apache.ivy.ant.IvyResolve cannot be found
using the classloader AntClassLoader[]
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 8.64 secs
However, gradle cannot find the ivy taskdef classes probably because gradle does not ship with the ivy jar that ships with groovy. I’m sure there is a better way to deal with this and I have been beating my head against chapter 50 in the users guide but I am not getting any closer. what is the ‘Gradle’ way to do this?
The best way to do this is just convert it to a script using the groovy plugin. Something like:
apply plugin: 'groovy'
// You can leave this out if you use the default src/main/groovy dir instead
sourceSets {
main {
groovy {
srcDirs = [ 'src' ]
}
}
}
repositories {
ivy {
url 'http://your.ivy.repo'
}
}
dependencies {
compile 'log4j:log4j:1.4.12'
compile 'org.slf4j:jul-to-slf4j:1.7.7'
// etc...
}
Then you can just run groovyCompile. You’ll need to convert whatever is in your ivy settings to set up the repository properly.
The same one(s) you have defined in your ivy settings file. If you’re not sure, it might be easier to just use maven central instead - the dependencies above should all be available:
ok, looks like just adding mavenCentral works. However, there is feature of Ivy where it has an attribute called conf that you can pass in “master” to get only the jar you are asking for and not other dependencies that are brought in by default. Here are the original dependencies I was using with Ivy…