How do I use a template code generator (eg freemarker) in Maven?

一笑奈何 提交于 2019-12-04 00:11:20
Faisal Feroz

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

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>

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.

Here is another plugin for the job: https://code.google.com/p/maven-replacer-plugin/

From the original description of the problem it sounds like you should consider creating a Maven Archetype (aka Project Template): http://maven.apache.org/archetype/maven-archetype-plugin/

And it sounds like you might want to add some properties into the equation: http://maven.apache.org/archetype/maven-archetype-plugin/examples/create-with-property-file.html

Maven Archetype functionality also provides a means of doing substitution using Apache Velocity (near enough the same as Freemarker) ... but I haven't worked that bit out yet.

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