This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch versions in repository https://gitbox.apache.org/repos/asf/maven-changes-plugin.git
commit cc3595060782a750de2390b264c9299595ee02e6 Author: Elliotte Rusty Harold <elh...@ibiblio.org> AuthorDate: Fri Nov 22 13:29:40 2024 -0500 Use try with resources --- .../plugins/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 aa24b75..4772952 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,50 @@ 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("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 4398f22..af9c9d2 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; @@ -439,24 +441,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;