Generate a Version.java file in Maven

Posted by Ralph on Stack Overflow See other posts from Stack Overflow or by Ralph
Published on 2010-03-18T12:58:43Z Indexed on 2010/03/18 13:01 UTC
Read the original article Hit count: 381

Filed under:
|

I have a Java project that I build using an Ant script. I am trying to convert the project to Maven.

One of the tasks generates a Java source file called Version.java that contains a static String representation of the compilation timestamp, as follows:

package com.foo.bar;
public final class Version {
 public static String VERSION="100301.1046";
}

The Ant task is very simple:

<target name="version" depends="init" description="Create Version.java">
    <echo file="src/${package.dir}/Version.java" message="package ${package.name};${line.separator}" />
    <echo file="src/${package.dir}/Version.java" append="true" message="public final class Version {${line.separator}" />
    <echo file="src/${package.dir}/Version.java"
          append="true"
          message=" public static String VERSION=&quot;${buildtime}&quot;;${line.separator}" />
    <echo file="src/${package.dir}/Version.java" append="true" message="}${line.separator}" />
    <echo message="BUILD ${buildtime}" />
</target>

Is it possible to do something similar in Maven, using generate-sources, or some other simple method?

© Stack Overflow or respective owner

Related posts about maven-2

Related posts about code-generation