From 8cc4c41fd332bfefe8809a501759e75733486e42 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Thu, 19 Dec 2024 00:12:51 +0100 Subject: [PATCH] Avoid using deprecated API --- .../AbstractProcessRemoteResourcesMojo.java | 46 ++++++------------- .../remote/BundleRemoteResourcesMojo.java | 8 ++-- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java b/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java index 6767b0f..c334a02 100644 --- a/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java +++ b/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java @@ -24,7 +24,6 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; @@ -33,6 +32,7 @@ import java.io.Writer; import java.net.MalformedURLException; import java.net.URL; +import java.nio.charset.Charset; import java.nio.file.Files; import java.time.Instant; import java.time.ZoneId; @@ -93,9 +93,7 @@ import org.codehaus.plexus.resource.loader.FileResourceLoader; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.StringUtils; -import org.codehaus.plexus.util.WriterFactory; import org.codehaus.plexus.util.io.CachingOutputStream; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -395,8 +393,9 @@ public void execute() throws MojoExecutionException { } if (encoding == null || encoding.isEmpty()) { - getLog().warn("File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING + getLog().warn("File encoding has not been set, using platform encoding " + Charset.defaultCharset() + ", i.e. build is platform dependent!"); + encoding = Charset.defaultCharset().name(); } if (resolveScopes == null) { @@ -510,8 +509,7 @@ protected List getProjects() { // add filters in well known order, least specific to most specific FilterArtifacts filter = new FilterArtifacts(); - Set artifacts = new LinkedHashSet<>(); - artifacts.addAll(getAllDependencies()); + Set artifacts = new LinkedHashSet<>(getAllDependencies()); if (this.excludeTransitive) { filter.addFilter(new ProjectTransitivityFilter(getDirectDependencies(), true)); } @@ -660,19 +658,11 @@ protected boolean copyResourceIfExists(File file, String relFileName, VelocityCo } private Reader getReader(File source) throws IOException { - if (encoding != null) { - return new InputStreamReader(Files.newInputStream(source.toPath()), encoding); - } else { - return ReaderFactory.newPlatformReader(source); - } + return Files.newBufferedReader(source.toPath(), Charset.forName(encoding)); } private Writer getWriter(OutputStream os) throws IOException { - if (encoding != null) { - return new OutputStreamWriter(os, encoding); - } else { - return WriterFactory.newPlatformWriter(os); - } + return new OutputStreamWriter(os, encoding); } private MavenFileFilterRequest setupRequest(Resource resource, File source, File file) { @@ -685,9 +675,7 @@ private MavenFileFilterRequest setupRequest(Resource resource, File source, File req.setMavenSession(mavenSession); req.setInjectProjectBuildFilters(true); - if (encoding != null) { - req.setEncoding(encoding); - } + req.setEncoding(encoding); if (filterDelimiters != null && !filterDelimiters.isEmpty()) { LinkedHashSet delims = new LinkedHashSet<>(); @@ -918,16 +906,12 @@ protected void processResourceBundles(ClassLoader classLoader, VelocityContext c if (!copyResourceIfExists(f, projectResource, context)) { if (doVelocity) { try (CachingOutputStream os = new CachingOutputStream(f)) { - try (Writer writer = bundle.getSourceEncoding() == null - ? new OutputStreamWriter(os) - : new OutputStreamWriter(os, bundle.getSourceEncoding())) { - if (bundle.getSourceEncoding() == null) { - // TODO: Is this correct? Shouldn't we behave like the rest of maven and fail - // down to JVM default instead ISO-8859-1 ? - velocity.mergeTemplate(bundleResource, "ISO-8859-1", context, writer); - } else { - velocity.mergeTemplate(bundleResource, bundle.getSourceEncoding(), context, writer); - } + String bundleEncoding = bundle.getSourceEncoding(); + if (bundleEncoding == null) { + bundleEncoding = encoding; + } + try (Writer writer = new OutputStreamWriter(os, bundleEncoding)) { + velocity.mergeTemplate(bundleResource, bundleEncoding, context, writer); } } } else { @@ -984,12 +968,12 @@ protected Model getSupplement(Xpp3Dom supplementModelXml) throws MojoExecutionEx String groupId = model.getGroupId(); String artifactId = model.getArtifactId(); - if (groupId == null || groupId.trim().equals("")) { + if (groupId == null || groupId.trim().isEmpty()) { throw new MojoExecutionException( "Supplemental project XML " + "requires that a element be present."); } - if (artifactId == null || artifactId.trim().equals("")) { + if (artifactId == null || artifactId.trim().isEmpty()) { throw new MojoExecutionException( "Supplemental project XML " + "requires that a element be present."); } diff --git a/src/main/java/org/apache/maven/plugin/resources/remote/BundleRemoteResourcesMojo.java b/src/main/java/org/apache/maven/plugin/resources/remote/BundleRemoteResourcesMojo.java index 4007dca..bcf6c81 100644 --- a/src/main/java/org/apache/maven/plugin/resources/remote/BundleRemoteResourcesMojo.java +++ b/src/main/java/org/apache/maven/plugin/resources/remote/BundleRemoteResourcesMojo.java @@ -22,6 +22,7 @@ import java.io.FileWriter; import java.io.IOException; import java.io.Writer; +import java.nio.charset.Charset; import java.util.Arrays; import java.util.List; @@ -33,7 +34,6 @@ import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.plexus.util.DirectoryScanner; import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.StringUtils; /** @@ -94,9 +94,9 @@ public void execute() throws MojoExecutionException { } if (sourceEncoding == null || sourceEncoding.isEmpty()) { - getLog().warn("sourceEncoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING - + ", i.e. build is platform dependent!"); - sourceEncoding = ReaderFactory.FILE_ENCODING; + getLog().warn("sourceEncoding has not been set; using platform encoding " + Charset.defaultCharset() + + "; i.e. build is platform dependent!"); + sourceEncoding = Charset.defaultCharset().name(); } // Look at the content of the resourcesDirectory and create a manifest of the files