Write project's dependencies to file

Hi guys,

This is how I managed to copy all project’s dependecies as JARs into a destinationDir:

Project proj = project(':shared:java-parent:swaps')
copy {
    from proj.getConfigurations().getByName("runtime")
    into destinationDir
}

This is a little magic to me, but it works. Now, I want all these dependencies to be put into a file, as text list. To my surprise, I’m not even able to iterate over the list of dependencies - how to do it having “proj” object?

There is no magic here :smile:
proj.getConfigurations().getByName("runtime") returns a Configuration object, which implements FileCollection. That’s why you can use it in the from method of your copy task

For your dependencies list, you must be more specific.
If you want all 1st level modules declared by the given configuration, you can do something like

def file = file(‘foo’)
project.getConfigurations().getByName(‘runtime’).resolvedConfiguration.firstLevelModuleDependencies.each { file.text << it.name }

But be aware that in order to do this, you have to resolve the configuration, i.e. explicitly look for its artifacts. After this is done, the configuration is in its resolved state, and can no longer be modified.

Hi François,

thanks for response. What I need is all dependencies, transitive included - so I should probably use resolvedArtifacts instead of firstLevelModuleDependencies.

I’m still not 100% convinced about the configuration resoultion though - in the 1st code example, I could get all the files simply by having Configuration object. Does it mean that they are achievable without configuration resolution, or is the resolution going under the hood when I invoke “copy” task? By the way, I think my configuration is 100% static so I should not be afraid of it being resolved, just curious why it is needed here and was not in the first example. :smile:

You are absolutely right, the configuration is resolved under the hood. If you look at the code for DefaultConfiguration, you see that it uses a ConfigurationFileCollection to represent its internal FileCollection
And ConfigurationFileCollection has the following code

public Set<File> getFiles() {
  synchronized (resolutionLock) {
    ResolvedConfiguration resolvedConfiguration = getResolvedConfiguration();
    if (getState() == State.RESOLVED_WITH_FAILURES) {
      resolvedConfiguration.rethrowFailure();
     }
    return resolvedConfiguration.getFiles(dependencySpec);
  }
 }

So any attempt to get the files from the Configuration will resolve it.