How do I add different jars to my build (from a plugin) that all have same file name?


(Mark Fisher) #1

Hi, (I don’t know why the first code snippet is duplicating itself, it’s not doing it in my edit)

I’m writing a plugin to add dependent jars from an application server to my build. All the jars I want to add are called classes.jar, and live in various submodule directories.

e.g.

DAS/lib/classes.jar DPS/lib/classes.jar …

(this is Oracle/ATG btw).

I can’t work out the syntax for adding a file to the dependency from its absolute path, so instead I tried adding the parent directory of the jar to the repositories section, and then create a compile dependency based on the name of the file with “:” prepended to it.

This works except that all the jar files have the same name (blame ATG, not me). I can’t change the file names of the jars.

The problem with this method is clearly there’s a name clash with all the dependencies simply adding “:classes”, and not being able to differentiate the different jars.

Here’s the snippet of code i tried

def filesToAdd = []
def parentDirs = filesToAdd.collect { new File(it).parentFile }.unique()
parentDirs << directoriesToAdd
project.repositories {
  flatDir dirs: parentDirs.unique()
}
  // convert "myfile.jar" to ":myfile" for each file in list
filesToAdd.collect { ":" + ... }.each { module ->
  project.dependencies {
    delegate."compile"(module)
  }
}

Can someone please shed some light on the syntax for adding an absolute named file to the build, I just can’t seem to work it out.

I’ve tried:

filesToAdd.each { file ->
 project.dependencies {
  classpath files(file) // gives error: 1
  classpath new File(file) // gives error: 2
 }
}

1 gives “No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.files() is applicable for argument types: (java.lang.String)”

2 gives “No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.classpath() is applicable for argument types: (java.io.File)”

So what’s the correct format for adding a jar whose exact filepath I know into the build?

Thanks, Mark


(Peter Niederwieser) #2

For a plugin (which needs a few ‘project.’), the syntax is ‘project.dependencies { configurationName project.files(fileOrPath) }’. Or, if you prefer the API version, ‘project.dependencies.add(configurationName, project.files(fileOrPath))’. For this to work, the referenced configuration has to exist.


(Mark Fisher) #3

If I understand you correctly, for my case, it should be:

filesToAdd.each { file ->
 project.dependencies {
  classpath project.files(file)
 }
}

but with this I get: “No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.classpath() is applicable for argument types: (org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection) values: [file collection]”


(Peter Niederwieser) #4

This means that no configuration named ‘classpath’ exists.


(Mark Fisher) #5

ah yes, I’d changed it from “compile” to “classpath” in one of my many attempts to fix this. changing it back has got it working again. Excellent!

Many many thanks for this quick response. This has been driving me nuts.