Source Filtering in Maven

I wrote a small BeanShell script that replaces "__LINE__" actual line number in the source code. It works well in Ant.

I am looking for a way to filter the source code in Maven so that my BeanShell script can generate a new source code directory, which is then compiled.

I know about filtering resource files. Are there any similar features for the source code?

+12
maven-2 filtering
Nov 05 '10 at 13:19
source share
1 answer

The source code of the filter has been fixed, which was somewhat complicated a few months ago, but now there is a standard plug-in in the MOJO project. Now you can do this with the classic plugin declaration.

To filter the source code (for example, if you want to have a constant in your Java code to extract the version of the project or artifactId), you should now use templating-maven-plugin .

  • Put your code that should be filtered during build in src/main/java-templates , as you usually do in src/main/java for unfiltered sources. Use ${project.version} or any other property coming from the POM in your code.

  • Just put something like:

     <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>templating-maven-plugin</artifactId> <version>1.0-alpha-3</version> <!-- Be sure to use the last version. Check on the website plugin --> <executions> <execution> <id>filter-src</id> <goals> <goal>filter-sources</goal> </goals> </execution> </executions> </plugin> 
  • Do It:-). The code that you put inside src/main/java-templates is filtered and added to the classpath.

Using is very simple (see example here ).

This reflects the idea of ​​a Maven configuration agreement much better. You basically replace dozens of XML lines and some hacks to do something clean.

Side note: this works great with Eclipse, for example.

+19
Aug 26 '13 at 21:01
source share



All Articles