Skip to content

Commit

Permalink
Preserve POM format when extensions are added/removed
Browse files Browse the repository at this point in the history
It also bumps maven-model-helper to 34.

- Fixes #39088
  • Loading branch information
gastaldi committed Mar 19, 2024
1 parent 1c34992 commit 0a3b45c
Show file tree
Hide file tree
Showing 21 changed files with 760 additions and 686 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ public void execute() throws MojoExecutionException {
parent.setArtifactId(parentPomModel.getArtifactId());
parent.setVersion(parentPomModel.getVersion());
subModulePomModel.setParent(parent);
MojoUtils.write(parentPomModel, pom);
MojoUtils.write(subModulePomModel, subModulePomFile);
MojoUtils.writeFormatted(parentPomModel, pom);
MojoUtils.writeFormatted(subModulePomModel, subModulePomFile);
}
} catch (Exception e) {
throw new MojoExecutionException("Failed to generate Quarkus project", e);
Expand Down
2 changes: 1 addition & 1 deletion docs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<asciidoctorj-pdf.version>1.5.0-beta.8</asciidoctorj-pdf.version>
<asciidoctor.fail-if>WARN</asciidoctor.fail-if>
<roaster-jdt.version>2.26.0.Final</roaster-jdt.version>
<maven-model-helper.version>28</maven-model-helper.version>
<maven-model-helper.version>34</maven-model-helper.version>
<eclipse-collections.version>11.1.0</eclipse-collections.version>
<jgit.version>6.9.0.202403050737-r</jgit.version>

Expand Down
5 changes: 5 additions & 0 deletions independent-projects/bootstrap/bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>maven-model-helper</artifactId>
<version>${maven-model-helper.version}</version>
</dependency>
<!-- Smallrye Common dependencies, imported as a BOM -->
<dependency>
<groupId>io.smallrye.common</groupId>
Expand Down
4 changes: 4 additions & 0 deletions independent-projects/bootstrap/maven-resolver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>maven-model-helper</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.quarkus.bootstrap.resolver.maven.workspace;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.Files;
Expand All @@ -16,10 +16,9 @@

import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import io.fabric8.maven.Maven;
import io.fabric8.maven.XMLFormat;
import io.quarkus.bootstrap.util.PropertyUtils;
import io.quarkus.fs.util.ZipUtils;
import io.quarkus.maven.dependency.ArtifactCoords;
Expand All @@ -28,7 +27,6 @@
import io.quarkus.maven.dependency.ResolvedDependencyBuilder;

/**
*
* @author Alexey Loubyansky
*/
public class ModelUtils {
Expand Down Expand Up @@ -182,7 +180,7 @@ public static String resolveVersion(String rawVersion, Model rawModel) {
putAll(props, System.getProperties());

Matcher matcher = getUnresolvedVersionPattern().matcher(rawVersion);
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
while (matcher.find()) {
final String resolved = props.get(matcher.group(1));
if (resolved == null) {
Expand Down Expand Up @@ -233,21 +231,30 @@ private static Properties loadPomProps(Path appJar, Path artifactIdPath) throws
}

public static Model readModel(final Path pomXml) throws IOException {
return readModel(Files.newInputStream(pomXml));
try {
return Maven.readModel(pomXml);
} catch (UncheckedIOException e) {
throw e.getCause();
} catch (RuntimeException e) {
throw new IOException("Failed to read model", e.getCause());
}
}

public static Model readModel(InputStream stream) throws IOException {
try (InputStream is = stream) {
return new MavenXpp3Reader().read(stream);
} catch (XmlPullParserException e) {
throw new IOException("Failed to parse POM", e);
return Maven.readModel(is);
} catch (UncheckedIOException e) {
throw e.getCause();
} catch (RuntimeException e) {
throw new IOException("Failed to read model", e.getCause());
}
}

public static void persistModel(Path pomFile, Model model) throws IOException {
final MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
try (BufferedWriter pomFileWriter = Files.newBufferedWriter(pomFile)) {
xpp3Writer.write(pomFileWriter, model);
try {
Maven.writeModel(model, pomFile, XMLFormat.builder().indent(" ").insertLineBreakBetweenMajorSections().build());
} catch (UncheckedIOException e) {
throw e.getCause();
}
}
}
1 change: 1 addition & 0 deletions independent-projects/bootstrap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<org-crac.version>0.1.3</org-crac.version>
<formatter-maven-plugin.version>2.23.0</formatter-maven-plugin.version>
<impsort-maven-plugin.version>1.9.0</impsort-maven-plugin.version>
<maven-model-helper.version>34</maven-model-helper.version>
</properties>
<modules>
<module>bom</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.maven.model.Model;

import io.fabric8.maven.Maven;
import io.fabric8.maven.XMLFormat;
import io.fabric8.maven.merge.SmartModelMerger;
import io.quarkus.devtools.codestarts.CodestartException;
import io.quarkus.devtools.codestarts.core.CodestartData;
Expand Down Expand Up @@ -41,6 +42,7 @@ public void process(Path targetDirectory, String relativePath, List<TargetFile>
while (iterator.hasNext()) {
merger.merge(targetModel, Maven.readModel(new StringReader(iterator.next().getContent())), true, null);
}
Maven.writeModel(targetModel, targetPath);
Maven.writeModel(targetModel, targetPath,
XMLFormat.builder().indent(" ").insertLineBreakBetweenMajorSections().build());
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.toto</groupId>
<artifactId>quarkus-project</artifactId>
<version>1.2.3</version>
<properties>
<abc>123</abc>
<fruit>pineapple</fruit>
<prop1>proppp</prop1>
<prop2>prop-2-namespaced</prop2>
<zebra>starts-with-z</zebra>
</properties>
<dependencies>
<dependency>
<groupId>io.b</groupId>
<artifactId>b</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.toto</groupId>
<artifactId>quarkus-project</artifactId>
<version>1.2.3</version>

<properties>
<abc>123</abc>
<fruit>pineapple</fruit>
<prop1>proppp</prop1>
<prop2>prop-2-namespaced</prop2>
<zebra>starts-with-z</zebra>
</properties>

<dependencies>
<dependency>
<groupId>io.b</groupId>
<artifactId>b</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

import org.apache.maven.cli.transfer.QuietMavenTransferListener;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.codehaus.plexus.util.WriterFactory;
import org.codehaus.plexus.util.xml.XmlStreamWriter;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession;

import io.fabric8.maven.Maven;
import io.fabric8.maven.XMLFormat;

/**
* @author kameshs
*/
Expand Down Expand Up @@ -161,14 +166,22 @@ public static Plugin plugin(String groupId, String artifactId, String version, L
}

public static Model readPom(final File pom) throws IOException {
return readPom(new FileInputStream(pom));
try {
return Maven.readModel(pom.toPath());
} catch (UncheckedIOException e) {
throw e.getCause();
} catch (RuntimeException e) {
throw new IOException("Failed to read model", e.getCause());
}
}

public static Model readPom(final InputStream resourceAsStream) throws IOException {
try (InputStream stream = resourceAsStream) {
return new MavenXpp3Reader().read(stream);
} catch (XmlPullParserException e) {
throw new IOException(e.getMessage(), e);
try (InputStream is = resourceAsStream) {
return Maven.readModel(is);
} catch (UncheckedIOException e) {
throw e.getCause();
} catch (RuntimeException e) {
throw new IOException("Failed to read model", e.getCause());
}
}

Expand All @@ -178,8 +191,20 @@ public static String[] readGavFromPom(final InputStream resourceAsStream) throws
}

public static void write(Model model, File outputFile) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
write(model, fileOutputStream);
try {
Maven.writeModel(model, outputFile.toPath());
} catch (UncheckedIOException e) {
throw e.getCause();
}
}

public static void writeFormatted(Model model, File outputFile) throws IOException {
try {
Maven.writeModel(model, outputFile.toPath(),
XMLFormat.builder().indent(" ").insertLineBreakBetweenMajorSections().build());
} catch (UncheckedIOException e) {
throw e.getCause();
}
}

public static void write(Model model, OutputStream fileOutputStream) throws IOException {
Expand All @@ -190,8 +215,10 @@ public static void write(Model model, OutputStream fileOutputStream) throws IOEx
sorted.putAll(props);
model.setProperties(sorted);
}
try (OutputStream stream = fileOutputStream) {
new MavenXpp3Writer().write(stream, model);
try (XmlStreamWriter writer = WriterFactory.newXmlWriter(fileOutputStream)) {
Maven.writeModel(model, writer);
} catch (UncheckedIOException e) {
throw e.getCause();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ void generateMavenWithCustomDep(TestInfo testInfo) throws Throwable {
checkMaven(projectDir);
assertThatMatchSnapshot(testInfo, projectDir, "pom.xml")
.satisfies(checkContains("<dependency>\n" +
" <groupId>commons-io</groupId>\n" +
" <artifactId>commons-io</artifactId>\n" +
" <version>2.5</version>\n" +
" </dependency>\n"))
" <groupId>io.quarkus</groupId>\n" +
" <artifactId>quarkus-resteasy</artifactId>\n" +
" <version>1.8</version>\n" +
" </dependency>"))
.satisfies(checkContains("<dependency>\n" +
" <groupId>io.quarkus</groupId>\n" +
" <artifactId>quarkus-resteasy</artifactId>\n" +
" <version>1.8</version>\n" +
" </dependency>\n"));
" <groupId>io.quarkus</groupId>\n" +
" <artifactId>quarkus-resteasy</artifactId>\n" +
" <version>1.8</version>\n" +
" </dependency>"));
}

@Test
Expand Down
Loading

0 comments on commit 0a3b45c

Please sign in to comment.