Acceleo M2T - Write timestamp into a generated file

余生颓废 提交于 2019-12-12 04:47:23

问题


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?

Thanks!

Edit: I solved this as described below.

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{}) /]

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

回答1:


try something like this:

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

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

public String getCurrentTime(){
  return customDate;
}

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

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

Good luck!




回答2:


You'll have to use what's called a "service". It's basically just a public method in a class that will return the date as a String, formatted the way you want. Lookt at the acceleo tutorials to see how services are used, everything is there.



来源:https://stackoverflow.com/questions/25907418/acceleo-m2t-write-timestamp-into-a-generated-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!