package com.internetitem.maven.writePropertiesFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Properties;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
 * Create a Java Properties file
 * 
 * @author Adam Batkin <adam@batkin.net>
 *
 */
@Mojo(name = "write-properties-file")
public class WritePropertiesFileMojo extends AbstractMojo {

	/**
	 * File where the properties are saved
	 */
	@Parameter(property = "outputFile", required = true)
	private File outputFile;

	/**
	 * Properties to save
	 */
	@Parameter(property = "properties", required = true)
	private Properties properties;

	public void execute() throws MojoExecutionException, MojoFailureException {
		getLog().info("Saving properties to file " + outputFile.getAbsolutePath());
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(outputFile);
			OutputStreamWriter writer = new OutputStreamWriter(out, Charset.forName("UTF-8"));
			properties.store(writer, "Generated by Maven");
			writer.close();
			out.close();
		} catch (IOException e) {
			throw new MojoFailureException("Unable to save properties to file " + outputFile.getAbsolutePath() + ": " + e.getMessage(), e);
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (Exception e) {
					// Ignore
				}
			}
		}
	}

}

@@ -569,16 +569,17 @@
 .net%3E%0A *
+ 
 %0A */%0A@Mo
@@ -930,137 +930,524 @@
 ;%0A%0A%09
-public void execute() throws MojoExecutionException, MojoFailureException %7B%0A%09%09getLog().info(%22Saving properties to file %22 + outp
+/**%0A%09 * Create intermediate directories if necessary (defaults to true)%0A%09 */%0A%09@Parameter(property = %22createDirectory%22, defaultValue = %22true%22)%0A%09private boolean createDirectory;%0A%0A%09public void execute() throws MojoExecutionException, MojoFailureException %7B%0A%09%09File absoluteFile = outputFile.getAbsoluteFile();%0A%0A%09%09File directory = absoluteFile.getParentFile();%0A%09%09if (!directory.exists()) %7B%0A%09%09%09getLog().info(%22Creating directory %22 + directory.getAbsolutePath());%0A%09%09%09directory.mkdirs();%0A%09%09%7D%0A%0A%09%09String absolutePath = absol
 ut
+e
 File
@@ -1460,24 +1460,85 @@
 solutePath()
+;%0A%09%09getLog().info(%22Saving properties to file %22 + absolutePath
 );%0A%09%09FileOut
@@ -1883,31 +1883,17 @@
 ile %22 + 
-outputFile.getA
+a
 bsoluteP
@@ -1895,18 +1895,16 @@
 lutePath
-()
  + %22: %22 

create intermediate directories