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-changes-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 0f418dc  [MCHANGES-435] Use try with resources (#55)
0f418dc is described below

commit 0f418dcfe182a0e6d83517b93a15b3d01cc5db79
Author: Elliotte Rusty Harold <elh...@users.noreply.github.com>
AuthorDate: Fri Nov 22 20:09:47 2024 +0000

    [MCHANGES-435] Use try with resources (#55)
    
    * Use try with resources
---
 .../plugins/announcement/AnnouncementMojo.java     | 59 +++++++++-------------
 .../maven/plugins/changes/ChangesReport.java       | 21 +++-----
 2 files changed, 31 insertions(+), 49 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 aa24b75..f718e15 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 javax.inject.Inject;
 
 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.context.Context;
 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,51 @@ public class AnnouncementMojo extends 
AbstractAnnouncementMojo {
     }
 
     /**
-     * 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("Failed 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
+                    + "; 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 (ResourceNotFoundException ex) {
+            throw new ResourceNotFoundException(
+                    "Template not found. ( " + templateDirectory + "/" + 
template + " )", ex);
         } 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 b9d6b4a..434dd4c 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 @@ package org.apache.maven.plugins.changes;
 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;
@@ -437,24 +439,13 @@ public class ChangesReport extends AbstractChangesReport {
         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;

Reply via email to