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?
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 }.
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.
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.
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.
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.
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With