[英]How do I use a template code generator (eg freemarker) in Maven?
您將如何將Freemarker (或替代方案)作為模板代碼生成器構建到 Maven 項目中? 我對 Maven 很陌生,希望得到一些幫助。
我想從我的項目中的模板生成一些代碼。 [一]
谷歌搜索找到了似乎被Spring使用的freemarker ,而不是自己寫,這對我來說是一個足夠好的參考,盡管我還沒有開始使用它,任何其他與 Maven 配合良好的建議也將不勝感激。
這個網站告訴我如何將它作為依賴添加到我的 pom.xml。 這個 SO question告訴我生成的源應該去哪里。 我無法解決的是如何將它們聯系在一起,所以我從模板生成我生成的源,然后我生成的源像常規源一樣用於編譯、測試、jar、javadoc 等。有沒有其他人使用過Maven 中的 Java 模板代碼生成器,可以提供幫助嗎?
[a] 我知道泛型是通常的解決方案,實際上我正在使用它們,但我必須使用模板來處理原始情況,而不會引入復制/粘貼錯誤。 請相信我:-)
為此,我編寫了一個 Maven 插件。 它使用 FreeMarker 預處理器。
下面是 pom.xml 中突出顯示其用法的片段:
<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 是您希望生成輸出文件的位置。
我正在編寫一份詳細的文檔,重點介紹插件的使用情況,並將相應地更新項目網站。
這是該工作的另一個插件: https : //code.google.com/p/maven-replacer-plugin/
從問題的原始描述看來,您應該考慮創建一個 Maven 原型(又名項目模板): http : //maven.apache.org/archetype/maven-archetype-plugin/
聽起來您可能想在等式中添加一些屬性: http : //maven.apache.org/archetype/maven-archetype-plugin/examples/create-with-property-file.html
Maven Archetype 功能還提供了一種使用 Apache Velocity(幾乎與 Freemarker 相同)進行替換的方法……但我還沒有解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.