0

I have a WAR archive compiled with maven. I want to insert the compilation timestamp in a jsf page.

How can I change a string in a jsf page at compilation time ? Example

<div>Compiled at (copilationTime)</div>

Must became

<div>Compiled at 2017-01-01 15:50</div>

If it is too complicate, I have an applicationscooped bean and i wrote

<div>Compiled at ${MyApp.compilationTime}</div>

But in my class how can i set 'xxxx'?

public class MyApp{
   String compilationTime = 'xxxx';

   public String getCompilationTime(){
      return compilationTime;
   }

}
Daniele Licitra
  • 1,520
  • 21
  • 45
  • 1
    I don't know if it is the exact thing you are requesting but you can generate a package file (called version.properties) with maven (in fact, with maven buildnumber plugin for example: http://www.mojohaus.org/buildnumber-maven-plugin/) and then retrieve the information from the file content (not from a variable). – Cristian Ramon-Cortes Jun 28 '17 at 15:34

3 Answers3

1

I think you're talking about a variation of resource filtering via maven-resources-plugin. The idea is this:

  • You ask Maven to filter a certain resource (either a src/main/resources resource, or a variation of it, via maven-resources-plugin, a src/main/webapp resource via maven-war-plugin) in which you have placed the Maven pre-defined variable ${maven.build.timestamp}
  • You make sure that the filtered resource is included in your project
  • You read that resource either via Class.getResourceAsStream (or a variation of it), or via servlet mechanisms (depending where you place it).
  • You use the value in your JSF.

Please note that you can ask Maven to replace a filter a variable directly in your JSF and cut some steps.

Andrei
  • 1,613
  • 3
  • 16
  • 36
0

From this answer you can get a reference to a file:

final File classFile = new File(MyApp.class.getProtectionDomain().getCodeSource().getLocation().getPath());

and then from here you get the property:

BasicFileAttributes attr = Files.readAttributes(classFile, BasicFileAttributes.class);
String compilationTime = attr.creationTime().toString();
Kudin
  • 456
  • 4
  • 7
  • 1) The file creation time and compilation time are not necessarily the same thing. Files can be copied. 2) This won't work if the class is loaded from a JAR file. – Stephen C Jan 26 '20 at 10:13
0

You can use @CompileTime from Kolobok and create a field

@CompileTime
private static long ct; // This field will contain compilation time in ms