Preprocessing source code as a part of a maven build
Asked Answered
C

3

15

I have a lot of Java source code that requires custom pre-processing. I'd like rid of it but that's not feasible right now so I'm stuck with it. Given that I have an unfortunate problem that shouldn't have existed in the first place, how do I solve it using maven?

(For the full story, I'm replacing a python-based build system with a maven one, so one improvement at a time please. Fixing the non-standard source code is harder, and will come later.)

Is it possible using any existing Maven plugins to actually alter the source files during compile time? (Obviously leaving the original, unprocessed code alone)

To be clear, by preprocessing I mean preprocessing in the same sense as antenna or a C compiler would preprocess the code, and by custom I mean that it's completely proprietary and looks nothing at all like C or antenna preprocessing.

Chlorella answered 9/10, 2008 at 14:35 Comment(0)
O
8

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.

Ong answered 9/10, 2008 at 21:34 Comment(2)
That's very useful, thanks. I think the part I'm struggling with is how to process the code to a copy of that code, and then have maven compile the copy instead of the original. I think perhaps I need to understand maven basics better though.Chlorella
-1 : you are not answering the question, just providing means to achieve it. In other words, what do you put in tasks ?Tasso
M
9

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

Monjan answered 5/12, 2011 at 13:43 Comment(0)
O
8

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.

Ong answered 9/10, 2008 at 21:34 Comment(2)
That's very useful, thanks. I think the part I'm struggling with is how to process the code to a copy of that code, and then have maven compile the copy instead of the original. I think perhaps I need to understand maven basics better though.Chlorella
-1 : you are not answering the question, just providing means to achieve it. In other words, what do you put in tasks ?Tasso
U
2

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...)

Underwaist answered 9/10, 2008 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.