1- /*
2- * Copyright the GradleX team.
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
7- *
8- * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
15- */
16-
1+ // SPDX-License-Identifier: Apache-2.0
172package org .gradlex .maven .gmm ;
183
194import com .google .gson .stream .JsonWriter ;
20- import org .apache .maven .artifact .Artifact ;
21- import org .apache .maven .model .Dependency ;
22- import org .apache .maven .model .Exclusion ;
23- import org .apache .maven .project .MavenProject ;
24- import org .gradlex .maven .gmm .checksums .HashUtil ;
25-
265import java .io .File ;
276import java .io .IOException ;
287import java .io .Writer ;
3211import java .util .Map ;
3312import java .util .Optional ;
3413import java .util .TreeMap ;
14+ import org .apache .maven .artifact .Artifact ;
15+ import org .apache .maven .model .Dependency ;
16+ import org .apache .maven .model .Exclusion ;
17+ import org .apache .maven .project .MavenProject ;
18+ import org .gradlex .maven .gmm .checksums .HashUtil ;
3519
3620/**
3721 * The Gradle module metadata file generator is responsible for generating a JSON file describing module metadata.
@@ -42,18 +26,25 @@ public class GradleModuleMetadataWriter {
4226
4327 private enum Variant {
4428 API_ELEMENTS ("apiElements" , "java-api" , "library" , "jar" , null , Collections .singletonList ("compile" )),
45- RUNTIME_ELEMENTS ("runtimeElements" , "java-runtime" , "library" , "jar" , null , Arrays .asList ("compile" , "runtime" )),
29+ RUNTIME_ELEMENTS (
30+ "runtimeElements" , "java-runtime" , "library" , "jar" , null , Arrays .asList ("compile" , "runtime" )),
4631 JAVADOC_ELEMENTS ("javadocElements" , "java-runtime" , "documentation" , null , "javadoc" , Collections .emptyList ()),
4732 SOURCES_ELEMENTS ("sourcesElements" , "java-runtime" , "documentation" , null , "sources" , Collections .emptyList ());
4833
4934 private final String name ;
5035 private final String usage ;
5136 private final String category ;
52- private final String libraryelements ; //nullable
37+ private final String libraryelements ; // nullable
5338 private final String docstype ; // nullable
5439 private final List <String > scopes ;
5540
56- Variant (String name , String usage , String category , String libraryelements , String docstype , List <String > scopes ) {
41+ Variant (
42+ String name ,
43+ String usage ,
44+ String category ,
45+ String libraryelements ,
46+ String docstype ,
47+ List <String > scopes ) {
5748 this .name = name ;
5849 this .usage = usage ;
5950 this .category = category ;
@@ -63,15 +54,24 @@ private enum Variant {
6354 }
6455 }
6556
66- public static void generateTo (MavenProject project ,
67- List <Dependency > platformDependencies , List <Capability > capabilities ,
68- List <Dependency > removedDependencies ,
69- List <Dependency > compileOnlyApiDependencies ,
70- Writer writer ) throws IOException {
57+ public static void generateTo (
58+ MavenProject project ,
59+ List <Dependency > platformDependencies ,
60+ List <Capability > capabilities ,
61+ List <Dependency > removedDependencies ,
62+ List <Dependency > compileOnlyApiDependencies ,
63+ Writer writer )
64+ throws IOException {
7165 JsonWriter jsonWriter = new JsonWriter (writer );
7266 jsonWriter .setHtmlSafe (false );
7367 jsonWriter .setIndent (" " );
74- writeComponentWithVariants (project , platformDependencies , capabilities , removedDependencies , compileOnlyApiDependencies , jsonWriter );
68+ writeComponentWithVariants (
69+ project ,
70+ platformDependencies ,
71+ capabilities ,
72+ removedDependencies ,
73+ compileOnlyApiDependencies ,
74+ jsonWriter );
7575 jsonWriter .flush ();
7676 writer .append ('\n' );
7777 }
@@ -103,16 +103,24 @@ private static Map<String, String> variantAttributes(Variant variant) {
103103 return attributes ;
104104 }
105105
106- private static void writeComponentWithVariants (MavenProject project ,
107- List <Dependency > platformDependencies ,
108- List <Capability > capabilities ,
109- List <Dependency > removedDependencies ,
110- List <Dependency > compileOnlyApiDependencies ,
111- JsonWriter jsonWriter ) throws IOException {
106+ private static void writeComponentWithVariants (
107+ MavenProject project ,
108+ List <Dependency > platformDependencies ,
109+ List <Capability > capabilities ,
110+ List <Dependency > removedDependencies ,
111+ List <Dependency > compileOnlyApiDependencies ,
112+ JsonWriter jsonWriter )
113+ throws IOException {
112114 jsonWriter .beginObject ();
113115 writeFormat (jsonWriter );
114116 writeIdentity (project , jsonWriter );
115- writeVariants (project , platformDependencies , capabilities , removedDependencies , compileOnlyApiDependencies , jsonWriter );
117+ writeVariants (
118+ project ,
119+ platformDependencies ,
120+ capabilities ,
121+ removedDependencies ,
122+ compileOnlyApiDependencies ,
123+ jsonWriter );
116124 jsonWriter .endObject ();
117125 }
118126
@@ -131,21 +139,42 @@ private static void writeIdentity(MavenProject project, JsonWriter jsonWriter) t
131139 jsonWriter .endObject ();
132140 }
133141
134-
135- private static void writeVariants (MavenProject project ,
136- List <Dependency > platformDependencies ,
137- List <Capability > capabilities ,
138- List <Dependency > removedDependencies ,
139- List <Dependency > compileOnlyApiDependencies ,
140- JsonWriter jsonWriter ) throws IOException {
141-
142- Optional <Artifact > javadocJar = project .getAttachedArtifacts ().stream ().filter (jar -> "javadoc" .equals (jar .getClassifier ())).findFirst ();
143- Optional <Artifact > sourcesJar = project .getAttachedArtifacts ().stream ().filter (jar -> "sources" .equals (jar .getClassifier ())).findFirst ();
142+ private static void writeVariants (
143+ MavenProject project ,
144+ List <Dependency > platformDependencies ,
145+ List <Capability > capabilities ,
146+ List <Dependency > removedDependencies ,
147+ List <Dependency > compileOnlyApiDependencies ,
148+ JsonWriter jsonWriter )
149+ throws IOException {
150+
151+ Optional <Artifact > javadocJar = project .getAttachedArtifacts ().stream ()
152+ .filter (jar -> "javadoc" .equals (jar .getClassifier ()))
153+ .findFirst ();
154+ Optional <Artifact > sourcesJar = project .getAttachedArtifacts ().stream ()
155+ .filter (jar -> "sources" .equals (jar .getClassifier ()))
156+ .findFirst ();
144157
145158 jsonWriter .name ("variants" );
146159 jsonWriter .beginArray ();
147- writeVariant (project , Variant .API_ELEMENTS , platformDependencies , capabilities , removedDependencies , compileOnlyApiDependencies , project .getArtifact (), jsonWriter );
148- writeVariant (project , Variant .RUNTIME_ELEMENTS , platformDependencies , capabilities , removedDependencies , null , project .getArtifact (), jsonWriter );
160+ writeVariant (
161+ project ,
162+ Variant .API_ELEMENTS ,
163+ platformDependencies ,
164+ capabilities ,
165+ removedDependencies ,
166+ compileOnlyApiDependencies ,
167+ project .getArtifact (),
168+ jsonWriter );
169+ writeVariant (
170+ project ,
171+ Variant .RUNTIME_ELEMENTS ,
172+ platformDependencies ,
173+ capabilities ,
174+ removedDependencies ,
175+ null ,
176+ project .getArtifact (),
177+ jsonWriter );
149178 if (javadocJar .isPresent ()) {
150179 writeVariant (project , Variant .JAVADOC_ELEMENTS , null , null , null , null , javadocJar .get (), jsonWriter );
151180 }
@@ -160,17 +189,27 @@ private static void writeFormat(JsonWriter jsonWriter) throws IOException {
160189 jsonWriter .value (FORMAT_VERSION );
161190 }
162191
163- private static void writeVariant (MavenProject project , Variant variant ,
164- List <Dependency > platformDependencies ,
165- List <Capability > capabilities ,
166- List <Dependency > removedDependencies ,
167- List <Dependency > addedDependencies ,
168- Artifact artifact , JsonWriter jsonWriter ) throws IOException {
192+ private static void writeVariant (
193+ MavenProject project ,
194+ Variant variant ,
195+ List <Dependency > platformDependencies ,
196+ List <Capability > capabilities ,
197+ List <Dependency > removedDependencies ,
198+ List <Dependency > addedDependencies ,
199+ Artifact artifact ,
200+ JsonWriter jsonWriter )
201+ throws IOException {
169202 jsonWriter .beginObject ();
170203 jsonWriter .name ("name" );
171204 jsonWriter .value (variant .name );
172205 writeAttributes (variantAttributes (variant ), jsonWriter );
173- writeDependencies (variant , project .getDependencies (), platformDependencies , removedDependencies , addedDependencies , jsonWriter );
206+ writeDependencies (
207+ variant ,
208+ project .getDependencies (),
209+ platformDependencies ,
210+ removedDependencies ,
211+ addedDependencies ,
212+ jsonWriter );
174213 writeArtifacts (artifact , jsonWriter );
175214 writeCapabilities (project , capabilities , jsonWriter );
176215
@@ -245,12 +284,14 @@ private static void writeChecksums(File artifact, JsonWriter jsonWriter) throws
245284 jsonWriter .value (HashUtil .md5 (artifact ).asHexString ());
246285 }
247286
248- private static void writeDependencies (Variant variant ,
249- List <Dependency > dependencies ,
250- List <Dependency > platformDependencies ,
251- List <Dependency > removedDependencies ,
252- List <Dependency > addedDependencies ,
253- JsonWriter jsonWriter ) throws IOException {
287+ private static void writeDependencies (
288+ Variant variant ,
289+ List <Dependency > dependencies ,
290+ List <Dependency > platformDependencies ,
291+ List <Dependency > removedDependencies ,
292+ List <Dependency > addedDependencies ,
293+ JsonWriter jsonWriter )
294+ throws IOException {
254295 if (dependencies .isEmpty () && isNullOrEmpty (platformDependencies ) && isNullOrEmpty (addedDependencies )) {
255296 return ;
256297 }
@@ -265,8 +306,10 @@ private static void writeDependencies(Variant variant,
265306 // Dependency is not in scope
266307 continue ;
267308 }
268- if (removedDependencies != null && removedDependencies .stream ().anyMatch (removed ->
269- dependency .getGroupId ().equals (removed .getGroupId ()) && dependency .getArtifactId ().equals (removed .getArtifactId ()))) {
309+ if (removedDependencies != null
310+ && removedDependencies .stream ()
311+ .anyMatch (removed -> dependency .getGroupId ().equals (removed .getGroupId ())
312+ && dependency .getArtifactId ().equals (removed .getArtifactId ()))) {
270313 // Dependency is explicitly removed (e.g. because the shade plugin removes it from the POM as well)
271314 continue ;
272315 }
@@ -289,8 +332,8 @@ private static void writeDependencies(Variant variant,
289332 jsonWriter .endArray ();
290333 }
291334
292- private static void writeDependency (Dependency dependency , boolean toPlatform ,
293- JsonWriter jsonWriter ) throws IOException {
335+ private static void writeDependency (Dependency dependency , boolean toPlatform , JsonWriter jsonWriter )
336+ throws IOException {
294337 jsonWriter .beginObject ();
295338 jsonWriter .name ("group" );
296339 jsonWriter .value (dependency .getGroupId ());
@@ -356,8 +399,8 @@ private static void writeExcludes(List<Exclusion> excludes, JsonWriter jsonWrite
356399 jsonWriter .endArray ();
357400 }
358401
359- private static void writeCapabilities (MavenProject project , List <Capability > capabilities ,
360- JsonWriter jsonWriter ) throws IOException {
402+ private static void writeCapabilities (MavenProject project , List <Capability > capabilities , JsonWriter jsonWriter )
403+ throws IOException {
361404 if (capabilities != null && !capabilities .isEmpty ()) {
362405 jsonWriter .name ("capabilities" );
363406 jsonWriter .beginArray ();
0 commit comments