How to get the dependencies from a custom plugin

Hello,
First of all apologies for all the ignorance, this is my first approach to Gradle.

I’m building a custom plugin and task. I’m using Java as language because I don’t know Groovy yet.
The purpose of the task is pretty similar to the Eclipse plugin task that generates the .classpath for Eclipse.
In this case I don’t want to generate a classpath file, but a file that will include some static text that I will get from other file, and then the list of referenced Jars of my application. So its like a classpath with more configuration.

I’ve already created the plugin structure, and a dummy java task that does nothing, which I annoted as @TaskAction. So now, the question: How do I iterate through all the dependencies of my project? I want to get the filename of the referred Jars.
I got to

this.getProject().getDependencies()

but I don’t find anyway to do what I want.

Thank you very much.

Your project’s dependencies are stored under various configurations (compile, runtime, etc)
If you want all of them, in Java, you’ll have to
iterate over the configurations
Project.getConfigrations() gives you the list
Then you have to retrieve each artifact contained in each configuration, and get its name.
You can read Configuration - Gradle DSL Version 8.4 to find what you need

1 Like

You put me in the right path.
Thank you very much Francois, i’ll continue playing with Gradle :slight_smile:

Thank you again.
How do I get all the transitive dependencies? Now I get the dependencies stated in build.gradle from my “compile” configuration, but not the dependencies of those dependencies. I see that this groovy task defined in my build.gradletask

copyDependencies(type: Copy) {
   from configurations.compile
   into 'lib'
}

copies all the transitive dependencies to lib. I need to get them in my custom plugin too.

Thank you very much.

OK I find the solution myself. In order to get all the resolved dependencies from a dependency, you must use the method Configuration.resolve, that returns a set of Files. Exactly what I need:

for (Iterator<Configuration> iter = this.getProject().getConfigurations().iterator(); iter.hasNext(); ) {
	Configuration element = iter.next();
	Set<File> filesSet = element.resolve();
	for (Iterator<File> filesIterator = filesSet.iterator(); filesIterator.hasNext(); ) {
	File file = filesIterator.next();
    	System.out.println(file.getName());
     	dependencyNamesSet.add(jarLocation + file.getName());
}
1 Like