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 ;
57-
58- @ OutputFile
59- private File outputFile ;
60+ @ Inject
61+ public abstract ProviderFactory getProviderFactory ();
6062
61- private LinkedHashMap <String , String > mappings ;
63+ @ Internal
64+ abstract Property <ArtifactCollection > getRuntimeArtifacts ();
6265
63- public Configuration getRuntimeConfiguration () {
64- return runtimeConfiguration ;
66+ @ Input
67+ public Provider <Set <ModuleComponentIdentifier >> getRuntimeModules () {
68+ return mapToModuleComponentIdentifiers (getRuntimeArtifacts ().get ());
6569 }
6670
67- public void setRuntimeConfiguration (Configuration runtimeConfiguration ) {
68- this .runtimeConfiguration = runtimeConfiguration ;
69- }
71+ @ Internal
72+ abstract Property <ArtifactCollection > getCompileOnlyArtifacts ();
7073
71- public Configuration getCompileOnlyConfiguration () {
72- return compileOnlyConfiguration ;
74+ @ Input
75+ public Provider <Set <ModuleComponentIdentifier >> getCompileOnlyModules () {
76+ return mapToModuleComponentIdentifiers (getCompileOnlyArtifacts ().get ());
7377 }
7478
75- public void setCompileOnlyConfiguration (Configuration compileOnlyConfiguration ) {
76- this .compileOnlyConfiguration = compileOnlyConfiguration ;
79+ @ InputFiles
80+ abstract ConfigurableFileCollection getClasspath ();
81+
82+ private Provider <Set <ModuleComponentIdentifier >> mapToModuleComponentIdentifiers (ArtifactCollection artifacts ) {
83+ return getProviderFactory ().provider (
84+ () -> artifacts .getArtifacts ()
85+ .stream ()
86+ .map (r -> r .getId ())
87+ .filter (id -> id instanceof ModuleComponentIdentifier )
88+ .map (id -> (ModuleComponentIdentifier ) id )
89+ .collect (Collectors .toSet ())
90+ );
7791 }
7892
93+ private final DirectoryProperty licensesDir ;
94+
95+ @ OutputFile
96+ private File outputFile ;
97+
98+ private LinkedHashMap <String , String > mappings ;
99+
79100 /**
80101 * Directory to read license files
81102 */
@@ -102,17 +123,6 @@ public void setOutputFile(File outputFile) {
102123 this .outputFile = outputFile ;
103124 }
104125
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-
116126 @ Inject
117127 public DependenciesInfoTask (ProjectLayout projectLayout , ObjectFactory objectFactory , ProviderFactory providerFactory ) {
118128 this .licensesDir = objectFactory .directoryProperty ();
@@ -123,22 +133,18 @@ public DependenciesInfoTask(ProjectLayout projectLayout, ObjectFactory objectFac
123133
124134 @ TaskAction
125135 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 ());
137136
137+ final Set <String > compileOnlyIds = getCompileOnlyModules ().map (
138+ set -> set .stream ()
139+ .map (id -> id .getModuleIdentifier ().getGroup () + ":" + id .getModuleIdentifier ().getName () + ":" + id .getVersion ())
140+ .collect (Collectors .toSet ())
141+ ).get ();
138142 final StringBuilder output = new StringBuilder ();
139- for (final Dependency dep : runtimeDependencies ) {
143+ Map <String , String > mappings = getMappings ().get ();
144+ for (final ModuleComponentIdentifier dep : getRuntimeModules ().get ()) {
140145 // we do not need compile-only dependencies here
141- if (compileOnlyArtifacts .contains (dep .getGroup () + ":" + dep .getName () + ":" + dep .getVersion ())) {
146+ String moduleName = dep .getModuleIdentifier ().getName ();
147+ if (compileOnlyIds .contains (dep .getGroup () + ":" + moduleName + ":" + dep .getVersion ())) {
142148 continue ;
143149 }
144150
@@ -147,25 +153,20 @@ public void generateDependenciesInfo() throws IOException {
147153 continue ;
148154 }
149155
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" );
156+ final String url = createURL (dep .getGroup (), moduleName , dep .getVersion ());
157+ final String dependencyName = DependencyLicensesTask .getDependencyName (mappings , moduleName );
158+ getLogger ().info ("mapped dependency " + dep .getGroup () + ":" + moduleName + " to " + dependencyName + " for license info" );
153159
154160 final String licenseType = getLicenseType (dep .getGroup (), dependencyName );
155- output .append (dep .getGroup () + ":" + dep . getName () + "," + dep .getVersion () + "," + url + "," + licenseType + "\n " );
161+ output .append (dep .getGroup () + ":" + moduleName + "," + dep .getVersion () + "," + url + "," + licenseType + "\n " );
156162 }
157163
158164 Files .writeString (outputFile .toPath (), output .toString (), StandardOpenOption .CREATE );
159165 }
160166
161167 @ 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- }
168+ @ Optional
169+ public abstract MapProperty <String , String > getMappings ();
169170
170171 /**
171172 * Create an URL on <a href="https://repo1.maven.org/maven2/">Maven Central</a>
0 commit comments