Question

Is there an elegant way to use a specific dependency as a file object (cast dependency to file object). It is often needed to pass a file as an argument to a task/ant task etc. I helped me with configurations.myDependency.files.iterator().next() But this looks not very intuitive.

Was it helpful?

Solution

I think you mean configuration not dependency. Assuming you have something like:

configurations{
  myConf
}

dependencies{
  myConf 'mydep:mydep:1.0'
}

Then, if you are confident there is going to be only one file in all of your dependencies for myConf then you can do configurations.myConf.singleFile (return type is File).

However, since configuration can contain multiple files, in order to make your code more robust you should iterate over all files in configurations.myConf.files (return type is Set<File>).

If you need to extract specific dependency jar from a configuration you can do something like:

configurations.myConf.files { dep -> dep.name == 'mydep' }

where dep is of type Dependency and the return type is Set<File>.

For more details see Configuration javadoc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top