The code I posted earlier does not what I want after all.
Basically I want to have a custom taks which could loop over all Artifacts in the Configurations.
I need the actual File of the artifact as I need to index the file contents.
I already found out that e.g. the “implementation” configuration is not resolvabe.
Now based on the previous code we want to offer that users using the custom task can even define which configurations should be used to index it’s artifacts.
Yes, I want the dependency jars. And thanks for your patience
E.g. when I do the getFiles() like:
Set<Configuration> configurations = getProject().getConfigurations();
for (Configuration configuration : configurations) {
if (includeDependenciesConfigurations.contains(configuration.getName())) {
Set<File> files = configuration.getFiles();
.. do other stuff
}
}
then it gives me:
Caused by: java.lang.IllegalStateException: Resolving dependency configuration 'implementation' is not allowed as it is defined as 'canBeResolved=false'.
Instead, a resolvable ('canBeResolved=true') dependency configuration that extends 'implementation' should be resolved.
So I can’t seem to be able to get the files for the implementation configuration.
Btw besides the File object I also need the extension of the dependency.
The first option does not contain any files unfortunately…
The second option gives Resolving dependency configuration 'implementationCopy' is not allowed as it is defined as 'canBeResolved=false'.
But I think you put me in the right direction with copying the configuration.
I now have this:
ConfigurationContainer configurations = getProject().getConfigurations();
for (String s : includeDependenciesConfigurations) {
Configuration configuration = configurations.getByName(s);
Configuration copiedConfiguration = configuration.copyRecursive();
copiedConfiguration.setCanBeResolved(true); // THIS IS IMPORTANT; AS IT IS A COPIED CONFIG I GUESS IT IS OK TO DO.
ResolvedConfiguration resolvedConfiguration = copiedConfiguration.getResolvedConfiguration();
Set<ResolvedArtifact> artifacts = resolvedConfiguration.getResolvedArtifacts();
for (ResolvedArtifact artifact : artifacts) {
getLogger().lifecycle("INCLUDED CONFIG = " + s +
"\n INCLUDED ARTIFACT = " + artifact.getName() +
"\n INCLUDED CLASSIFIER = " + artifact.getClassifier() +
"\n INCLUDED EXTENSION = " + artifact.getExtension() +
"\n INCLUDED TYPE = " + artifact.getType() +
"\n INCLUDED FILE = " + artifact.getFile().getPath());
}
}
So I think I’m almost there.
The artifacts printed also include transitive dependencies.
I wonder if it would be possible to include only the top dependencies defined directly in the configuration.
So e.g.
dependencies {
implementation 'com.github.javafaker:javafaker:1.0.2' // just a random dep
}
would only include this artifact, but not the transitive dependency artifacts.
configuration.copyRecursive() vs configuration.copy() doesn’t seem to matter.
From the apidocs I see for copyRecursive:
Creates a copy of this configuration that contains the dependencies directly in this configuration and those derived from superconfigurations.
and for copy():
Creates a copy of this configuration that only contains the dependencies directly in this configuration (without contributions from superconfigurations).
I don’t think that is related to transitive dependencies, only derived from other configurations?
For a bit of further reading you might also be interested in Configuration. resolvedConfiguration.firstLevelModuleDependencies which I make usage of here