1111
1212import org .elasticsearch .gradle .internal .precommit .DependencyLicensesTask ;
1313import org .elasticsearch .gradle .internal .precommit .LicenseAnalyzer ;
14- import org .gradle .api .artifacts .Configuration ;
15- import org .gradle .api .artifacts .Dependency ;
16- import org .gradle .api .artifacts .DependencySet ;
17- import org .gradle .api .artifacts .ModuleVersionIdentifier ;
14+ import org .gradle .api .artifacts .ArtifactCollection ;
1815import org .gradle .api .artifacts .ProjectDependency ;
16+ import org .gradle .api .artifacts .component .ModuleComponentIdentifier ;
17+ import org .gradle .api .file .ConfigurableFileCollection ;
1918import org .gradle .api .file .DirectoryProperty ;
2019import org .gradle .api .file .ProjectLayout ;
2120import org .gradle .api .internal .ConventionTask ;
2221import org .gradle .api .model .ObjectFactory ;
22+ import org .gradle .api .provider .MapProperty ;
23+ import org .gradle .api .provider .Property ;
24+ import org .gradle .api .provider .Provider ;
2325import org .gradle .api .provider .ProviderFactory ;
2426import org .gradle .api .tasks .Input ;
2527import org .gradle .api .tasks .InputDirectory ;
2628import org .gradle .api .tasks .InputFiles ;
29+ import org .gradle .api .tasks .Internal ;
2730import org .gradle .api .tasks .Optional ;
2831import org .gradle .api .tasks .OutputFile ;
2932import org .gradle .api .tasks .TaskAction ;
3437import java .nio .file .StandardOpenOption ;
3538import java .util .Arrays ;
3639import java .util .LinkedHashMap ;
40+ import java .util .Map ;
3741import java .util .Set ;
3842import java .util .regex .Pattern ;
3943import java .util .stream .Collectors ;
5155 * <li>license: <a href="https://spdx.org/licenses/">SPDX license</a> identifier, custom license or UNKNOWN.</li>
5256 * </ul>
5357 */
54- public class DependenciesInfoTask extends ConventionTask {
58+ public abstract class DependenciesInfoTask extends ConventionTask {
5559
56- private final DirectoryProperty licensesDir ;
60+ @ Inject
61+ public abstract ProviderFactory getProviderFactory ();
5762
58- @ OutputFile
59- private File outputFile ;
63+ /**
64+ * We have to use ArtifactCollection instead of ResolvedArtifactResult here as we're running
65+ * into a an issue in Gradle: https://github.com/gradle/gradle/issues/27582
66+ */
6067
61- private LinkedHashMap <String , String > mappings ;
68+ @ Internal
69+ abstract Property <ArtifactCollection > getRuntimeArtifacts ();
6270
63- public Configuration getRuntimeConfiguration () {
64- return runtimeConfiguration ;
71+ @ Input
72+ public Provider <Set <ModuleComponentIdentifier >> getRuntimeModules () {
73+ return mapToModuleComponentIdentifiers (getRuntimeArtifacts ().get ());
6574 }
6675
67- public void setRuntimeConfiguration (Configuration runtimeConfiguration ) {
68- this .runtimeConfiguration = runtimeConfiguration ;
69- }
76+ @ Internal
77+ abstract Property <ArtifactCollection > getCompileOnlyArtifacts ();
7078
71- public Configuration getCompileOnlyConfiguration () {
72- return compileOnlyConfiguration ;
79+ @ Input
80+ public Provider <Set <ModuleComponentIdentifier >> getCompileOnlyModules () {
81+ return mapToModuleComponentIdentifiers (getCompileOnlyArtifacts ().get ());
7382 }
7483
75- public void setCompileOnlyConfiguration (Configuration compileOnlyConfiguration ) {
76- this .compileOnlyConfiguration = compileOnlyConfiguration ;
84+ /**
85+ * We need to track file inputs here from the configurations we inspect to ensure we dont miss any
86+ * artifact transforms that might be applied and fail due to missing task dependency to jar
87+ * generating tasks.
88+ * */
89+ @ InputFiles
90+ abstract ConfigurableFileCollection getClasspath ();
91+
92+ private Provider <Set <ModuleComponentIdentifier >> mapToModuleComponentIdentifiers (ArtifactCollection artifacts ) {
93+ return getProviderFactory ().provider (
94+ () -> artifacts .getArtifacts ()
95+ .stream ()
96+ .map (r -> r .getId ())
97+ .filter (id -> id instanceof ModuleComponentIdentifier )
98+ .map (id -> (ModuleComponentIdentifier ) id )
99+ .collect (Collectors .toSet ())
100+ );
77101 }
78102
103+ private final DirectoryProperty licensesDir ;
104+
105+ @ OutputFile
106+ private File outputFile ;
107+
108+ private LinkedHashMap <String , String > mappings ;
109+
79110 /**
80111 * Directory to read license files
81112 */
@@ -102,17 +133,6 @@ public void setOutputFile(File outputFile) {
102133 this .outputFile = outputFile ;
103134 }
104135
105- /**
106- * Dependencies to gather information from.
107- */
108- @ InputFiles
109- private Configuration runtimeConfiguration ;
110- /**
111- * We subtract compile-only dependencies.
112- */
113- @ InputFiles
114- private Configuration compileOnlyConfiguration ;
115-
116136 @ Inject
117137 public DependenciesInfoTask (ProjectLayout projectLayout , ObjectFactory objectFactory , ProviderFactory providerFactory ) {
118138 this .licensesDir = objectFactory .directoryProperty ();
@@ -123,22 +143,18 @@ public DependenciesInfoTask(ProjectLayout projectLayout, ObjectFactory objectFac
123143
124144 @ TaskAction
125145 public void generateDependenciesInfo () throws IOException {
126- final DependencySet runtimeDependencies = runtimeConfiguration .getAllDependencies ();
127- // we have to resolve the transitive dependencies and create a group:artifactId:version map
128-
129- final Set <String > compileOnlyArtifacts = compileOnlyConfiguration .getResolvedConfiguration ()
130- .getResolvedArtifacts ()
131- .stream ()
132- .map (r -> {
133- ModuleVersionIdentifier id = r .getModuleVersion ().getId ();
134- return id .getGroup () + ":" + id .getName () + ":" + id .getVersion ();
135- })
136- .collect (Collectors .toSet ());
137146
147+ final Set <String > compileOnlyIds = getCompileOnlyModules ().map (
148+ set -> set .stream ()
149+ .map (id -> id .getModuleIdentifier ().getGroup () + ":" + id .getModuleIdentifier ().getName () + ":" + id .getVersion ())
150+ .collect (Collectors .toSet ())
151+ ).get ();
138152 final StringBuilder output = new StringBuilder ();
139- for (final Dependency dep : runtimeDependencies ) {
153+ Map <String , String > mappings = getMappings ().get ();
154+ for (final ModuleComponentIdentifier dep : getRuntimeModules ().get ()) {
140155 // we do not need compile-only dependencies here
141- if (compileOnlyArtifacts .contains (dep .getGroup () + ":" + dep .getName () + ":" + dep .getVersion ())) {
156+ String moduleName = dep .getModuleIdentifier ().getName ();
157+ if (compileOnlyIds .contains (dep .getGroup () + ":" + moduleName + ":" + dep .getVersion ())) {
142158 continue ;
143159 }
144160
@@ -147,25 +163,20 @@ public void generateDependenciesInfo() throws IOException {
147163 continue ;
148164 }
149165
150- final String url = createURL (dep .getGroup (), dep . getName () , dep .getVersion ());
151- final String dependencyName = DependencyLicensesTask .getDependencyName (getMappings (), dep . getName () );
152- getLogger ().info ("mapped dependency " + dep .getGroup () + ":" + dep . getName () + " to " + dependencyName + " for license info" );
166+ final String url = createURL (dep .getGroup (), moduleName , dep .getVersion ());
167+ final String dependencyName = DependencyLicensesTask .getDependencyName (mappings , moduleName );
168+ getLogger ().info ("mapped dependency " + dep .getGroup () + ":" + moduleName + " to " + dependencyName + " for license info" );
153169
154170 final String licenseType = getLicenseType (dep .getGroup (), dependencyName );
155- output .append (dep .getGroup () + ":" + dep . getName () + "," + dep .getVersion () + "," + url + "," + licenseType + "\n " );
171+ output .append (dep .getGroup () + ":" + moduleName + "," + dep .getVersion () + "," + url + "," + licenseType + "\n " );
156172 }
157173
158174 Files .write (outputFile .toPath (), output .toString ().getBytes ("UTF-8" ), StandardOpenOption .CREATE );
159175 }
160176
161177 @ Input
162- public LinkedHashMap <String , String > getMappings () {
163- return mappings ;
164- }
165-
166- public void setMappings (LinkedHashMap <String , String > mappings ) {
167- this .mappings = mappings ;
168- }
178+ @ Optional
179+ public abstract MapProperty <String , String > getMappings ();
169180
170181 /**
171182 * Create an URL on <a href="https://repo1.maven.org/maven2/">Maven Central</a>
0 commit comments