【问题标题】:Set current compilation time in a java class or jsf page在 java 类或 jsf 页面中设置当前编译时间
【发布时间】:2017-06-28 15:28:49
【问题描述】:

我有一个用 maven 编译的 WAR 存档。 我想在 jsf 页面中插入编译时间戳。

如何在编译时更改 jsf 页面中的字符串? 示例

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

必须变成

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

如果太复杂的话,我有一个applicationscooped bean,我写了

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

但是在我的课堂上我如何设置'xxxx'?

public class MyApp{
   String compilationTime = 'xxxx';

   public String getCompilationTime(){
      return compilationTime;
   }

}

【问题讨论】:

  • 我不知道这是否是您要求的确切内容,但您可以使用 maven 生成一个包文件(称为 version.properties)(实际上,使用 maven buildnumber 插件,例如:@987654321 @) 然后从文件内容中检索信息(而不是从变量中)。

标签: java maven jsf


【解决方案1】:

我认为您是在谈论通过maven-resources-plugin 进行资源过滤的一种变体。思路是这样的:

  • 您要求 Maven 过滤某个资源(src/main/resources 资源或它的变体,通过 maven-resources-pluginsrc/main/webapp 资源通过 maven-war-plugin),您在其中放置了 Maven 预定义变量 ${maven.build.timestamp}
  • 您确保过滤后的资源包含在您的项目中
  • 您可以通过Class.getResourceAsStream(或其变体)或通过 servlet 机制(取决于您放置它的位置)读取该资源。
  • 您使用 JSF 中的值。

请注意,您可以要求 Maven 直接在您的 JSF 中替换过滤器变量并减少一些步骤。

【讨论】:

    【解决方案2】:

    this answer你可以得到一个文件的引用:

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

    然后从here 获得属性:

    BasicFileAttributes attr = Files.readAttributes(classFile, BasicFileAttributes.class);
    String compilationTime = attr.creationTime().toString();
    

    【讨论】:

    • 1) 文件创建时间和编译时间不一定是一回事。可以复制文件。 2) 如果类是从 JAR 文件加载的,这将不起作用。
    【解决方案3】:

    您可以使用@CompileTime from Kolobok 并创建一个字段

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      相关资源
      最近更新 更多