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

后端 未结 2 1975
终归单人心
终归单人心 2021-02-19 14:55

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 wa

相关标签:
2条回答
  • 2021-02-19 15:54

    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.

    0 讨论(0)
  • 2021-02-19 15:58

    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.

    0 讨论(0)
提交回复
热议问题