package org.spiffyui.maven.plugins;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.json.JSONObject;

/**
 * Goal which determines various revision details about the build
 * 
 * @goal build-info
 * @phase generate-resources
 */
public class BuildInfoMojo extends AbstractMojo
{
    /**
     * The maven project.
     * 
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    protected MavenProject project;
    
    /**
     * The character encoding scheme to be applied exporting the JSON document
     *
     * @parameter expression="${encoding}" default-value="UTF-8"
     */
    protected String encoding;
    
    /**
     * The character encoding scheme to be applied exporting the JSON document
     *
     * @parameter expression="${dateFormat}" default-value="yyyy.MMMMM.dd hh:mm aaa"
     */
    protected String dateFormat;
    
    /**
     * Location of the file.
     * 
     * @parameter expression="${project.build.outputDirectory}/build-info.json"
     * @required
     */
    private File outputFile;
    
    /**
     * The base directory of this project
     * 
     * @parameter expression="${basedir}"
     * @required
     * @readonly
     */
    protected File basedir;

    @Override
    public void execute()
        throws MojoExecutionException,
            MojoFailureException
    {
        Properties p = project.getProperties();
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String date = sdf.format(cal.getTime());
        
        try {
            File parent = outputFile.getParentFile();
            if (!parent.exists())
                FileUtils.forceMkdir(parent);
            
            // Create file
            PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFile), encoding));
            
            JSONObject info = new JSONObject();
            info.put("build.date", date);
            info.put("build.user", System.getProperties().get("user.name"));
            //info.put("build.version", version);
            info.put("build.revision", p.getProperty("revision.number"));
            info.put("build.revision.date", p.getProperty("revision.date"));
            info.put("build.dir", basedir.getAbsolutePath());
 
            out.write(info.toString());
            // Close the output stream
            out.close();
        } catch (Exception e) {// Catch exception if any
            throw new MojoExecutionException(e.getMessage());
        }

    }

}

@@ -2309,20 +2309,19 @@
 NObject 
-info
+rev
  = new J
@@ -2349,35 +2349,58 @@
     
-info
+rev
 .put(%22
-build.date%22, date
+number%22, p.getProperty(%22revision.number%22)
 );%0A 
@@ -2414,192 +2414,220 @@
     
-info
+rev
 .put(%22
-build.user%22, System.getProperties().get(%22user.name%22));%0A            //info.put(%22build.version%22, version);%0A            info.put(%22build.revision%22, p.getProperty(%22revision.number
+date%22, p.getProperty(%22revision.date%22));%0A            %0A            JSONObject info = new JSONObject();%0A            info.put(%22date%22, date);%0A            info.put(%22user%22, System.getProperties().get(%22user.name
 %22));
@@ -2631,32 +2631,34 @@
 ));%0A            
+//
 info.put(%22build.
@@ -2661,37 +2661,48 @@
 ild.
-revision.date%22, p.getProperty
+version%22, version);%0A            info.put
 (%22re
@@ -2699,39 +2699,38 @@
 fo.put(%22revision
-.date%22)
+%22, rev
 );%0A            i
@@ -2738,22 +2738,16 @@
 fo.put(%22
-build.
 dir%22, ba

Convert to a more natural JSON structure