tags:

views:

242

answers:

4

I need to automate getting the compilation date in a specific format into one Java Source file, like the C compilers DATE define, How?

+7  A: 

The standard Java compiler has no way of doing this (I don't think C compilers do either - I'm guessing that it is the pre-processor that gives DATE, FILE etc)

You'll have to write a source file with the date as a string into it - look at ant's copy task with a filter

<copy file="pre-src/Version.java" toFile="src/Version.java">
  <filterset>
    <filter token="DATE" value="${TODAY}"/>
  </filterset>
</copy>

then in your source

public class Version {
    public static final String date = "@DATE@";
}

To get the date in various formats into the ant property TODAY look at the Tstamp task.

Duncan McGregor
You need to call the <tstamp/> task before using ${TODAY}. Otherwise, it works like a charm.
Ayman
+1  A: 

You could do this using aspectj compile time weaving to initialise a variable (you would obviously want to do the assignment by converting the date in your aspect to a string and then have your code parse that string).

Of course there is also nothing stopping you actually using a c pre-processor on a java file.

vickirk
+1  A: 

Have you considered annotation processors? You can run them before compilation with javac and then manually add date to the file if there was certain annotation.

tulskiy