Acceleo M2T-将时间戳记写入生成的文件 [英] Acceleo M2T - Write timestamp into a generated file

查看:64
本文介绍了Acceleo M2T-将时间戳记写入生成的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过使用在* .mtl文件中定义的不同Acceleo模板来生成一些文件.

I am generating some files by using different Acceleo templates defined into a *.mtl file.

在这些文件的顶部,我需要编写类似以下内容的文件:

At the top op these files I need to write something like:

#-----------------------------------------------------------------------------
# Project automatically generated by XXX at (add timestamp here)
#-----------------------------------------------------------------------------

每次生成文件时如何动态生成此时间戳?

How could I generate this timestamp dynamically each time I generate the files?

谢谢!

我如下所述解决了这个问题.

I solved this as described below.

module 声明之后,添加 query 声明:

Just after the module declaration, add query declarations:

[module generate('platform:/resource/qt48_model/qt48_xmlschema.xsd') ]
[comment get timestamp/]
[query public getCurrentTime(c : OclAny) : String =
invoke('org.eclipse.acceleo.qt_test_api.generator.common.GenerationSupport', 'getCurrentTime()', Sequence{}) /]

然后,创建一个名为 GenerationSupport 的类,并添加一个名为 getCurrentTime()的方法:

Then, create a class called GenerationSupport and add a method called getCurrentTime():

package org.eclipse.acceleo.qt_test_api.generator.common;

import java.sql.Timestamp;

public class GenerationSupport {

public String getCurrentTime(){
    java.util.Date date = new java.util.Date();
    Timestamp ts = new Timestamp(date.getTime());
    return ts.toString();
}}

推荐答案

尝试如下操作:

[query public getCurrentTime(traceabilityContext : OclAny): 
    String = invoke('yourPackage.YourJavaClass', 'getCurrentTime()', Sequence{})
/]

然后在您的Java类中,声明具有以下功能的方法:

And in your Java class, declare a method with this functionality:

public String getCurrentTime(){
  return customDate;
}

其中"customDate"应为自定义格式的字符串:新的Date().toString(),使用mm/dd/yyyy格式或您想要的任何格式.

Where "customDate" should be a String in your custom format: new Date().toString(), use of formats mm/dd/yyyy or whatever you want.

请不要忘记添加包含此Java类的包以将包导出到MANIFEST.MF

Please, don't forget to add the package which contains this Java class to export packages in MANIFEST.MF

祝你好运!

这篇关于Acceleo M2T-将时间戳记写入生成的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆