Skip to content
Permalink
Browse files
Fix possible resource leak and/or file locking errors: The file output
streams for the readme file and header file were not being closed.
  • Loading branch information
garydgregory committed May 29, 2018
1 parent 0c29ac3 commit c6d7168bd8d3d3232ffecf8bb064572dccd5657d
Showing 2 changed files with 29 additions and 24 deletions.
@@ -50,7 +50,7 @@ private SharedFunctions() {
*
* @param log is the Maven log for output logging, particularly in regards to error management.
* @param workingDirectory is a {@link File} that represents the directory to first attempt to delete then create.
* @throws MojoExecutionException when an {@link IOException} or {@link NullPointerException} is caught for the
* @throws MojoExecutionException when an {@link IOException} or {@link NullPointerException} is caught for the
* purpose of bubbling the exception up to Maven properly.
*/
public static void initDirectory(Log log, File workingDirectory) throws MojoExecutionException {
@@ -298,32 +298,37 @@ private List<File> copyDistributionsIntoScmDirectoryStructure(File copiedRelease
private List<File> buildReadmeAndHeaderHtmlFiles() throws MojoExecutionException {
List<File> headerAndReadmeFiles = new ArrayList<>();
File headerFile = new File(distCheckoutDirectory, "HEADER.html");
//
// HEADER file
//
try (Writer headerWriter = new OutputStreamWriter(new FileOutputStream(headerFile), "UTF-8")) {
HeaderHtmlVelocityDelegate.builder().build().render(headerWriter);
} catch (IOException e) {
final String message = "Could not build HEADER html file " + headerFile;
getLog().error(message, e);
throw new MojoExecutionException(message, e);
}
headerAndReadmeFiles.add(headerFile);
//
// README file
//
File readmeFile = new File(distCheckoutDirectory, "README.html");
try {
FileOutputStream headerStream = new FileOutputStream(headerFile);
Writer headerWriter = new OutputStreamWriter(headerStream, "UTF-8");
FileOutputStream readmeStream = new FileOutputStream(readmeFile);
Writer readmeWriter = new OutputStreamWriter(readmeStream, "UTF-8");
HeaderHtmlVelocityDelegate headerHtmlVelocityDelegate = HeaderHtmlVelocityDelegate
.builder()
.build();
headerWriter = headerHtmlVelocityDelegate.render(headerWriter);
headerWriter.close();
headerAndReadmeFiles.add(headerFile);
ReadmeHtmlVelocityDelegate readmeHtmlVelocityDelegate = ReadmeHtmlVelocityDelegate
.builder()
.withArtifactId(project.getArtifactId())
.withVersion(project.getVersion())
.withSiteUrl(project.getUrl())
.build();
readmeWriter = readmeHtmlVelocityDelegate.render(readmeWriter);
readmeWriter.close();
headerAndReadmeFiles.add(readmeFile);
headerAndReadmeFiles.addAll(copyHeaderAndReadmeToSubdirectories(headerFile, readmeFile));
try (Writer readmeWriter = new OutputStreamWriter(new FileOutputStream(readmeFile), "UTF-8")) {
// @formatter:off
ReadmeHtmlVelocityDelegate readmeHtmlVelocityDelegate = ReadmeHtmlVelocityDelegate.builder()
.withArtifactId(project.getArtifactId())
.withVersion(project.getVersion())
.withSiteUrl(project.getUrl())
.build();
// @formatter:on
readmeHtmlVelocityDelegate.render(readmeWriter);
} catch (IOException e) {
getLog().error("Could not build HEADER and README html files", e);
throw new MojoExecutionException("Could not build HEADER and README html files", e);
final String message = "Could not build README html file " + readmeFile;
getLog().error(message, e);
throw new MojoExecutionException(message, e);
}
headerAndReadmeFiles.add(readmeFile);
headerAndReadmeFiles.addAll(copyHeaderAndReadmeToSubdirectories(headerFile, readmeFile));
return headerAndReadmeFiles;
}

0 comments on commit c6d7168

Please sign in to comment.