Skip to content

Commit cc35950

Browse files
committed
Use try with resources
1 parent b36a2b4 commit cc35950

File tree

2 files changed

+28
-47
lines changed

2 files changed

+28
-47
lines changed

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

Lines changed: 22 additions & 32 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,50 @@ 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("Faield 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+
+ ", i.e. 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);
686679
} catch (ResourceNotFoundException rnfe) {
687680
throw new ResourceNotFoundException("Template not found. ( " + templateDirectory + "/" + template + " )");
688681
} 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());
682+
throw ve;
683+
} catch (RuntimeException | IOException e) {
684+
throw new MojoExecutionException(e.toString(), e);
695685
}
696686
}
697687

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;
@@ -439,24 +441,13 @@ private boolean generateFeed(final ChangesXML changesXml, final Locale locale) {
439441
feed.setAuthor(changesXml.getAuthor());
440442
feed.setDateFormat(new SimpleDateFormat(publishDateFormat, new Locale(publishDateLocale)));
441443

442-
Writer writer = null;
443-
444-
try {
445-
writer = new FileWriter(new File(getReportOutputDirectory(), "changes.rss"));
444+
Path changes = getReportOutputDirectory().toPath().resolve("changes.rss");
445+
try (Writer writer = Files.newBufferedWriter(changes, StandardCharsets.UTF_8)) {
446446
feed.export(changesXml.getReleaseList(), feedType, writer);
447447
} catch (IOException ex) {
448448
success = false;
449-
getLog().warn("Failed to create rss feed: " + ex.getMessage());
449+
getLog().warn("Failed to create RSS feed: " + ex.getMessage());
450450
getLog().debug(ex);
451-
} finally {
452-
try {
453-
if (writer != null) {
454-
writer.close();
455-
}
456-
} catch (IOException ex) {
457-
getLog().warn("Failed to close writer: " + ex.getMessage());
458-
getLog().debug(ex);
459-
}
460451
}
461452

462453
return success;

0 commit comments

Comments
 (0)