Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a dependency as file object?

Tags:

gradle

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.

like image 444
Cengiz Avatar asked Nov 29 '11 17:11

Cengiz


People also ask

Which file is used to add dependencies?

You can add dependencies to a package. json file from the command line or by manually editing the package. json file.

How do I add a dependency in gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

What is testImplementation in gradle?

For example the testImplementation configuration extends the implementation configuration. The configuration hierarchy has a practical purpose: compiling tests requires the dependencies of the source code under test on top of the dependencies needed write the test class.

What is FileTree in gradle?

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.


1 Answers

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.

like image 121
rodion Avatar answered Nov 13 '22 12:11

rodion