This seems like a very useful mojo. Are you going to release it? Maybe in the sandbox at mojo.codehaus.org? I'm going to use it and could help out if necessary.

J

On 16-Dec-05, at 12:29 PM, puschteblume wrote:

/*
 * Main.java
 *
 * Created on 13. November 2005, 17:02
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.form105.maven.plugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
 *
 * @goal create
 * @phase compile
 * @description a sample maven plugin
 */
public class CreateNumberMojo extends AbstractMojo {
public void execute() throws MojoExecutionException, MojoFailureException {
        FileInputStream fileInStream = null;
        FileOutputStream fileOutStream = null;
        File file = new File(getFileName());
        getLog().info("Properties file to load: "+file);
        try {
            fileInStream = new FileInputStream(getFileName());
        } catch (FileNotFoundException ex) {
            try {
                file.createNewFile();
            } catch (IOException ioe) {
                getLog().error("Can't create file: " + file);
                ex.printStackTrace();
            }
        }
        Properties properties = new Properties();
        try {
            properties.load(fileInStream);
            fileInStream.close();
        } catch (IOException ex) {
            getLog().error(ex.getCause());
            ex.printStackTrace();
        }
getLog().info("buildnumber: "+properties.getProperty (keyName));
        getLog().debug("Keyname: "+keyName);
        if (properties.getProperty(keyName) == null) {
            properties.setProperty(keyName,startNumber);
getLog().info("Started a new BuildNumber from "+startNumber);
        } else {
            int i = Integer.parseInt(properties.getProperty(keyName));
            i++;
properties.setProperty(keyName, new Integer(i).toString ());
            getLog().info("Set BuildNumber to: "+i);
        }
        try {
            fileOutStream = new FileOutputStream(file);
properties.store(fileOutStream, "Do not edit by hand. This counts build numbers");
            fileOutStream.close();
        } catch (IOException ex) {
            getLog().error(ex.getCause());
            getLog().error(ex.fillInStackTrace().toString());
        }
    }

    /**
     * Holds value of property version.
     * @parameter expression="${settings.localRepository}"
     */
    private String version;

    /**
     * Getter for property version.
     * @return Value of property version.
     */
    public String getVersion() {
        return this.version;
    }

    /**
     * Setter for property version.
     * @param version New value of property version.
     */
    public void setVersion(String version) {
        this.version = version;
    }

    /**
     * Holds value of property file.
     *
     * @parameter expression="buildnumber.properties"
     */
    private String fileName;

    /**
     * Getter for property file.
     * @return Value of property file.
     */
    public String getFileName() {
        return this.fileName;
    }

    /**
     * Setter for property file.
     * @param file New value of property file.
     */
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    /**
     * Holds value of property startNumber.
     * @parameter expression="1"
     */
    private String startNumber;

    /**
     * Getter for property startNumber.
     * @return Value of property startNumber.
     */
    public String getStartNumber() {
        return this.startNumber;
    }

    /**
     * Setter for property startNumber.
     * @param startNumber New value of property startNumber.
     */
    public void setStartNumber(String startNumber) {
        this.startNumber = startNumber;
    }

    /**
     * Holds value of property keyName.
     *
     * @parameter expression="buildnumber"
     */
    private String keyName;

    /**
     * Getter for property keyName.
     * @return Value of property keyName.
     */
    public String getKeyName() {
        return this.keyName;
    }

    /**
     * Setter for property keyName.
     * @param keyName New value of property keyName.
     */
    public void setKeyName(String keyName) {
        this.keyName = keyName;
    }

public static void main(String[] args) throws MojoExecutionException, MojoFailureException {

        Properties prop = new Properties();
        FileInputStream inStream;
        File file = new File("buildnumber.properties");
        try {
            inStream = new FileInputStream(file);
            prop.load(inStream);

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        prop.list(System.out);

        CreateNumberMojo buildnumber = new CreateNumberMojo();
        buildnumber.setFileName("buildnumber.properties");
        buildnumber.setKeyName("buildnumber");
        buildnumber.setStartNumber("1");

        buildnumber.execute();
    }

}
<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : pom.xml.xml
    Created on : 14. November 2005, 10:34
    Author     : Administrator
    Description:
        Purpose of the document follows.
-->

<project>
  <modelVersion>4.0.0</modelVersion>
  <!-- groupId = path to the plugin -->
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-buildnumber-plugin</artifactId>
  <packaging>maven-plugin</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Plugin to generate/increment a buildnumber</name>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>2.0</version>
    </dependency>
  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
  </build>
</project>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

--
Julian Wood <[EMAIL PROTECTED]>

Programmer/Analyst
University of Calgary

http://commons.ucalgary.ca


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to