/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kirchnerei.buildnumber.plugin;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

/**
 * kirchnerei buildnumber plugin (equals to ant buildnumber)
 *
 * @author <a href="kirchnerei@gmx.net">Mulder3</a>
 * @goal create
 * @requiresProject
 * @threadsafe true
 * @phase validate
 * @describe Lightweight mojo to create a buildnumber
 */
public class BuildNumberMojo extends AbstractMojo {

	public static final String PROPERTY_COMMENT = "This is the build number. Do not change directly";
	/**
     * Location of the file.
	 *
     * @parameter expression="${project.build.directory}"
     * @required
     */
    private File outputDirectory;

	/**
	 * The name of the file.
	 *
	 * @parameter default-value="build.properties"
	 */
	private String buildFile;

	/**
	 * The name of the property value in which the buildnumber will be store.
	 *
	 * @parameter default-value="buildnumber"
	 */
	private String propertyName;

	/**
	 * The maven project.
	 *
	 * @parameter expression="${project}"
	 * @required
	 * @readonly
	 */
	private MavenProject project;

    public void execute() throws MojoExecutionException {
	    try {
	        File file = new File(outputDirectory, buildFile);

		    int buildNumber = readBuildNumberFrom(file);
		    if (buildNumber <= 0) {
			    getLog().info("create new build number with '1'");
			    buildNumber = 1;
		    } else {
			    buildNumber++;
			    getLog().info("build number '" + buildNumber + "' will be use");
		    }
		    writeBuildNumberTo(file, buildNumber);
		    Properties prop = project.getProperties();
		    prop.put(propertyName, String.valueOf(buildNumber));
	    } catch (IOException e) {
		    getLog().error(e);
		    throw new MojoExecutionException("could not handle the build number", e);
	    }
    }

	public void setProject(MavenProject project) {
		this.project = project;
	}

	int readBuildNumberFrom(File file) throws IOException{
		if (!file.exists()) {
			return -1;
		} else {
			Properties prop = new Properties();
			InputStream input = new FileInputStream(file);
			prop.load(input);
			input.close();
			return NumberUtils.toInt(prop.getProperty(propertyName, "1"), 1);
		}
	}

	void writeBuildNumberTo(File file, int buildNumber) throws IOException {
		if (!file.exists()) {
			File parent = file.getParentFile();
			parent.mkdirs();
			file.createNewFile();
		}
		OutputStream output = new FileOutputStream(file);
		Properties prop = new Properties();
		prop.setProperty(propertyName, String.valueOf(buildNumber));
		prop.store(output, PROPERTY_COMMENT);
	}
}

@@ -1461,20 +1461,17 @@
 %22;%0A%09/**%0A
-    
+%09
  * Locat
@@ -1491,20 +1491,17 @@
 le.%0A%09 *%0A
-    
+%09
  * @para
@@ -1546,20 +1546,17 @@
 ctory%7D%22%0A
-    
+%09
  * @requ
@@ -1564,20 +1564,14 @@
 red%0A
-     */%0A    
+%09 */%0A%09
 priv
@@ -2012,20 +2012,17 @@
 oject;%0A%0A
-    
+%09
 public v
@@ -2072,27 +2072,18 @@
  %7B%0A%09
-    
+%09
 try %7B%0A%09
-        
+%09%09
 File
@@ -2131,20 +2131,17 @@
 le);%0A%0A%09%09
-    
+%09
 int buil
@@ -2179,20 +2179,17 @@
 ile);%0A%09%09
-    
+%09
 if (buil
@@ -2203,28 +2203,25 @@
  %3C= 0) %7B%0A%09%09%09
-    
+%09
 getLog().inf
@@ -2258,28 +2258,25 @@
 h '1'%22);%0A%09%09%09
-    
+%09
 buildNumber 
@@ -2282,20 +2282,17 @@
  = 1;%0A%09%09
-    
+%09
 %7D else %7B
@@ -2295,20 +2295,17 @@
 se %7B%0A%09%09%09
-    
+%09
 buildNum
@@ -2314,20 +2314,17 @@
 r++;%0A%09%09%09
-    
+%09
 getLog()
@@ -2386,20 +2386,14 @@
 ;%0A%09%09
-    %7D%0A%09%09    
+%09%7D%0A%09%09%09
 writ
@@ -2429,20 +2429,17 @@
 ber);%0A%09%09
-    
+%09
 Properti
@@ -2475,20 +2475,17 @@
 es();%0A%09%09
-    
+%09
 prop.put
@@ -2530,20 +2530,17 @@
 ber));%0A%09
-    
+%09
 %7D catch 
@@ -2559,20 +2559,17 @@
  e) %7B%0A%09%09
-    
+%09
 getLog()
@@ -2581,20 +2581,17 @@
 r(e);%0A%09%09
-    
+%09
 throw ne
@@ -2661,18 +2661,12 @@
 );%0A%09
-    %7D%0A    
+%09%7D%0A%09
 %7D%0A%0A%09
@@ -2796,16 +2796,17 @@
 xception
+ 
 %7B%0A%09%09if (

Format the source file