Mar 30, 2009

Managing Ivy Dependencies In Gant

Maven and IntelliJ are far from best friends. I really really don't enjoy using them together. This post is born out of that frustration. I want good, transparent dependency management, and a very very easy time integrating with my IDE.

What I wanted was to actually download the jar files to inside a project's folder and have them all in one directory to make updating from the IDE a simple process.

No, I don't want to use maven. Gradle is still to other for me - I also couldn't find out from the documentation how to explicitly download the jars to a location. So, Gant is my logical choice.

Right. Gant integrates well with Ivy - there's just not a lot of documentation. I wanted adding a new dependency to be as readable as this:
dependency( jar:'commons-lang:commons-lang:2.3').

Here's a gant build file to do just that:

libDirectory = 'lib'

includeTool << gant.tools.Ivy

def patternForScope(scope) {
"$libDirectory/$scope/[artifact]-[revision].[ext]"
}

def dependency( map ) {
def elements = map.jar.split(':')
def opts = [ organisation: elements[0],
module: elements[1],
revision: elements[2],
inline:true
]
ivy.retrieve( opts + [ pattern:patternForScope(map.scope ?: 'main') ] )
ivy.retrieve( opts + [ pattern:patternForScope('all') ] )
}

target ( retrieveDependencies : 'Downloads jars for lib/main, lib/test, and lib/all.' ) {
delete ( dir : libDirectory )
mkdir ( dir : libDirectory )
dependency ( jar:'commons-lang:commons-lang:2.3' )
dependency ( jar:'junit:junit:4.5', scope:'test')
}

setDefaultTarget ( retrieveDependencies )
Once you've run gant this is what the resulting structure looks like:
$ find lib
lib
lib/all
lib/all/commons-lang-2.3.jar
lib/all/junit-4.5.jar
lib/main
lib/main/commons-lang-2.3.jar
lib/test
lib/test/junit-4.5.jar
The build file here is derivative of this.

No comments: