What's the right way to gather actual locations of dependency jars?


(Nicholas Roth) #1

I’m trying to get a list of jars needed for a certain project so that I can add them to a list of classpath entries in a generated config file. The application will use the classpath from the config file to start JNI and access certain Java classes/methods.

The project looks like this:

configurations {
  fooAdapter
}

dependencies  {
  compile project(':foo')
  compile project(':foo_api')
  compile project(':bar')
}

task fooAdapterJar(type: Jar, dependsOn: compileJava) {
  from {
    configurations.compile.collect {
      it.isDirectory() ? it : zipTree(it)
    }
  }

  from compileJava.destinationDir
  appendix = "FooAdapter"
}

artifacts {
  fooAdapter fooAdapterJar
}

Of course it’s best not to have a fat jar, since we’re actually copying foo, foo_api, bar, the current project, and all their dependencies into one jar even though most of this already exist as build artifacts elsewhere. Shipping this is unprofessional since this fat jar has a huge amount of redundant information.

Instead, I would like to find the paths to each dependency jar and the jar for the current project. The artifact we deploy will have config files generated by Gradle that point to the jars relative to their packaged positions (a single directory, so only the names of each jar will be used). Running certain testing/development tasks in-place will use an artifact with Gradle-generated config files that point to the jars relative to the root project.

Does anyone know how to do this? Can Gradle do this at all?


(James Justinic) #2

If you’re just looking for a collection of the file names of your dependencies that will end up being copied to somewhere like ${rootDir}/lib, this should do it:

configurations.runtimeClasspath.files.collect { "$it.name" }

You may need to switch runtimeClasspath for a different configuration depending on your needs.