How do I tell gradle where to download dependencies to?

Hi All,

Having a first go at gradle here, so apologies if I’m on the wrong track,

Firstly, I would like to confirm if my approach is sound:

I want to have a directory in my project say “lib” where maven dependencies are downloaded to, (plus a couple of my own) Then, when jarring I want to add whatever jar’s are in this location to the built jar’s manifest like:

Class-Path: . …/lib/one_of_my_jars.jar …and so on.

My deploy task would then build the target structure something like: bin/myBuiltJar.jar lib/one_of_my_jars.jar

Is this a reasonable way of going about it?

If all you want to do is set up the manifest, copying them to a separate directory seems like overkill. You should be able to just access the dependencies for the relevant configuration and transform them into a Class-Path attribute. I think something like this would work.

jar {
  manifest.attributes 'Class-Path':configurations.runtime.files.collect { "../lib/${it.name}" }
}

OK great, that makes sense, and looks easy enough.

So, is it not common practice to keep required project jars in a local sub directory of the project itself?

Under ANT, that’s what I have been doing, then I add them to the eclipse build path, which in turn gives me a simple way of deploying those dependancies. By hand or a deployment target in ANT.

If I update the manifest as you demonstrate, wouldn’t I still need copy these jars as part of my deployment? or is there some gradle magic that I am missing here?

Thanks

Gradle’s dependency management mostly removes the need for a separate directory within your build that holds dependencies. They all get stored in a central cache, but you have the option to copy them anywhere you need to.

Yes if whatever you are distributing needs access to those JARs, you will want to include them. Depending on what your needs are I would take a look at the application which generates everything you need for an app that would be kicked off by a batch or shell script.

If you really just want to copy the jars somewhere, you can use a copy task.

For example:

task copyDeps(type: Copy) {
  from configurations.runtime
  into 'build/lib'
}

This is not working, at least on gradle 1.12 what is ‘configurations.runtime’ ? is it a folder ?

no, it isn’t a folder, but a set of dependencies your program needs when it is run. This says nothing about some physical location of downloaded jars (aka Gradle cache).

When you need to define where Gradle should store downloaded files, you can define a location for this Gradle cache. This is done by declaring an envrionment variable like this (Windows example):

set GRADLE_USER_HOME=D:\Java\gradle-prj\gradleCache

Please note this Gradle cache has some internal “cryptic” structure and files.

Note that if you use the Gradle Eclipse plugin, and you add the Gradle nature to your project, you don’t have to modify the Eclipse build path, as Eclipse understands Gradle build scripts and determines jar dependencies directly from that.

Inside gradleCache there are lot’s of folders with configs for different builds. That’s not what I needed. Currently I am getting the path for jars needed in my script in this way:

def jarPath = new ArrayList()
task setJarPath() << {
    configurations.compile.each {
        jarPath.add(it)
    }
}