From b881d37713468c4e4ad3dfc566a0ba9f58b7d0ca Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Tue, 14 Mar 2023 13:46:12 +0100 Subject: [PATCH 1/2] Resolves #931: Change the way to read the raw POM file into String to circumvent problems with encoding Uses Plexus XmlStreamReader to guess file encoding. --- .../codehaus/mojo/versions/api/PomHelper.java | 5 ++--- .../versions/utils/MavenProjectUtils.java | 16 ++++++++++++++++ .../invoker.properties | 2 ++ .../pom.xml | 19 +++++++++++++++++++ .../verify.groovy | 5 +++++ .../invoker.properties | 2 ++ .../pom.xml | 19 +++++++++++++++++++ .../verify.groovy | 5 +++++ .../mojo/versions/UseDepVersionMojo.java | 6 +++--- 9 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/invoker.properties create mode 100644 versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/pom.xml create mode 100644 versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/verify.groovy create mode 100644 versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/invoker.properties create mode 100644 versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/pom.xml create mode 100644 versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/verify.groovy diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java index 974239a94e..ada3229962 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java @@ -71,6 +71,7 @@ import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingResult; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import org.codehaus.mojo.versions.utils.MavenProjectUtils; import org.codehaus.mojo.versions.utils.ModelNode; import org.codehaus.mojo.versions.utils.RegexUtils; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; @@ -1364,9 +1365,7 @@ private static List getRawModelTree(ModelNode rootNode, Log logger, X .map(pomFile -> { try { ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader( - new StringBuilder(new String(Files.readAllBytes(pomFile))), - inputFactory, - pomFile.toString()); + MavenProjectUtils.readFile(pomFile), inputFactory, pomFile.toString()); return new ModelNode(rootNode, getRawModel(pom), pom); } catch (IOException e) { throw new UncheckedIOException("Could not open " + pomFile, e); diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/utils/MavenProjectUtils.java b/versions-common/src/main/java/org/codehaus/mojo/versions/utils/MavenProjectUtils.java index 730ee44edb..519c7dd155 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/utils/MavenProjectUtils.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/utils/MavenProjectUtils.java @@ -18,6 +18,9 @@ * under the License. */ +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.Set; import java.util.TreeSet; @@ -29,6 +32,7 @@ import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.codehaus.mojo.versions.api.VersionRetrievalException; +import org.codehaus.plexus.util.xml.XmlStreamReader; import static java.util.Collections.emptyList; import static java.util.Optional.ofNullable; @@ -150,4 +154,16 @@ public static Dependency interpolateVersion(final Dependency dependency, final M } return dependency; } + + /** + * Reads the given file to a StringBuilder using {@link XmlStreamReader} to determine the file encoding. + * @param path file path + * @return StringBuilder containing the given file + * @throws IOException thrown in case of an I/O error + */ + public static StringBuilder readFile(Path path) throws IOException { + try (XmlStreamReader reader = new XmlStreamReader(path.toFile())) { + return new StringBuilder(new String(Files.readAllBytes(path), reader.getEncoding())); + } + } } diff --git a/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/invoker.properties b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/invoker.properties new file mode 100644 index 0000000000..eea1477f0e --- /dev/null +++ b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:use-dep-version +invoker.mavenOpts = -Dfile.encoding=latin1 -Dincludes=localhost -DdepVersion=1.0.1 diff --git a/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/pom.xml b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/pom.xml new file mode 100644 index 0000000000..4ce5ded45c --- /dev/null +++ b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/pom.xml @@ -0,0 +1,19 @@ + + + 4.0.0 + + test-group + test-artifact + DEVELOP-SNAPSHOT + + Wörter mit Umlauten + + + + + localhost + dummy-api + 1.0 + + + diff --git a/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/verify.groovy b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/verify.groovy new file mode 100644 index 0000000000..a057a3c630 --- /dev/null +++ b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts-iso8859-1/verify.groovy @@ -0,0 +1,5 @@ +import groovy.xml.XmlSlurper + +def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) ) +assert project.dependencies.dependency.version == '1.0.1' +assert project.description == 'Wörter mit Umlauten' diff --git a/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/invoker.properties b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/invoker.properties new file mode 100644 index 0000000000..eea1477f0e --- /dev/null +++ b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:use-dep-version +invoker.mavenOpts = -Dfile.encoding=latin1 -Dincludes=localhost -DdepVersion=1.0.1 diff --git a/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/pom.xml b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/pom.xml new file mode 100644 index 0000000000..76e36d2fd9 --- /dev/null +++ b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/pom.xml @@ -0,0 +1,19 @@ + + + 4.0.0 + + test-group + test-artifact + DEVELOP-SNAPSHOT + + Wörter mit Umlauten + + + + + localhost + dummy-api + 1.0 + + + diff --git a/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/verify.groovy b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/verify.groovy new file mode 100644 index 0000000000..a057a3c630 --- /dev/null +++ b/versions-maven-plugin/src/it/it-use-dep-version-issue-931-umlauts/verify.groovy @@ -0,0 +1,5 @@ +import groovy.xml.XmlSlurper + +def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) ) +assert project.dependencies.dependency.version == '1.0.1' +assert project.description == 'Wörter mit Umlauten' diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseDepVersionMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseDepVersionMojo.java index 6a8e35f4fd..107429ca06 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseDepVersionMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseDepVersionMojo.java @@ -20,7 +20,6 @@ import java.io.File; import java.io.IOException; -import java.nio.file.Files; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -54,6 +53,7 @@ import org.codehaus.mojo.versions.recording.DefaultPropertyChangeRecord; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.DependencyComparator; +import org.codehaus.mojo.versions.utils.MavenProjectUtils; import org.codehaus.mojo.versions.utils.ModelNode; import org.codehaus.plexus.util.FileUtils; @@ -136,10 +136,10 @@ protected void update(ModifiedPomXMLEventReader pom) public void execute() throws MojoExecutionException, MojoFailureException { validateInput(); List rawModels; + try { ModifiedPomXMLEventReader pomReader = newModifiedPomXER( - new StringBuilder( - new String(Files.readAllBytes(getProject().getFile().toPath()))), + MavenProjectUtils.readFile(getProject().getFile().toPath()), getProject().getFile().toPath().toString()); ModelNode rootNode = new ModelNode(PomHelper.getRawModel(pomReader), pomReader); rawModels = PomHelper.getRawModelTree(rootNode, getLog()); From 919dbe4310c0427e8e7b7c9c8cd8c49d57065b97 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sun, 14 May 2023 17:08:49 +0200 Subject: [PATCH 2/2] Use PomHelper.readXmlFile --- .../codehaus/mojo/versions/api/PomHelper.java | 3 +-- .../mojo/versions/utils/MavenProjectUtils.java | 16 ---------------- .../mojo/versions/UseDepVersionMojo.java | 3 +-- 3 files changed, 2 insertions(+), 20 deletions(-) diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java index ada3229962..6ab8b56d10 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java @@ -71,7 +71,6 @@ import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingResult; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; -import org.codehaus.mojo.versions.utils.MavenProjectUtils; import org.codehaus.mojo.versions.utils.ModelNode; import org.codehaus.mojo.versions.utils.RegexUtils; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; @@ -1365,7 +1364,7 @@ private static List getRawModelTree(ModelNode rootNode, Log logger, X .map(pomFile -> { try { ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader( - MavenProjectUtils.readFile(pomFile), inputFactory, pomFile.toString()); + readXmlFile(pomFile.toFile()), inputFactory, pomFile.toString()); return new ModelNode(rootNode, getRawModel(pom), pom); } catch (IOException e) { throw new UncheckedIOException("Could not open " + pomFile, e); diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/utils/MavenProjectUtils.java b/versions-common/src/main/java/org/codehaus/mojo/versions/utils/MavenProjectUtils.java index 519c7dd155..730ee44edb 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/utils/MavenProjectUtils.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/utils/MavenProjectUtils.java @@ -18,9 +18,6 @@ * under the License. */ -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.List; import java.util.Set; import java.util.TreeSet; @@ -32,7 +29,6 @@ import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.codehaus.mojo.versions.api.VersionRetrievalException; -import org.codehaus.plexus.util.xml.XmlStreamReader; import static java.util.Collections.emptyList; import static java.util.Optional.ofNullable; @@ -154,16 +150,4 @@ public static Dependency interpolateVersion(final Dependency dependency, final M } return dependency; } - - /** - * Reads the given file to a StringBuilder using {@link XmlStreamReader} to determine the file encoding. - * @param path file path - * @return StringBuilder containing the given file - * @throws IOException thrown in case of an I/O error - */ - public static StringBuilder readFile(Path path) throws IOException { - try (XmlStreamReader reader = new XmlStreamReader(path.toFile())) { - return new StringBuilder(new String(Files.readAllBytes(path), reader.getEncoding())); - } - } } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseDepVersionMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseDepVersionMojo.java index 107429ca06..33b47ed3c5 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseDepVersionMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseDepVersionMojo.java @@ -53,7 +53,6 @@ import org.codehaus.mojo.versions.recording.DefaultPropertyChangeRecord; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.DependencyComparator; -import org.codehaus.mojo.versions.utils.MavenProjectUtils; import org.codehaus.mojo.versions.utils.ModelNode; import org.codehaus.plexus.util.FileUtils; @@ -139,7 +138,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { try { ModifiedPomXMLEventReader pomReader = newModifiedPomXER( - MavenProjectUtils.readFile(getProject().getFile().toPath()), + PomHelper.readXmlFile(getProject().getFile()), getProject().getFile().toPath().toString()); ModelNode rootNode = new ModelNode(PomHelper.getRawModel(pomReader), pomReader); rawModels = PomHelper.getRawModelTree(rootNode, getLog());