1616
1717package org .gradlex .javamodule .dependencies .tasks ;
1818
19- import org .gradle .api .provider .Provider ;
20- import org .gradlex .javamodule .dependencies .JavaModuleDependenciesExtension ;
2119import org .gradle .api .DefaultTask ;
2220import org .gradle .api .Project ;
2321import org .gradle .api .artifacts .Configuration ;
2725import org .gradle .api .artifacts .component .ModuleComponentSelector ;
2826import org .gradle .api .artifacts .component .ProjectComponentIdentifier ;
2927import org .gradle .api .artifacts .dsl .ComponentMetadataHandler ;
30- import org .gradle .api .artifacts .result .ResolvedComponentResult ;
3128import org .gradle .api .attributes .AttributeContainer ;
3229import org .gradle .api .attributes .LibraryElements ;
3330import org .gradle .api .attributes .Usage ;
31+ import org .gradle .api .file .RegularFileProperty ;
3432import org .gradle .api .plugins .JavaPlugin ;
33+ import org .gradle .api .provider .ListProperty ;
3534import org .gradle .api .provider .Property ;
35+ import org .gradle .api .provider .Provider ;
3636import org .gradle .api .tasks .Input ;
37+ import org .gradle .api .tasks .InputFile ;
3738import org .gradle .api .tasks .SourceSet ;
3839import org .gradle .api .tasks .SourceSetContainer ;
3940import org .gradle .api .tasks .TaskAction ;
41+ import org .gradlex .javamodule .dependencies .JavaModuleDependenciesExtension ;
4042
4143import javax .inject .Inject ;
4244import java .util .Objects ;
4345import java .util .Set ;
4446import java .util .TreeSet ;
47+ import java .util .stream .Collectors ;
4548
4649public abstract class ModuleVersionRecommendation extends DefaultTask {
4750
48- private final ConfigurationContainer configurations ;
49- private final ComponentMetadataHandler components ;
50- private final SourceSetContainer sourceSets ;
51- private final JavaModuleDependenciesExtension javaModuleDependencies ;
51+ @ Input
52+ public abstract ListProperty <String > getResolutionResult ();
5253
5354 @ Input
5455 public abstract Property <Boolean > getPrintForPlatform ();
5556
5657 @ Input
5758 public abstract Property <Boolean > getPrintForCatalog ();
5859
60+ @ InputFile
61+ public abstract RegularFileProperty getPrintForPropertiesFile ();
62+
5963 @ Inject
6064 public ModuleVersionRecommendation (Project project ) {
61- this .configurations = project .getConfigurations ();
62- this .components = project .getDependencies ().getComponents ();
63- this .sourceSets = project .getExtensions ().getByType (SourceSetContainer .class );
64- this .javaModuleDependencies = project .getExtensions ().getByType (JavaModuleDependenciesExtension .class );
65- }
65+ ConfigurationContainer configurations = project .getConfigurations ();
66+ ComponentMetadataHandler components = project .getDependencies ().getComponents ();
67+ SourceSetContainer sourceSets = project .getExtensions ().getByType (SourceSetContainer .class );
68+ JavaModuleDependenciesExtension javaModuleDependencies = project .getExtensions ().getByType (JavaModuleDependenciesExtension .class );
6669
67- @ TaskAction
68- public void report () {
69- Set <String > moduleVersionsPlatform = new TreeSet <>();
70- Set <String > moduleVersionsCatalog = new TreeSet <>();
7170 AttributeContainer rtClasspathAttributes = configurations .getByName (JavaPlugin .RUNTIME_CLASSPATH_CONFIGURATION_NAME ).getAttributes ();
7271 Configuration latestVersionsClasspath = configurations .create ("latestVersionsClasspath" , c -> {
7372 c .setCanBeConsumed (false );
@@ -100,24 +99,40 @@ public void report() {
10099 || lcVersion .contains ("-b" )
101100 || lcVersion .contains ("beta" )
102101 || lcVersion .contains ("cr" )
102+ || lcVersion .contains ("ea" )
103103 || lcVersion .contains ("m" )
104104 || lcVersion .contains ("rc" )) {
105105
106106 c .setStatus ("integration" );
107107 }
108108 });
109+ getResolutionResult ().set (project .provider (() -> latestVersionsClasspath .getIncoming ().getResolutionResult ().getAllComponents ().stream ().map (
110+ result -> {
111+ ModuleVersionIdentifier moduleVersion = result .getModuleVersion ();
112+ if (moduleVersion != null && !(result .getId () instanceof ProjectComponentIdentifier )) {
113+ String ga = moduleVersion .getGroup () + ":" + moduleVersion .getName ();
114+ String version = moduleVersion .getVersion ();
115+ Provider <String > moduleName = javaModuleDependencies .moduleName (ga );
116+ if (moduleName .isPresent ()) {
117+ return moduleName .get () + ":" + version ;
118+ }
119+ }
120+ return null ;
121+ }).filter (Objects ::nonNull ).collect (Collectors .toList ())));
122+ }
109123
110- for (ResolvedComponentResult result : latestVersionsClasspath .getIncoming ().getResolutionResult ().getAllComponents ()) {
111- ModuleVersionIdentifier moduleVersion = result .getModuleVersion ();
112- if (moduleVersion != null && !(result .getId () instanceof ProjectComponentIdentifier )) {
113- String ga = moduleVersion .getGroup () + ":" + moduleVersion .getName ();
114- String version = moduleVersion .getVersion ();
115- Provider <String > moduleName = javaModuleDependencies .moduleName (ga );
116- if (moduleName .isPresent ()) {
117- moduleVersionsPlatform .add (" version(\" " + moduleName .get () + "\" , \" " + version + "\" )" );
118- moduleVersionsCatalog .add (moduleName .get ().replace ('.' , '_' ) + " = \" " + version + "\" " );
119- }
120- }
124+ @ TaskAction
125+ public void report () {
126+ Set <String > moduleVersionsPlatform = new TreeSet <>();
127+ Set <String > moduleVersionsCatalog = new TreeSet <>();
128+ Set <String > moduleVersionsPropertiesFile = new TreeSet <>();
129+
130+ for (String result : getResolutionResult ().get ()) {
131+ String moduleName = result .split (":" )[0 ];
132+ String version = result .split (":" )[1 ];
133+ moduleVersionsPlatform .add (" version(\" " + moduleName + "\" , \" " + version + "\" )" );
134+ moduleVersionsCatalog .add (moduleName .replace ('.' , '_' ) + " = \" " + version + "\" " );
135+ moduleVersionsPropertiesFile .add (moduleName + "=" + version );
121136 }
122137
123138 if (getPrintForPlatform ().get ()) {
@@ -140,6 +155,15 @@ public void report() {
140155 }
141156 }
142157
158+ if (getPrintForCatalog ().isPresent ()) {
159+ p ("" );
160+ p ("Latest Stable Versions of Java Modules - use in: " + getPrintForPropertiesFile ().get ().getAsFile ());
161+ p ("=================================================================================================" );
162+ for (String entry : moduleVersionsPropertiesFile ) {
163+ p (entry );
164+ }
165+ }
166+
143167 p ("" );
144168 }
145169
0 commit comments