1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cling.invoker.mvnup.goals;
20
21 import java.nio.file.Path;
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25
26 import eu.maveniverse.domtrip.Document;
27 import eu.maveniverse.domtrip.Element;
28 import eu.maveniverse.domtrip.maven.Coordinates;
29 import eu.maveniverse.domtrip.maven.MavenPomElements;
30 import org.apache.maven.api.cli.mvnup.UpgradeOptions;
31 import org.apache.maven.cling.invoker.mvnup.UpgradeContext;
32
33 import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PARENT;
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public abstract class AbstractUpgradeStrategy implements UpgradeStrategy {
48
49
50
51
52
53 @Override
54 public final UpgradeResult apply(UpgradeContext context, Map<Path, Document> pomMap) {
55 context.info(getDescription());
56 context.indent();
57
58 try {
59 UpgradeResult result = doApply(context, pomMap);
60
61
62 logSummary(context, result);
63
64 return result;
65 } catch (Exception e) {
66 context.failure("Strategy execution failed: " + e.getMessage());
67 return UpgradeResult.failure(pomMap.keySet(), Set.of());
68 } finally {
69 context.unindent();
70 }
71 }
72
73
74
75
76
77
78
79
80 protected abstract UpgradeResult doApply(UpgradeContext context, Map<Path, Document> pomMap);
81
82
83
84
85
86
87
88 protected final UpgradeOptions getOptions(UpgradeContext context) {
89 return context.options();
90 }
91
92
93
94
95
96
97
98 protected void logSummary(UpgradeContext context, UpgradeResult result) {
99 context.println();
100 context.info(getDescription() + " Summary:");
101 context.indent();
102 context.info(result.modifiedCount() + " POM(s) modified");
103 context.info(result.unmodifiedCount() + " POM(s) needed no changes");
104 if (result.errorCount() > 0) {
105 context.info(result.errorCount() + " POM(s) had errors");
106 }
107 context.unindent();
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121 public static Coordinates extractArtifactCoordinatesWithParentResolution(
122 UpgradeContext context, Document pomDocument) {
123 Element root = pomDocument.root();
124
125
126 String groupId = root.childTextTrimmed(MavenPomElements.Elements.GROUP_ID);
127 String artifactId = root.childTextTrimmed(MavenPomElements.Elements.ARTIFACT_ID);
128 String version = root.childTextTrimmed(MavenPomElements.Elements.VERSION);
129
130
131 if (groupId == null || version == null) {
132 Element parentElement = root.child(PARENT).orElse(null);
133 if (parentElement != null) {
134 if (groupId == null) {
135 groupId = parentElement.childTextTrimmed(MavenPomElements.Elements.GROUP_ID);
136 }
137 if (version == null) {
138 version = parentElement.childTextTrimmed(MavenPomElements.Elements.VERSION);
139 }
140 }
141 }
142
143
144 if (artifactId == null || artifactId.isEmpty()) {
145 context.debug("Cannot determine artifactId for POM");
146 return null;
147 }
148
149
150 if (groupId == null || groupId.isEmpty() || version == null || version.isEmpty()) {
151 context.debug("Cannot determine complete GAV for artifactId: " + artifactId);
152 return null;
153 }
154
155 return Coordinates.of(groupId, artifactId, version);
156 }
157
158
159
160
161
162
163
164
165
166 public static Set<Coordinates> computeAllArtifactCoordinates(UpgradeContext context, Map<Path, Document> pomMap) {
167 Set<Coordinates> coordinates = new HashSet<>();
168
169 context.info("Computing artifacts for inference from " + pomMap.size() + " POM(s)...");
170
171
172 for (Map.Entry<Path, Document> entry : pomMap.entrySet()) {
173 Path pomPath = entry.getKey();
174 Document pomDocument = entry.getValue();
175
176 Coordinates coordinate =
177 AbstractUpgradeStrategy.extractArtifactCoordinatesWithParentResolution(context, pomDocument);
178 if (coordinate != null) {
179 coordinates.add(coordinate);
180 context.debug("Found artifact: " + coordinate.toGAV() + " from " + pomPath);
181 }
182 }
183
184 context.info("Computed " + coordinates.size() + " unique artifact(s) for inference");
185 return coordinates;
186 }
187 }