This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-project-info-reports-plugin.git
The following commit(s) were added to refs/heads/master by this push: new bf4a3d2 Switch to modern I/O (#91) bf4a3d2 is described below commit bf4a3d21c5d6859564cd4c19d4e747d0358370b1 Author: Elliotte Rusty Harold <elh...@users.noreply.github.com> AuthorDate: Wed Dec 18 12:16:43 2024 +0000 Switch to modern I/O (#91) --- .../report/projectinfo/DependenciesReport.java | 54 ++++++----------- .../projectinfo/DependencyConvergenceReport.java | 70 +++++++++++----------- .../report/projectinfo/ProjectInfoReportUtils.java | 34 ++--------- .../projectinfo/stubs/ProjectInfoProjectStub.java | 15 ++--- 4 files changed, 62 insertions(+), 111 deletions(-) diff --git a/src/main/java/org/apache/maven/report/projectinfo/DependenciesReport.java b/src/main/java/org/apache/maven/report/projectinfo/DependenciesReport.java index d6f246a..6fe3a43 100644 --- a/src/main/java/org/apache/maven/report/projectinfo/DependenciesReport.java +++ b/src/main/java/org/apache/maven/report/projectinfo/DependenciesReport.java @@ -29,6 +29,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.Locale; import org.apache.maven.artifact.Artifact; @@ -54,7 +55,6 @@ import org.apache.maven.shared.jar.classes.JarClassesAnalysis; import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver; import org.codehaus.plexus.i18n.I18N; import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.ReaderFactory; /** * Generates the Project Dependencies report. @@ -223,45 +223,27 @@ public class DependenciesReport extends AbstractProjectInfoReport { * @throws IOException if any */ private void copyResources(File outputDirectory) throws IOException { - InputStream resourceList = null; - InputStream in = null; - BufferedReader reader = null; - OutputStream out = null; - try { - resourceList = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/resources.txt"); - - if (resourceList != null) { - reader = new LineNumberReader(new InputStreamReader(resourceList, ReaderFactory.US_ASCII)); - + InputStream resourceList = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/resources.txt"); + if (resourceList != null) { + try (BufferedReader reader = + new LineNumberReader(new InputStreamReader(resourceList, StandardCharsets.US_ASCII))) { for (String line = reader.readLine(); line != null; line = reader.readLine()) { - in = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/" + line); - - if (in == null) { - throw new IOException("The resource " + line + " doesn't exist."); + try (InputStream in = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/" + line)) { + if (in == null) { + throw new IOException("The resource " + line + " doesn't exist."); + } + + File outputFile = new File(outputDirectory, line); + if (!outputFile.getParentFile().exists()) { + outputFile.getParentFile().mkdirs(); + } + + try (OutputStream out = new FileOutputStream(outputFile)) { + IOUtil.copy(in, out); + } } - - File outputFile = new File(outputDirectory, line); - - if (!outputFile.getParentFile().exists()) { - outputFile.getParentFile().mkdirs(); - } - - out = new FileOutputStream(outputFile); - IOUtil.copy(in, out); - out.close(); - out = null; - in.close(); - in = null; } - - reader.close(); - reader = null; } - } finally { - IOUtil.close(out); - IOUtil.close(reader); - IOUtil.close(in); - IOUtil.close(resourceList); } } } diff --git a/src/main/java/org/apache/maven/report/projectinfo/DependencyConvergenceReport.java b/src/main/java/org/apache/maven/report/projectinfo/DependencyConvergenceReport.java index e5edfd4..14973e5 100644 --- a/src/main/java/org/apache/maven/report/projectinfo/DependencyConvergenceReport.java +++ b/src/main/java/org/apache/maven/report/projectinfo/DependencyConvergenceReport.java @@ -131,56 +131,54 @@ public class DependencyConvergenceReport extends AbstractProjectInfoReport { @Override protected void executeReport(Locale locale) throws MavenReportException { - Sink sink = getSink(); + try (Sink sink = getSink()) { + sink.head(); + sink.title(); - sink.head(); - sink.title(); + if (isReactorBuild()) { + sink.text(getI18nString(locale, "reactor.title")); + } else { + sink.text(getI18nString(locale, "title")); + } - if (isReactorBuild()) { - sink.text(getI18nString(locale, "reactor.title")); - } else { - sink.text(getI18nString(locale, "title")); - } + sink.title_(); + sink.head_(); - sink.title_(); - sink.head_(); + sink.body(); - sink.body(); + sink.section1(); - sink.section1(); + sink.sectionTitle1(); - sink.sectionTitle1(); + if (isReactorBuild()) { + sink.text(getI18nString(locale, "reactor.title")); + } else { + sink.text(getI18nString(locale, "title")); + } - if (isReactorBuild()) { - sink.text(getI18nString(locale, "reactor.title")); - } else { - sink.text(getI18nString(locale, "title")); - } + sink.sectionTitle1_(); - sink.sectionTitle1_(); + DependencyAnalyzeResult dependencyResult = analyzeDependencyTree(); + int convergence = calculateConvergence(dependencyResult); - DependencyAnalyzeResult dependencyResult = analyzeDependencyTree(); - int convergence = calculateConvergence(dependencyResult); + if (convergence < FULL_CONVERGENCE) { + // legend + generateLegend(locale, sink); + sink.lineBreak(); + } - if (convergence < FULL_CONVERGENCE) { - // legend - generateLegend(locale, sink); - sink.lineBreak(); - } + // stats + generateStats(locale, sink, dependencyResult); - // stats - generateStats(locale, sink, dependencyResult); + sink.section1_(); - sink.section1_(); + if (convergence < FULL_CONVERGENCE) { + // convergence + generateConvergence(locale, sink, dependencyResult); + } - if (convergence < FULL_CONVERGENCE) { - // convergence - generateConvergence(locale, sink, dependencyResult); + sink.body_(); } - - sink.body_(); - sink.flush(); - sink.close(); } // ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/maven/report/projectinfo/ProjectInfoReportUtils.java b/src/main/java/org/apache/maven/report/projectinfo/ProjectInfoReportUtils.java index 40c83d8..54e49cf 100644 --- a/src/main/java/org/apache/maven/report/projectinfo/ProjectInfoReportUtils.java +++ b/src/main/java/org/apache/maven/report/projectinfo/ProjectInfoReportUtils.java @@ -70,9 +70,6 @@ public class ProjectInfoReportUtils { /** The timeout when getting the url input stream */ private static final int TIMEOUT = 1000 * 5; - /** The default encoding used to transform bytes to characters */ - private static final String DEFAULT_ENCODING = "UTF-8"; - /** * Get the input stream using UTF-8 as character encoding from a URL. * @@ -83,7 +80,7 @@ public class ProjectInfoReportUtils { * @see #getContent(URL, Settings, String) */ public static String getContent(URL url, Settings settings) throws IOException { - return getContent(url, settings, DEFAULT_ENCODING); + return getContent(url, settings, "UTF-8"); } /** @@ -115,23 +112,13 @@ public class ProjectInfoReportUtils { String scheme = url.getProtocol(); if (encoding == null || encoding.isEmpty()) { - encoding = DEFAULT_ENCODING; + encoding = "UTF-8"; } if ("file".equals(scheme)) { - InputStream in = null; - try { - URLConnection conn = url.openConnection(); - in = conn.getInputStream(); - + try (InputStream in = url.openConnection().getInputStream()) { final String content = IOUtil.toString(in, encoding); - - in.close(); - in = null; - return content; - } finally { - IOUtil.close(in); } } @@ -167,19 +154,8 @@ public class ProjectInfoReportUtils { } } - InputStream in = null; - try { - URLConnection conn = getURLConnection(url, project, settings); - in = conn.getInputStream(); - - final String string = IOUtil.toString(in, encoding); - - in.close(); - in = null; - - return string; - } finally { - IOUtil.close(in); + try (InputStream in = getURLConnection(url, project, settings).getInputStream()) { + return IOUtil.toString(in, encoding); } } diff --git a/src/test/java/org/apache/maven/report/projectinfo/stubs/ProjectInfoProjectStub.java b/src/test/java/org/apache/maven/report/projectinfo/stubs/ProjectInfoProjectStub.java index 3bf5a8b..92143a2 100644 --- a/src/test/java/org/apache/maven/report/projectinfo/stubs/ProjectInfoProjectStub.java +++ b/src/test/java/org/apache/maven/report/projectinfo/stubs/ProjectInfoProjectStub.java @@ -19,6 +19,8 @@ package org.apache.maven.report.projectinfo.stubs; import java.io.File; +import java.io.IOException; +import java.io.Reader; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -40,9 +42,8 @@ import org.apache.maven.model.Model; import org.apache.maven.model.PluginManagement; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.plugin.testing.stubs.MavenProjectStub; -import org.apache.maven.shared.utils.io.IOUtil; import org.codehaus.plexus.util.ReaderFactory; -import org.codehaus.plexus.util.xml.XmlStreamReader; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.eclipse.aether.repository.RemoteRepository; /** @@ -61,17 +62,11 @@ public abstract class ProjectInfoProjectStub extends MavenProjectStub { */ public ProjectInfoProjectStub() { MavenXpp3Reader pomReader = new MavenXpp3Reader(); - XmlStreamReader reader = null; - try { - reader = ReaderFactory.newXmlReader(new File(getBasedir(), getPOM())); + try (Reader reader = ReaderFactory.newXmlReader(new File(getBasedir(), getPOM()))) { model = pomReader.read(reader); - reader.close(); - reader = null; setModel(model); - } catch (Exception e) { + } catch (IOException | XmlPullParserException e) { throw new RuntimeException(e); - } finally { - IOUtil.close(reader); } setGroupId(model.getGroupId());