Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding file merging features #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -53,6 +54,18 @@ public class WritePropertiesFileMojo extends AbstractMojo {
@Parameter(property = "createDirectory", defaultValue = "true")
private boolean createDirectory;

/**
* If properties already exist at the given output, we the results be combined (defaults to false)
*/
@Parameter(property = "mergeProperties", defaultValue = "false")
private boolean mergeProperties;

/**
* Whether or not provided properties take precedence over existing properties (does nothing if mergeProperties is false, defaults to true)
*/
@Parameter(property = "overrideProperties", defaultValue = "true")
private boolean overrideProperties;

public void execute() throws MojoExecutionException, MojoFailureException {
File finalFile = new File(outputDirectory, filename).getAbsoluteFile();
String finalFilename = finalFile.getAbsolutePath();
Expand All @@ -67,14 +80,15 @@ public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Saving properties to file " + finalFilename);
FileOutputStream out = null;
try {
out = new FileOutputStream(finalFile);
OutputStreamWriter writer = new OutputStreamWriter(out, Charset.forName("UTF-8"));

String finalComment = comment;
if (comment != null && comment.trim().isEmpty()) {
finalComment = null;
}
properties.store(writer, finalComment);
Properties propertiesToWrite = obtainPotentiallyMergedProperties(finalFile);
out = new FileOutputStream(finalFile);
OutputStreamWriter writer = new OutputStreamWriter(out, Charset.forName("UTF-8"));
propertiesToWrite.store(writer, finalComment);
writer.close();
out.close();
} catch (IOException e) {
Expand All @@ -90,4 +104,25 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}

private Properties obtainPotentiallyMergedProperties(File propertiesFile) throws MojoExecutionException {
if (!mergeProperties || !propertiesFile.exists()) {
return properties;
}
Properties preExistingProperties = new Properties();
try {
FileInputStream fileInputStream = new FileInputStream(propertiesFile);
preExistingProperties.load(fileInputStream);
fileInputStream.close();
} catch (IOException e) {
throw new MojoExecutionException("Error loading pre-existing properties file " + propertiesFile, e);
}
return overrideProperties
? mergeProperties(preExistingProperties, properties)
: mergeProperties(properties, preExistingProperties);
}

private Properties mergeProperties(Properties lowPriorityProperties, Properties highPriorityProperties) {
lowPriorityProperties.putAll(highPriorityProperties);
return lowPriorityProperties;
}
}