2

we want to show the time on page when WAR of a JAVA(Web Dynamic Project 3.0) is created , We do this in Grails by calling this function

/**
 * Gant script to update buildtimestamp
 */
eventCreateWarStart = { warName, stagingDir ->
    def buildDate = new Date()

    ant.propertyfile(file: "${stagingDir}/WEB-INF/classes/application.properties") {
        entry(key:"build.date", value:buildDate)
    }

}

Now the same thing we want to implement in java , WAR event is call when WAR of that project is created How to do this ??

abhaygarg12493
  • 1,565
  • 2
  • 20
  • 40

1 Answers1

1

It depends on what you use to build the war.

If you use directly the jar command, you should probably create a script that writes the date in the application.properties file and launches the war creation.

If you use ant you can do this in two steps:

  1. Create a class like this one:
package tests;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

public class GeneratePropertiesFile {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.setProperty("build.date", new Date().toString());
        properties.store(new FileOutputStream(stagingDir + "/WEB-INF/classes/application.properties"), "");
    }
}
  1. Write an ant build file like this:
<project name="sample" basedir="." default="buildWar">
    <target name="buildProperties">
        <java fork="true" failonerror="yes" classname="tests.GeneratePropertiesFile"/>
    </target>
    <target name="buildWar" depends="buildProperties">
        <war destfile="myapp.war" webxml="src/metadata/myapp.xml">
            <fileset dir="src/html/myapp"/>
            <fileset dir="src/jsp/myapp"/>
            <lib dir="thirdparty/libs">
                <exclude name="jdbc1.jar"/>
            </lib>
            <classes dir="build/main"/>
        </war>
    </target>
</project>
Cristian Sevescu
  • 1,364
  • 8
  • 11