Skip to content

Commit 0f418dc

Browse files
authored
[MCHANGES-435] Use try with resources (#55)
* Use try with resources
1 parent e2997ac commit 0f418dc

File tree

2 files changed

+31
-49
lines changed

2 files changed

+31
-49
lines changed

src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
import java.io.File;
2424
import java.io.FileOutputStream;
25+
import java.io.IOException;
2526
import java.io.OutputStreamWriter;
2627
import java.io.Writer;
28+
import java.nio.charset.Charset;
2729
import java.util.ArrayList;
2830
import java.util.Collections;
2931
import java.util.List;
@@ -57,7 +59,6 @@
5759
import org.apache.velocity.exception.ResourceNotFoundException;
5860
import org.apache.velocity.exception.VelocityException;
5961
import org.apache.velocity.tools.ToolManager;
60-
import org.codehaus.plexus.util.ReaderFactory;
6162
import org.codehaus.plexus.velocity.VelocityComponent;
6263

6364
/**
@@ -637,61 +638,51 @@ protected void doGenerate(List<Release> releases, Release release) throws MojoEx
637638
}
638639

639640
/**
640-
* Create the velocity template
641+
* Create the velocity template.
641642
*
642643
* @param context velocity context that has the parameter values
643644
* @param outputDirectory directory where the file will be generated
644645
* @param template velocity template which will the context be merged
645-
* @param announcementFile The file name of the generated announcement
646-
* @throws VelocityException in case of errors.
647-
* @throws MojoExecutionException in case of errors.
646+
* @param announcementFile the file name of the generated announcement
647+
* @throws VelocityException in case of error processing the Velocty template
648+
* @throws MojoExecutionException in case of errors
648649
*/
649650
public void processTemplate(Context context, File outputDirectory, String template, String announcementFile)
650651
throws VelocityException, MojoExecutionException {
651-
File f;
652652

653653
// Use the name of the template as a default value
654654
if (announcementFile == null || announcementFile.isEmpty()) {
655655
announcementFile = template;
656656
}
657657

658-
try {
659-
f = new File(outputDirectory, announcementFile);
660-
661-
if (!f.getParentFile().exists()) {
662-
f.getParentFile().mkdirs();
658+
if (!outputDirectory.exists()) {
659+
if (!outputDirectory.mkdirs()) {
660+
throw new MojoExecutionException("Failed to create directory " + outputDirectory);
663661
}
662+
}
664663

665-
VelocityEngine engine = velocity.getEngine();
666-
667-
engine.setApplicationAttribute("baseDirectory", basedir);
664+
File f = new File(outputDirectory, announcementFile);
668665

669-
if (templateEncoding == null || templateEncoding.isEmpty()) {
670-
templateEncoding = ReaderFactory.FILE_ENCODING;
671-
getLog().warn("File encoding has not been set, using platform encoding " + templateEncoding
672-
+ ", i.e. build is platform dependent!");
673-
}
666+
VelocityEngine engine = velocity.getEngine();
674667

675-
Writer writer = new OutputStreamWriter(new FileOutputStream(f), templateEncoding);
668+
engine.setApplicationAttribute("baseDirectory", basedir);
676669

670+
if (templateEncoding == null || templateEncoding.isEmpty()) {
671+
templateEncoding = Charset.defaultCharset().name();
672+
getLog().warn("File encoding has not been set, using platform encoding " + templateEncoding
673+
+ "; build is platform dependent!");
674+
}
675+
try (Writer writer = new OutputStreamWriter(new FileOutputStream(f), templateEncoding)) {
677676
Template velocityTemplate = engine.getTemplate(templateDirectory + "/" + template, templateEncoding);
678-
679677
velocityTemplate.merge(context, writer);
680-
681-
writer.flush();
682-
683-
writer.close();
684-
685678
getLog().info("Created template " + f);
686-
} catch (ResourceNotFoundException rnfe) {
687-
throw new ResourceNotFoundException("Template not found. ( " + templateDirectory + "/" + template + " )");
679+
} catch (ResourceNotFoundException ex) {
680+
throw new ResourceNotFoundException(
681+
"Template not found. ( " + templateDirectory + "/" + template + " )", ex);
688682
} catch (VelocityException ve) {
689-
throw new VelocityException(ve.toString());
690-
} catch (Exception e) {
691-
if (e.getCause() != null) {
692-
getLog().warn(e.getCause());
693-
}
694-
throw new MojoExecutionException(e.toString(), e.getCause());
683+
throw ve;
684+
} catch (RuntimeException | IOException e) {
685+
throw new MojoExecutionException(e.toString(), e);
695686
}
696687
}
697688

src/main/java/org/apache/maven/plugins/changes/ChangesReport.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import javax.inject.Inject;
2222

2323
import java.io.File;
24-
import java.io.FileWriter;
2524
import java.io.IOException;
2625
import java.io.Writer;
2726
import java.net.URL;
27+
import java.nio.charset.StandardCharsets;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
2830
import java.text.SimpleDateFormat;
2931
import java.util.Collections;
3032
import java.util.Date;
@@ -437,24 +439,13 @@ private boolean generateFeed(final ChangesXML changesXml, final Locale locale) {
437439
feed.setAuthor(changesXml.getAuthor());
438440
feed.setDateFormat(new SimpleDateFormat(publishDateFormat, new Locale(publishDateLocale)));
439441

440-
Writer writer = null;
441-
442-
try {
443-
writer = new FileWriter(new File(getReportOutputDirectory(), "changes.rss"));
442+
Path changes = getReportOutputDirectory().toPath().resolve("changes.rss");
443+
try (Writer writer = Files.newBufferedWriter(changes, StandardCharsets.UTF_8)) {
444444
feed.export(changesXml.getReleaseList(), feedType, writer);
445445
} catch (IOException ex) {
446446
success = false;
447-
getLog().warn("Failed to create rss feed: " + ex.getMessage());
447+
getLog().warn("Failed to create RSS feed: " + ex.getMessage());
448448
getLog().debug(ex);
449-
} finally {
450-
try {
451-
if (writer != null) {
452-
writer.close();
453-
}
454-
} catch (IOException ex) {
455-
getLog().warn("Failed to close writer: " + ex.getMessage());
456-
getLog().debug(ex);
457-
}
458449
}
459450

460451
return success;

0 commit comments

Comments
 (0)