> From: Francois Beauregard [mailto:[EMAIL PROTECTED]
> Sent: Thursday, December 04, 2003 8:20 AM
>
> Thanks, is there an example of a Maven script that uses
> velocity or JSL to
> do this kind of substitution somewhere that I can look at.
>
> Thanks,
> Fran�ois
Assume you have a file in your source directory called 'Version.j' with the
following contents.
NOTE: This file can exist in any package, all occurences of 'Version.j'
inside
of your source tree will work.
--- snip-start ---
package com.foo;
/**
* Version Meta Data
*
* NOTE: This file is overwritten during the build process.
* See Version.j for checked in version of this file.
*
* @author Joakim Erdfelt
*/
public class Version
{
/** VERSION_NUMBER is "@VERSION_NUMBER@" */
public static final String VERSION_NUMBER = "@VERSION_NUMBER@";
/** BUILD_DATE is "@BUILD_DATE@" */
public static final String BUILD_DATE = "@BUILD_DATE@";
/** BUILD_MACHINE is "@BUILD_MACHINE@" */
public static final String BUILD_MACHINE = "@BUILD_MACHINE@";
/** BUILD_PATH is "@BUILD_PATH@" */
public static final String BUILD_PATH = "@BUILD_PATH@";
/** BUILD_USER is "@BUILD_USER@" */
public static final String BUILD_USER = "@BUILD_USER@";
/** BUILD_JAVA_VERSION is "@BUILD_JAVA_VERSION@" */
public static final String BUILD_JAVA_VERSION = "@BUILD_JAVA_VERSION@";
public static void main(String args[]) {
System.out.println(Version.class.getName() + " - cingularONE - " +
VERSION_NUMBER);
System.out.println(" ");
System.out.println(" Build Information ");
System.out.println("-----------------------------------------------");
System.out.println(" Date : " + BUILD_DATE);
System.out.println(" User : " + BUILD_USER);
System.out.println(" Machine : " + BUILD_MACHINE);
System.out.println(" Path : " + BUILD_PATH);
System.out.println(" Java : " + BUILD_JAVA_VERSION);
}
}
--- snip-end ---
Now when you execute the following maven goal ...
--- snip-start ---
<goal name="genversion">
<!-- determine the time now -->
<ant:tstamp prefix="build">
<ant:format property="date" pattern="EEE MMM d, yyyy hh:mm:ss z"
locale="en" />
</ant:tstamp>
<!-- Platform independant way to find out hostname.
This works for UNIX and Windows.
UNIX uses $HOSTNAME
Windows uses %COMPUTERNAME%.
-->
<ant:property environment="env" />
<ant:property name="env.HOSTNAME" value="${env.COMPUTERNAME}" />
<!-- sanitize path (useful for builds on ms windows) -->
<ant:pathconvert property="build.path" targetos="unix">
<ant:path>
<ant:pathelement location="${basedir}" />
</ant:path>
</ant:pathconvert>
<ant:filterset id="version.filter" begintoken="@" endtoken="@">
<!-- comes from build.properties file specified in command line -->
<ant:filter token="VERSION_NUMBER" value="${pom.currentVersion}" />
<!-- computed using ant's internal tstamp task -->
<ant:filter token="BUILD_DATE" value="${build.date}" />
<!-- computed using an external command -->
<ant:filter token="BUILD_MACHINE" value="${env.HOSTNAME}" />
<!-- uses ant's internal basedir property -->
<ant:filter token="BUILD_PATH" value="${build.path}" />
<!-- determined from user.name system property -->
<ant:filter token="BUILD_USER" value="${user.name}" />
<!-- determined from java.version system property -->
<ant:filter token="BUILD_JAVA_VERSION" value="${java.vendor} -
${java.version}" />
</ant:filterset>
<ant:copy todir="${pom.build.sourceDirectory}" filtering="true"
overwrite="true">
<ant:fileset dir="${pom.build.sourceDirectory}">
<ant:include name="**/Version.j"/>
</ant:fileset>
<ant:filterset refid="version.filter" />
<ant:mapper type="regexp" from="^(.*)\.j$$" to="\1.java" />
</ant:copy>
</goal>
--- snip-end ---
You will have a few Version.java files created for you.
This uses the ant copy/filtering/filterset technique.
I do not have an example of the Velocity or JSL techniques.
More tidbits, with the above example the created Version.java file
can be executed directly. You can place this class into your jar
file's Main-Class attribute of the META-INF/MANIFEST.MF
and have the jar file itself tell you how it was compiled.
Enjoy,
/* Joakim Erdfelt - Cingular Wireless */