java - How to generate source code from template with Maven? -


i have file list of tokens:

tokens.txt

foo bar baz 

and template file:

template.txt

public class @token@myinterface implements myinterface {      public void dostuff() {         // first line of generated code         // second line of generated code      } } 

i want generate following source code files target/generated-sources/my/package:

  • foomyinterface.java
  • barmyinterface.java
  • bazmyinterface.java

one of generated source files this:

foomyinterface.java

public class foomyinterface implements myinterface {      public void dostuff() {         // first line of generated code         // second line of generated code      } } 

how can maven ?

what you're looking called filtering. you can read more here. can see, you'll have change way you're doing things. variables defined differently. you'll want rename file .java.

but you've got problem: take source file , replace variables literals, wont compile .java file when build project. assuming want that, here's tutorial on how. i'm going inline of tutorial in case disappears day:

example source file:

public static final string domain = "${pom.groupid}"; public static final string wcb_id = "${pom.artifactid}"; 

the filtering:

<project...>   ...   <build>     ...     <!-- configure source files resources filtered       custom target directory -->     <resources>       <resource>         <directory>src/main/java</directory>         <filtering>true</filtering>         <targetpath>../filtered-sources/java</targetpath>       </resource>       <resource>         <directory>src/main/resources</directory>         <filtering>true</filtering>       </resource>     </resources>   ...   </build> ... </project> 

now change directory in maven finds source files compile:

<project...>   ...   <build>     ...       <!-- overrule default pom source directory match             our generated sources compiler pick them -->       <sourcedirectory>target/filtered-sources/java</sourcedirectory>   ...   </build> ... </project> 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -