Skip to content

Commit

Permalink
[MCHANGES-435] Use try with resources (#55)
Browse files Browse the repository at this point in the history
* Use try with resources
  • Loading branch information
elharo authored Nov 22, 2024
1 parent e2997ac commit 0f418dc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -637,61 +638,51 @@ protected void doGenerate(List<Release> 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("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);
}
}

Expand Down
21 changes: 6 additions & 15 deletions src/main/java/org/apache/maven/plugins/changes/ChangesReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -437,24 +439,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;
Expand Down

0 comments on commit 0f418dc

Please sign in to comment.