Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a template code generator (eg freemarker) in 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.

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

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.

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] 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 :-)

like image 946
Nick Fortescue Avatar asked Oct 13 '10 14:10

Nick Fortescue


People also ask

What is the use of FreeMarker template?

FreeMarker is a template engine, written in Java, and maintained by the Apache Foundation. We can use the FreeMarker Template Language, also known as FTL, to generate many text-based formats like web pages, email, or XML files.

What is the use of FreeMarker jar?

Apache FreeMarker™ is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data.

What is FreeMarker Template Error?

FreeMarker template error appears in logs when attempting to add a Web Content Article to a page or search for it in the Search portlet. The issue only occurs when the Summary field is empty and when using the following line in the template.

How does FTL file work?

It is configurable from where FreeMarker reads the templates; commonly used options are loading from a file-system directory, from the class-path, from the servlet context ( WEB-INF/templates or such), or even from a database table. It's also possible to "load" templates directly from String objects.


1 Answers

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.

like image 165
Faisal Feroz Avatar answered Oct 02 '22 23:10

Faisal Feroz