Maven : Increment Build Number

This page last changed on Nov 05, 2008 by Kees de Kooter

When I was still building projects using Ant I let Ant increment a build number in a properties each time a build was executed (major and minor number were incremented manually when appropriate). The properties file was packaged with the application. The application was able to read the version and build number and display it. This way a user in trouble can always refer to the build number he is using.

I am trying to duplicate this functionality in Maven. Here are the results. It turned out to be quite simple actually (smile).

The mojo class

/**
 * Mojo incrementing the build.number property in the build.properties file
 * in src/main/config.
 * 
 * TODO: make major and minor version configurable / pick up from pom
 * TODO: add option to put version info in the MANIFEST
 * 
 * @author Kees de Kooter (kees@boplicity.net)
 * @date 5-apr-2006
 *
 * @goal increment
 * @phase compile
 * @description Increments build number in build.properties
 */
public class BuildNumberMojo extends AbstractMojo {

    /**
     * The name of the properties file.
     * @parameter expression="build.properties"
     */
    private String fileName;

    /**
     * The path of the properties file.
     * @parameter expression="${basedir}/src/main/config"
     */
    private String filePath;

    /**
     * @see org.apache.maven.plugin.Mojo#execute()
     */
    public void execute() throws MojoExecutionException, MojoFailureException {

        getLog().info("Starting build-number-plugin");

        BuildNumberIncrementer buildNumberIncrementer = 
            new BuildNumberIncrementer(
                    filePath + "/" + fileName,
                    getLog());
        buildNumberIncrementer.increment();
    }

}

The class doing the actual legwork

public class BuildNumberIncrementer {

    private String filePath;
    private Log log;

    public BuildNumberIncrementer(String filePath, Log log) {

        this.filePath = filePath;
    }

    /**
     * Increments the build.number property in the file and adds the
     * build.date.
     *
     */
    public void increment() {

        Integer buildNumber = 1;
        File file = new File(filePath);

        Properties properties = new Properties();

        if (file.exists()) {
            try {
                properties.load(new FileInputStream(file));
            } catch (FileNotFoundException e) {
                logError(e.toString());
            } catch (IOException e) {
                logError(e.toString());
            }
        }

        if (properties.containsKey("build.number")) {
            String property = properties.getProperty("build.number", "0");

            try {
                buildNumber = Integer.parseInt(property);
                buildNumber++;
            } catch (Exception e) {
                // Apparently the properties file was tinkred with, we just overwrite it
            }
        }

        properties.setProperty("build.number", buildNumber.toString());

        SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
        String formattedDate = dateFormatter.format(new Date());

        properties.setProperty("build.date", formattedDate);

        try {
            OutputStream outputStream = new FileOutputStream(file, false);
            properties.store(outputStream, "Created by build-number-plugin");

            outputStream.close();
        } catch (FileNotFoundException e) {
            logError(e.toString());
        } catch (IOException e) {
            logError(e.toString());
        }

    }

    /**
     * @param string
     */
    private void logError(String string) {

        if (log != null) {
            log.error(string);
        } else {
            System.out.println(string);
        }

    }
}

The pom configuration for using this plugin

<plugin>
    <groupId>org.codething.mojo</groupId>
    <artifactId>build-number-plugin</artifactId>
    <version>SNAPSHOT</version>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>increment</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The snapshot repository

Add the following repo to the repositories section in your pom.

<repository>
      <id>boplicity</id>
      <name>Boplicity Maven Repository</name>
      <url>http://www.boplicity.nl/maven-repository</url>
</repository>