diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java index 388c47c4e..ab1c2689c 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java @@ -18,15 +18,12 @@ */ package org.apache.maven.plugins.dependency.fromDependencies; -import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Comparator; import java.util.Iterator; @@ -35,6 +32,8 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.lang3.StringUtils; import org.apache.maven.artifact.Artifact; @@ -307,9 +306,8 @@ private void storeClasspathFile(String cpString, File out) throws MojoExecutionE // make sure the parent path exists. out.getParentFile().mkdirs(); - String encoding = Objects.toString(outputEncoding, "UTF-8"); - - try (Writer w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out), encoding))) { + String encoding = Objects.toString(outputEncoding, StandardCharsets.UTF_8.name()); + try (Writer w = Files.newBufferedWriter(out.toPath(), Charset.forName(encoding))) { w.write(cpString); getLog().info("Wrote classpath file '" + out + "'."); } catch (IOException ex) { @@ -333,15 +331,11 @@ protected String readClasspathFile() throws IOException { if (!outputFile.isFile()) { return null; } - StringBuilder sb = new StringBuilder(); - String encoding = Objects.toString(outputEncoding, "UTF-8"); - try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(outputFile), encoding))) { - for (String line = r.readLine(); line != null; line = r.readLine()) { - sb.append(line); - } + String encoding = Objects.toString(outputEncoding, StandardCharsets.UTF_8.name()); - return sb.toString(); + try (Stream lines = Files.lines(outputFile.toPath(), Charset.forName(encoding))) { + return lines.collect(Collectors.joining()); } } diff --git a/src/main/java/org/apache/maven/plugins/dependency/utils/DependencyUtil.java b/src/main/java/org/apache/maven/plugins/dependency/utils/DependencyUtil.java index 0f58d1b2d..dbb6a654e 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/utils/DependencyUtil.java +++ b/src/main/java/org/apache/maven/plugins/dependency/utils/DependencyUtil.java @@ -20,11 +20,11 @@ import java.io.BufferedReader; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStreamWriter; import java.io.StringReader; import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.file.Files; import java.util.Objects; import org.apache.commons.lang3.StringUtils; @@ -106,15 +106,13 @@ public static String getFormattedFileName( destFileName.append(artifact.getGroupId()).append("."); } - String versionString; + String versionString = ""; if (!removeVersion) { if (useBaseVersion) { versionString = "-" + ArtifactUtils.toSnapshotVersion(artifact.getVersion()); } else { versionString = "-" + artifact.getVersion(); } - } else { - versionString = ""; } String classifierString = ""; @@ -227,9 +225,9 @@ public static synchronized void write(String string, File file, boolean append, */ public static synchronized void write(String string, File file, boolean append, String encoding) throws IOException { - file.getParentFile().mkdirs(); + Files.createDirectories(file.getParentFile().toPath()); - try (Writer writer = new OutputStreamWriter(new FileOutputStream(file, append), encoding)) { + try (Writer writer = Files.newBufferedWriter(file.toPath(), Charset.forName(encoding))) { writer.write(string); } } @@ -242,15 +240,9 @@ public static synchronized void write(String string, File file, boolean append, * @throws IOException if an I/O error occurs */ public static synchronized void log(String string, Log log) throws IOException { - BufferedReader reader = new BufferedReader(new StringReader(string)); - - String line; - - while ((line = reader.readLine()) != null) { - log.info(line); + try (BufferedReader reader = new BufferedReader(new StringReader(string))) { + reader.lines().forEach(log::info); } - - reader.close(); } /** @@ -273,7 +265,7 @@ public static String cleanToBeTokenizedString(String str) { String ret = ""; if (!(str == null || str.isEmpty())) { // remove initial and ending spaces, plus all spaces next to commas - ret = str.trim().replaceAll("[\\s]*,[\\s]*", ","); + ret = str.trim().replaceAll("\\s*,\\s*", ","); } return ret;