How can I get a file object for named dependency cache artifacts?


(keithh) #1

I have a series of dependencies for my project:

dependencies {
  compile 'org.codehaus.groovy:groovy-all:1.8.4'
  compile 'org.python:org.python:2.5.2'
  compile 'org.mozilla:rhino:1.7R4'
    testCompile 'junit:junit:4.8.2'
  testCompile 'org.mockito:mockito-all:1.8.5'
}

Later on when putting my jar file together, I want to explode some of the compile dependencies into my final jar. At the moment I do the following

jar {
  from {
 files(
  file('/home/me/.m2/repository/org/python/org.python/2.5.2/org.python-2.5.2.jar'),
  file('/home/me/.m2/repository/org/mozilla/rhino/1.7R4/rhino-1.7R4.jar'),
 ).collect { it.isDirectory() ? it : zipTree(it) }
  }

since the files happen to be in my Maven repo cache.

What I would like to do is pull these things out of the gradle cache without having to specify absolute paths. Something on the order of

jar {
  from {
 files(
  file('org.python:org.python:2.5.2'),
  file('org.mozilla:rhino:1.7R4'),
 ).collect { it.isDirectory() ? it : zipTree(it) }
  }

What is the actual syntax to do what I want?


(Peter Niederwieser) #2

I recommend to study the “Working with Files” and “Dependency Management” chapters in the Gradle user guide. For example, you could do something like:

jar {
  from zipTree(findArtifact("org.python-"))
  from zipTree(findArtifact("rhino-"))
}
  def findArtifact(prefix) {
  configurations.compile.find { it.name.startsWith(prefix) }
}

By the way, you don’t need to do ‘files(file(“foo”), file(“bar”))’. You can just do ‘files(“foo”, “bar”)’.