From cc3595060782a750de2390b264c9299595ee02e6 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Fri, 22 Nov 2024 13:29:40 -0500 Subject: [PATCH 1/3] Use try with resources --- .../announcement/AnnouncementMojo.java | 54 ++++++++----------- .../maven/plugins/changes/ChangesReport.java | 21 +++----- 2 files changed, 28 insertions(+), 47 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java b/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java index aa24b758..47729520 100644 --- a/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java +++ b/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java @@ -22,8 +22,10 @@ import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -57,7 +59,6 @@ import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.exception.VelocityException; import org.apache.velocity.tools.ToolManager; -import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.velocity.VelocityComponent; /** @@ -637,61 +638,50 @@ protected void doGenerate(List releases, Release release) throws MojoEx } /** - * Create the velocity template + * Create the velocity template. * * @param context velocity context that has the parameter values * @param outputDirectory directory where the file will be generated * @param template velocity template which will the context be merged - * @param announcementFile The file name of the generated announcement - * @throws VelocityException in case of errors. - * @throws MojoExecutionException in case of errors. + * @param announcementFile the file name of the generated announcement + * @throws VelocityException in case of error processing the Velocty template + * @throws MojoExecutionException in case of errors */ public void processTemplate(Context context, File outputDirectory, String template, String announcementFile) throws VelocityException, MojoExecutionException { - File f; // Use the name of the template as a default value if (announcementFile == null || announcementFile.isEmpty()) { announcementFile = template; } - try { - f = new File(outputDirectory, announcementFile); - - if (!f.getParentFile().exists()) { - f.getParentFile().mkdirs(); + if (!outputDirectory.exists()) { + if (!outputDirectory.mkdirs()) { + throw new MojoExecutionException("Faield to create directory " + outputDirectory); } + } - VelocityEngine engine = velocity.getEngine(); - - engine.setApplicationAttribute("baseDirectory", basedir); + File f = new File(outputDirectory, announcementFile); - if (templateEncoding == null || templateEncoding.isEmpty()) { - templateEncoding = ReaderFactory.FILE_ENCODING; - getLog().warn("File encoding has not been set, using platform encoding " + templateEncoding - + ", i.e. build is platform dependent!"); - } + VelocityEngine engine = velocity.getEngine(); - Writer writer = new OutputStreamWriter(new FileOutputStream(f), templateEncoding); + engine.setApplicationAttribute("baseDirectory", basedir); + if (templateEncoding == null || templateEncoding.isEmpty()) { + templateEncoding = Charset.defaultCharset().name(); + getLog().warn("File encoding has not been set, using platform encoding " + templateEncoding + + ", i.e. build is platform dependent!"); + } + try (Writer writer = new OutputStreamWriter(new FileOutputStream(f), templateEncoding)) { Template velocityTemplate = engine.getTemplate(templateDirectory + "/" + template, templateEncoding); - velocityTemplate.merge(context, writer); - - writer.flush(); - - writer.close(); - getLog().info("Created template " + f); } catch (ResourceNotFoundException rnfe) { throw new ResourceNotFoundException("Template not found. ( " + templateDirectory + "/" + template + " )"); } catch (VelocityException ve) { - throw new VelocityException(ve.toString()); - } catch (Exception e) { - if (e.getCause() != null) { - getLog().warn(e.getCause()); - } - throw new MojoExecutionException(e.toString(), e.getCause()); + throw ve; + } catch (RuntimeException | IOException e) { + throw new MojoExecutionException(e.toString(), e); } } diff --git a/src/main/java/org/apache/maven/plugins/changes/ChangesReport.java b/src/main/java/org/apache/maven/plugins/changes/ChangesReport.java index 4398f229..af9c9d2c 100644 --- a/src/main/java/org/apache/maven/plugins/changes/ChangesReport.java +++ b/src/main/java/org/apache/maven/plugins/changes/ChangesReport.java @@ -21,10 +21,12 @@ import javax.inject.Inject; import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; @@ -439,24 +441,13 @@ private boolean generateFeed(final ChangesXML changesXml, final Locale locale) { feed.setAuthor(changesXml.getAuthor()); feed.setDateFormat(new SimpleDateFormat(publishDateFormat, new Locale(publishDateLocale))); - Writer writer = null; - - try { - writer = new FileWriter(new File(getReportOutputDirectory(), "changes.rss")); + Path changes = getReportOutputDirectory().toPath().resolve("changes.rss"); + try (Writer writer = Files.newBufferedWriter(changes, StandardCharsets.UTF_8)) { feed.export(changesXml.getReleaseList(), feedType, writer); } catch (IOException ex) { success = false; - getLog().warn("Failed to create rss feed: " + ex.getMessage()); + getLog().warn("Failed to create RSS feed: " + ex.getMessage()); getLog().debug(ex); - } finally { - try { - if (writer != null) { - writer.close(); - } - } catch (IOException ex) { - getLog().warn("Failed to close writer: " + ex.getMessage()); - getLog().debug(ex); - } } return success; From 7fe908639fa45a45d6fec7ae2bd58a541aeb846a Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Fri, 22 Nov 2024 13:37:03 -0500 Subject: [PATCH 2/3] spelling --- .../apache/maven/plugins/announcement/AnnouncementMojo.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java b/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java index 47729520..0a3c3d37 100644 --- a/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java +++ b/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java @@ -657,7 +657,7 @@ public void processTemplate(Context context, File outputDirectory, String templa if (!outputDirectory.exists()) { if (!outputDirectory.mkdirs()) { - throw new MojoExecutionException("Faield to create directory " + outputDirectory); + throw new MojoExecutionException("Failed to create directory " + outputDirectory); } } @@ -670,7 +670,7 @@ public void processTemplate(Context context, File outputDirectory, String templa if (templateEncoding == null || templateEncoding.isEmpty()) { templateEncoding = Charset.defaultCharset().name(); getLog().warn("File encoding has not been set, using platform encoding " + templateEncoding - + ", i.e. build is platform dependent!"); + + "; build is platform dependent!"); } try (Writer writer = new OutputStreamWriter(new FileOutputStream(f), templateEncoding)) { Template velocityTemplate = engine.getTemplate(templateDirectory + "/" + template, templateEncoding); From 9cce500f12dee467844921758541911ab9a70518 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Fri, 22 Nov 2024 13:38:46 -0500 Subject: [PATCH 3/3] spelling --- .../apache/maven/plugins/announcement/AnnouncementMojo.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java b/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java index 0a3c3d37..f718e151 100644 --- a/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java +++ b/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java @@ -676,8 +676,9 @@ public void processTemplate(Context context, File outputDirectory, String templa Template velocityTemplate = engine.getTemplate(templateDirectory + "/" + template, templateEncoding); velocityTemplate.merge(context, writer); getLog().info("Created template " + f); - } catch (ResourceNotFoundException rnfe) { - throw new ResourceNotFoundException("Template not found. ( " + templateDirectory + "/" + template + " )"); + } catch (ResourceNotFoundException ex) { + throw new ResourceNotFoundException( + "Template not found. ( " + templateDirectory + "/" + template + " )", ex); } catch (VelocityException ve) { throw ve; } catch (RuntimeException | IOException e) {