如何在Maven中使用模板代码生成器(例如,freemarker)? [英] How do I use a template code generator (eg freemarker) in Maven?

查看:143
本文介绍了如何在Maven中使用模板代码生成器(例如,freemarker)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何将 Freemarker (或替代方案)构造为Maven项目中的模板代码生成器?我对Maven还是很陌生,希望能获得一些帮助.

How would you structure Freemarker (or an alternative) as a templating code generator into a Maven project? I'm pretty new to Maven and would appreciate some help.

我想从项目中的模板生成一些代码. [a]

I want to generate some code from templates in my project. [a]

不是写我自己的,而是谷歌搜索了 freemarker ,它似乎由

Rather than write my own, googling found freemarker which appears to be used by Spring which is a good enough reference for me, though as I haven't started with it yet, any other suggestions that work well with Maven would be appreciated too.

此网站告诉我如何将其添加为依赖项到我的pom.xml. 这个SO问题告诉我生成的源应该去哪里.我无法解决的是如何将它们结合在一起,因此我从模板生成了生成的源,然后将生成的源用作常规源,如编译,测试,jar,javadoc等的常规源. Maven中的Java模板代码生成器,可以帮上忙吗?

This website tells me how to add it as a dependency to my pom.xml. This SO question tells me where the generated sources should go. What I can't work out is how to tie it all together, so I get my generated sources generated from the templates, and then my generated sources used like regular sources for compile, test, jar, javadoc etc. Has anyone else used a template code generator for java within maven and could help?

[a]我知道泛型将是通常的解决方案,实际上我正在使用它们,但是我必须使用模板来处理原始情况,而不会引入复制/粘贴错误.请相信我:-)

[a] I know Generics would be the usual solution, and in fact I'm using them, but I have to use templates to cope with the primitive cases, without introducing copy/paste errors. Please trust me on this :-)

推荐答案

我为此目的编写了一个maven插件.它使用FreeMarker预处理器.

I had written a maven plugin for this purpose. It uses the FreeMarker Pre Processor.

在pom.xml的片段中突出显示了其用法:

Heres the fragment from pom.xml highlighting its usage:

<plugins>
    <plugin>
        <configuration>
            <cfgFile>src/test/resources/freemarker/config.fmpp</cfgFile>
            <outputDirectory>target/test/generated-sources/fmpp/</outputDirectory>
            <templateDirectory>src/test/resources/fmpp/</templateDirectory>
        </configuration>
        <groupId>com.googlecode.fmpp-maven-plugin</groupId>
        <artifactId>fmpp-maven-plugin</artifactId>
        <version>1.0</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

在此cfgFile是保存FMPP的配置文件的路径. (如果您不使用FreeMarker中传递的任何特殊数据,那么一个空文件就足够了) templateDirectory是保存FreeMarker模板的位置. outputDirectory是您要在其中生成输出文件的地方.

Here the cfgFile is the path where you keep the config file for FMPP. (if you are not using any special data passing in FreeMarker then an empty file will be enough) templateDirectory is where you keep the FreeMarker templates. outputDirectory is where you want the output files to be generated.

我正在写详细的文档,突出显示插件的用法,并将更新项目网站.

I am in process of writing a detailed documentation highlighting the plugins usage and will update the project website accordingly.

这篇关于如何在Maven中使用模板代码生成器(例如,freemarker)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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