Search code examples
javacode-generationfreemarkerfmpp

Java Code Generation - Is there a way to dymanically generate folders with the names you want?


I have been trying out fmpp for code generation. But I haven't been able to achieve one of the function that I want at this moment, which is dynamically replace the folder name and file name.

For example, if I have a folder with file as follows:

/template/src/main/java/com/${project_name}/${project_name}Test.java

it does not replace the ${project_name} value of file and folder when it execute and copy it over to the output generated directory.

Just wonder if anyone has an insight to this problem, or suggest any better tools that can do this.


Solution

  • While FMPP has no such feature built in (good idea BTW), you can achieve that if you add this to the FMPP configuration:

    borders: [
      header("\
        <#if pp.outputFile?contains(r'${')>\
          <#assign finalOutputFile>/<@pp.outputFile?interpret /></#assign>\
          <@pp.renameOutputFile name=finalOutputFile />\
        </#if>\
        ", **/*)
    ]