Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering source code in Maven

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?

like image 887
Ralph Avatar asked Nov 05 '10 13:11

Ralph


People also ask

What is Maven resource filtering?

Resource Filtering. You can use Maven to perform variable replacement on project resources. When resource filtering is activated, Maven will scan resources for property references surrounded by ${ and }.

What is Maven clean plugin?

The Maven Clean Plugin, as the name implies, attempts to clean the files and directories generated by Maven during its build. While there are plugins that generate additional files, the Clean Plugin assumes that these files are generated inside the target directory.

What is Maven resources plugin?

The Resources Plugin handles the copying of project resources to the output directory. There are two different kinds of resources: main resources and test resources.

What is Maven war plugin?

The Maven WAR plugin is responsible for collecting and compiling all the dependencies, classes, and resources of the web application into a web application archive. There are some defined goals in the Maven WAR plugin: war: This is the default goal that is invoked during the packaging phase of the project.


1 Answers

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.

like image 139
Baptiste Mathus Avatar answered Nov 19 '22 06:11

Baptiste Mathus