16

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

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

I know about resource file filtering. Is there any similar facility for source code?

durron597
  • 31,968
  • 17
  • 99
  • 158
Ralph
  • 31,584
  • 38
  • 145
  • 282

1 Answers1

26

Filtering source code was still tricky some months ago, but there's now a standard plugin at the MOJO project. You can now do that with a classic plugin declaration.

To filter source code (for example, when you'd like to have a constant in your Java code to retrieve the project version or artifactId), you should now use the templating-maven-plugin.

  1. Put your code that should be filtered during the build under src/main/java-templates as you would normally do under src/main/java for non filtered sources. Use ${project.version} or whatever property coming from the POM in your code.

  2. 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's plugin -->
      <executions>
          <execution>
          <id>filter-src</id>
          <goals>
              <goal>filter-sources</goal>
          </goals>
          </execution>
      </executions>
    </plugin>
    
  3. Be done :-). The code you put inside src/main/java-templates gets filtered and added to the classpath.

The usage is very straightforward (see the example here).

This respects far better the idea of convention over configuration of Maven. You're basically replacing tens of XML lines and some hacks to get something done cleanly.

Side note: this works fine with Eclipse for example.

Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58
Baptiste Mathus
  • 888
  • 2
  • 11
  • 18
  • Hi, I tried following your answer above, but when I import the project into eclipse-kepler and enable eclipse's auto-build, its complaining that it cannot find the classes in `src/main/java-templates` src dir...also looking at the project source build path, the `src/main/java-templates` src dir is not there...not sure if it should be there. Just to also mention that the project builds successfully when I run `mvn install` from eclipse, but I want eclipse not to complain about and it should automatically pick processed source from `target/generated-sources/java-templates` or maybe I'm wrong? – Simple-Solution Mar 03 '14 at 10:52
  • 1
    src/main/java-templates isn't a source folder, and must not be configured as one inside the IDE. Doing this will clash with the target/generated-sources/java-templates filtered directory (which has to show as a source folder in Eclipse, which you shouldn't edit). – Baptiste Mathus Mar 13 '14 at 14:51
  • 1
    For the automatic refresh inside Eclipse, I think you're missing some M2E configuration, try adding https://gist.github.com/Batmat/9529785 in your pom.xml. Does it work better? – Baptiste Mathus Mar 13 '14 at 14:52
  • I use the build-helper-maven-plugin:add-source goal so Eclipse will add the source directory to the project when updating the project using maven. configuration/sources/source = ${project.build.directory}/generate-sources/java-templates – Aaron Roller May 30 '15 at 06:22
  • @AaronRoller bad idea: doing that, you actually add that folder to the classpath twice. The templating-maven-plugin already does it. It's just that it must be configured to run in Eclipse through M2E too. – Baptiste Mathus Sep 04 '15 at 08:25