本文介绍了将源代码作为 Maven 构建的一部分进行预处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多需要自定义预处理的 Java 源代码.我想摆脱它,但现在不可行,所以我坚持下去.鉴于我有一个不应该存在的不幸问题,我如何使用 maven 解决它?

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?

(对于完整的故事,我正在用 maven 替换基于 python 的构建系统,所以请一次改进.修复非标准源代码更难,稍后会出现.)

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

是否可以使用任何现有的 Maven 插件在编译时实际更改源文件?(显然保留原始的、未处理的代码)

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

明确地说,预处理是指与天线或 C 编译器预处理代码具有相同意义的预处理,而自定义是指它是完全专有的,看起来与 C 或天线预处理完全不同.

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.

推荐答案

这是非常可行的,我过去也做过类似的事情.

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

我的一个项目的例子,我使用antrun插件来执行一个外部程序来处理源:

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>

注意我指示运行阶段的标记.Maven 中的生命周期文档位于此处.另一种选择是实际编写您自己的 Maven 插件来执行此操作.这有点复杂,但也是可行的.您仍将按照我在此处记录的方式对其进行配置.

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.

这篇关于将源代码作为 Maven 构建的一部分进行预处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:50