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?
我有很多需要自定义预处理的Java源代码。我想摆脱它,但现在不可行,所以我坚持下去。鉴于我有一个不应该存在的不幸问题,我如何使用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.)
(就完整的故事而言,我正在用一个基于python的构建系统替换一个基于python的构建系统,所以请一次改进。修复非标准源代码更难,并且稍后会出现。)
Is it possible using any existing Maven plugins to actually alter the source files during compile time? (Obviously leaving the original, unprocessed code alone)
是否可以使用任何现有的Maven插件在编译期间实际更改源文件? (显然只保留原始的,未经处理的代码)
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.
需要说明的是,通过预处理,我的意思是预处理与天线相同,或者C编译器会对代码进行预处理,而且我认为它是完全专有的,看起来就像C或天线预处理一样。
3 个解决方案
#1
9
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:
我的一个项目的例子,我使用antrun插件执行外部程序来处理源:
<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.
请注意标记,其中我指示运行它的阶段。 Maven生命周期的文档就在这里。另一个选择是实际编写自己的Maven插件来执行此操作。它有点复杂,但也很可行。您仍然会按照我在此处记录的内容进行配置。
#2
8
There is a Java preprocessor with support of MAVEN: java-comment-preprocessor
有一个支持MAVEN的Java预处理器:java-comment-preprocessor
#3
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.
Maven插件可以在编译前编译时挂钩到构建过程是的,至于任何现有插件是否会帮助我不知道。
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...)
几年前我作为大学项目的一部分编写了一个maven插件,虽然当时文档有点缺乏,但它并不太复杂。所以你可以考虑自己动手,应该有很多开源项目,你可以从中获取想法或代码(例如我们的BSD许可......)
#1
9
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:
我的一个项目的例子,我使用antrun插件执行外部程序来处理源:
<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.
请注意标记,其中我指示运行它的阶段。 Maven生命周期的文档就在这里。另一个选择是实际编写自己的Maven插件来执行此操作。它有点复杂,但也很可行。您仍然会按照我在此处记录的内容进行配置。
#2
8
There is a Java preprocessor with support of MAVEN: java-comment-preprocessor
有一个支持MAVEN的Java预处理器:java-comment-preprocessor
#3
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.
Maven插件可以在编译前编译时挂钩到构建过程是的,至于任何现有插件是否会帮助我不知道。
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...)
几年前我作为大学项目的一部分编写了一个maven插件,虽然当时文档有点缺乏,但它并不太复杂。所以你可以考虑自己动手,应该有很多开源项目,你可以从中获取想法或代码(例如我们的BSD许可......)