1818 */
1919package org .apache .maven .cling .invoker .mvnup .goals ;
2020
21- import java .io .ByteArrayOutputStream ;
2221import java .io .IOException ;
23- import java .io .OutputStream ;
24- import java .nio .charset .StandardCharsets ;
2522import java .nio .file .Files ;
2623import java .nio .file .Path ;
2724import java .nio .file .Paths ;
2825import java .util .Map ;
2926
27+ import eu .maveniverse .domtrip .Document ;
28+ import eu .maveniverse .domtrip .DomTripException ;
29+ import eu .maveniverse .domtrip .maven .MavenPomElements ;
3030import org .apache .maven .api .cli .mvnup .UpgradeOptions ;
3131import org .apache .maven .api .di .Inject ;
3232import org .apache .maven .cling .invoker .mvnup .Goal ;
3333import org .apache .maven .cling .invoker .mvnup .UpgradeContext ;
34- import org .jdom2 .Document ;
35- import org .jdom2 .JDOMException ;
36- import org .jdom2 .output .Format ;
37- import org .jdom2 .output .XMLOutputter ;
3834
39- import static org . apache . maven .cling . invoker . mvnup . goals . UpgradeConstants .Files .MVN_DIRECTORY ;
40- import static org . apache . maven .cling . invoker . mvnup . goals . UpgradeConstants .ModelVersions .MODEL_VERSION_4_1_0 ;
35+ import static eu . maveniverse . domtrip . maven .MavenPomElements .Files .MVN_DIRECTORY ;
36+ import static eu . maveniverse . domtrip . maven .MavenPomElements .ModelVersions .MODEL_VERSION_4_1_0 ;
4137
4238/**
4339 * Base class for upgrade goals containing shared functionality.
@@ -160,7 +156,7 @@ public int execute(UpgradeContext context) throws Exception {
160156 } else if (options .all ().orElse (false )) {
161157 targetModel = MODEL_VERSION_4_1_0 ;
162158 } else {
163- targetModel = UpgradeConstants .ModelVersions .MODEL_VERSION_4_0_0 ;
159+ targetModel = MavenPomElements .ModelVersions .MODEL_VERSION_4_0_0 ;
164160 }
165161
166162 if (!ModelVersionUtils .isValidModelVersion (targetModel )) {
@@ -176,7 +172,7 @@ public int execute(UpgradeContext context) throws Exception {
176172 Map <Path , Document > pomMap ;
177173 try {
178174 pomMap = PomDiscovery .discoverPoms (startingDirectory );
179- } catch (IOException | JDOMException e ) {
175+ } catch (IOException | DomTripException e ) {
180176 context .failure ("Failed to discover POM files: " + e .getMessage ());
181177 return 1 ;
182178 }
@@ -212,7 +208,7 @@ protected int doUpgrade(UpgradeContext context, String targetModel, Map<Path, Do
212208 // This is needed for both 4.0.0 and 4.1.0 to help Maven find the project root
213209 createMvnDirectoryIfNeeded (context );
214210
215- return result .success () ? 0 : 1 ;
211+ return result .errorPoms (). isEmpty () ? 0 : 1 ;
216212 } catch (Exception e ) {
217213 context .failure ("Strategy execution failed: " + e .getMessage ());
218214 return 1 ;
@@ -226,7 +222,7 @@ protected int doUpgrade(UpgradeContext context, String targetModel, Map<Path, Do
226222 protected abstract boolean shouldSaveModifications ();
227223
228224 /**
229- * Saves the modified documents to disk.
225+ * Saves the modified documents to disk using domtrip's perfect formatting preservation .
230226 */
231227 protected void saveModifications (UpgradeContext context , Map <Path , Document > pomMap ) {
232228 context .info ("" );
@@ -236,27 +232,16 @@ protected void saveModifications(UpgradeContext context, Map<Path, Document> pom
236232 Path pomPath = entry .getKey ();
237233 Document document = entry .getValue ();
238234 try {
239- String content = Files .readString (entry .getKey (), StandardCharsets .UTF_8 );
240- int startIndex = content .indexOf ("<" + document .getRootElement ().getName ());
241- String head = startIndex >= 0 ? content .substring (0 , startIndex ) : "" ;
242- String lastTag = document .getRootElement ().getName () + ">" ;
243- int endIndex = content .lastIndexOf (lastTag );
244- String tail = endIndex >= 0 ? content .substring (endIndex + lastTag .length ()) : "" ;
245- Format format = Format .getRawFormat ();
246- format .setLineSeparator (System .lineSeparator ());
247- XMLOutputter out = new XMLOutputter (format );
248- ByteArrayOutputStream output = new ByteArrayOutputStream ();
249- try (OutputStream outputStream = output ) {
250- outputStream .write (head .getBytes (StandardCharsets .UTF_8 ));
251- out .output (document .getRootElement (), outputStream );
252- outputStream .write (tail .getBytes (StandardCharsets .UTF_8 ));
253- }
254- String newBody = output .toString (StandardCharsets .UTF_8 );
255- Files .writeString (pomPath , newBody , StandardCharsets .UTF_8 );
235+ // Use domtrip for perfect formatting preservation
236+ String xmlContent = DomUtils .toXml (document );
237+ Files .writeString (pomPath , xmlContent );
238+ context .detail ("Saved: " + pomPath );
256239 } catch (Exception e ) {
257240 context .failure ("Failed to save " + pomPath + ": " + e .getMessage ());
258241 }
259242 }
243+
244+ context .success ("All modifications saved successfully" );
260245 }
261246
262247 /**
0 commit comments