Preprocessing source code as a part of a maven build

微笑、不失礼 提交于 2019-11-28 07:36:05

This is something that is very doable and I've done something very similar in the past.

An example from a project of mine, where I used the antrun plug-in to execute an external program to process sources:

  <build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-antrun-plugin</artifactId>
      <executions>
        <execution>
          <id>process-sources</id>
          <phase>process-sources</phase>
          <configuration>
            <tasks>
               <!-- Put the code to run the program here -->
            </tasks>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Note the tag where I indicate the phase where this is run. Documentation for the lifecycles in Maven is here. Another option is to actually write your own Maven plug-in that does this. It's a little more complex, but is also doable. You will still configure it similarly to what I have documented here.

Igor Maznitsa

There is a Java preprocessor with support of MAVEN: java-comment-preprocessor

Maven plugins can hook into the build process at pre-compile time yes, as for whether or not any existing ones will help I have no idea.

I wrote a maven plugin a couple of years ago as part of a university project though, and while the documentation was a bit lacking at the time, it wasn't too complicated. So you may look into rolling your own, there should be plenty of open source projects you can rip ideas or code from (ours was BSD-licenced for instance...)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!