Filtering source code in Maven

后端 未结 1 651
别那么骄傲
别那么骄傲 2020-12-09 18:49

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

相关标签:
1条回答
  • 2020-12-09 19:21

    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.

    0 讨论(0)
提交回复
热议问题