Acceleo M2T - 将时间戳写入生成的文件

时间:2014-09-18 08:09:27

标签: eclipse acceleo ocl ecore

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

在顶部op这些文件我需要写一些像:

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

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

谢谢!

编辑:我解决了这个问题,如下所述。

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

[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()的方法:

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();
}}

2 个答案:

答案 0 :(得分:2)

尝试这样的事情:

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

在Java类中,使用此功能声明一个方法:

public String getCurrentTime(){
  return customDate;
}

“customDate”应该是自定义格式的字符串:  new Date()。toString(),使用格式mm / dd / yyyy或任何你想要的。

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

祝你好运!

答案 1 :(得分:0)

您必须使用名为" service"的内容。 它基本上只是一个类中的公共方法,它将日期作为String返回,格式化为您想要的方式。 查看加速教程,看看如何使用服务,一切都在那里。