Skip to content

Commit

Permalink
Resolves #931: Change the way to read the raw POM file into String to…
Browse files Browse the repository at this point in the history
… circumvent problems with encoding
  • Loading branch information
andrzejj0 committed Mar 14, 2023
1 parent 7ed5bd4 commit ff62d6f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1364,9 +1365,7 @@ private static List<ModelNode> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
* under the License.
*/

import java.io.BufferedReader;
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;
Expand Down Expand Up @@ -150,4 +154,18 @@ public static Dependency interpolateVersion(final Dependency dependency, final M
}
return dependency;
}

/**
* Reads the given file to a StringBuilder
* @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 (BufferedReader reader = Files.newBufferedReader(path)) {
return reader.lines().collect(StringBuilder::new, StringBuilder::append, StringBuilder::append);
} catch (IOException e) {
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>test-group</groupId>
<artifactId>test-artifact</artifactId>
<version>DEVELOP-SNAPSHOT</version>

<description>Wörter mit Umlauten</description>
<!-- Vorläufige Container für Tests -->

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -136,10 +136,10 @@ protected void update(ModifiedPomXMLEventReader pom)
public void execute() throws MojoExecutionException, MojoFailureException {
validateInput();
List<ModelNode> 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());
Expand Down

0 comments on commit ff62d6f

Please sign in to comment.