Configure a jar location/flat repository dynamically


(David Resnick) #1

I’m trying to configure a jar location/flat repository dynamically.

I have a multi-project build where the task ‘download’ on project ‘get-components’ is responsible for retrieving jars from an internal repository. I cannot control the location to which the jars are downloaded (though I can copy them after they are downloaded). My problem is using the retrieved jars in other projects.

Approach 1: Define the dependencies with

compile files("$JAR_DIR/a.jar")

Defining JAR_DIR in get-components:download is too late; the location checked for a.jar is /a.jar. If JAR_DIR is defined outside of a task in project get-components then this works.

Approach 2: Define the dependencies with

compile name: 'a'

and add the following repository:

repositories {
 flatDir {
  dirs "$JAR_DIR"
 }
}

The dependency is not found if the repository is added in get-components:download. In fact, it inexplicably isn’t even found if the repository is added outside the download task in the get-components project file (though it does work if the repository is added outside tasks in other gradle files).

Approach 3: 1. define the dependencies with

compile name: 'a'
  1. add the following repository:
repositories {
 flatDir {
  dirs '/downloaded'
 }
}
  1. copy downloaded jars to /downloaded

Approach 3 works, though I’d rather not need to copy the dependencies after I’ve downloaded them.

Any ideas on how I could get approaches 1 or 2 working?

As an aside, I’ve noticed that adding the flat dir repository greatly increases the dependency resolution time. I assembled my project when all compilation was up-to-date; these are the dependency resolution times I got (from profile report): With flat dir repository: 2m47s With hard-coded path to jars: 9s

I’m using 1.0 milestone 6.


(David Resnick) #2

Anyone?

In the end, because of the performance hit when using the flat dir repository I’m going with copying the jars to a location defined in advance and defining the dependency using

compile files("$JAR_DIR/a.jar")

.


(Luke Daley) #3

Hi David,

I don’t fully understand your scenario, but I think you are coming awry with evaluation order.

If project ‘get-components’ defines this variable then you must be sure that any other build script that uses it is evaluated after ‘get-components’.

See http://gradle.org/docs/current/userguide/userguide_single.html#multiprojectMessagesConfigDependencies

I’d also be tempted to wrap this in a higher level construct.

In ‘get-components’:

def JAR_DIR = file("downloads")
allprojects {
    downloadedJar = { jarName ->
        files("$JAR_DIR/${jarName}.jar")
    }
}

Then in a client project:

evaluationDependsOn ":get-components"
  dependencies {
    compile downloadedJar("foo")
}

That’s untested, but hopefully gets you started.