01 November 2007

CompilationTask

The C preprocessor contains some predefined macros that can be used to identify the date when your pogram was compiled:

(...)
printf("%s Compiled on %s at %s.\n",argv[0],__DATE__,__TIME__);
(...)


Java has no preprocessor, and is missing this kind of information: I wrote a custom ant task generating a java file called Compilation.java and containing all the needed informations.


Compilation:
<taskdef name="compileInfoTask"
classname="org.lindenb.ant.CompileInfoTask"
classpath="build/ant"/>
(...)
<compileInfoTask name="Pubmed2Wikipedia" package="org.lindenb.util" dir="build/compile"/>




Result:


package org.lindenb.util;
import java.util.GregorianCalendar;
public class Compilation
{
private Compilation() {}
public static String getName() { return "Pubmed2Wikipedia";}
public static String getPath() { return "~/lindenb";}
public static String getUser() { return "pierre";}
public static GregorianCalendar getCalendar() { return new GregorianCalendar(2007,10,1,22,30,11);}
public static String getDate() {return "2007-11-01 at 22:30:11"; }
public static String getLabel() { return (getName()==null?"":getName()+" : ")+"Compiled by "+getUser()+" on "+getDate()+" in "+getPath();}
public static void main(String args[]) { System.out.println(getLabel());}
}


The source code is available here:

http://lindenb.googlecode.com/svn/trunk/src/java/org/lindenb/ant/CompileInfoTask.java


Pierre

3 comments:

Paul Guermonprez said...

it is not present in the class file because it is supposed to be in the jar header file.

Pierre Lindenbaum said...

But anybody can unpack the jar, edit the manifest and re-pack the jar. Moreover the information in the manifest is more complicated to extract than calling:

Compilation.getLabel()

Paul Guermonprez said...

true, class level is more precise.
but jar info is meant to be extracted easily too with java.util.jar.Manifest

if security is your concern, you can even sign the class binary (and your custom information in the process) and enter the signature hash in the jar header.

while in the compilation/loading stuff, creating a custom classloader can be interesting too, just in case you want to store your classes in a database or a non supported place.

good luck