Preprocessing source code as a part of a maven build

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.

Asked by: Lydia184 | Posted: 28-01-2022

Answer 1

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

Answered by: Daryl288 | Posted: 01-03-2022

Answer 2

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:

               <!-- Put the code to run the program here -->

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.

Answered by: Ryan231 | Posted: 01-03-2022

Answer 3

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

Answered by: Connie636 | Posted: 01-03-2022

Similar questions

java - Does anyone know how succesfully use preprocessing on J2ME?

I'll try to better explain my problem. Using Eclipse and MTJ (Mobile Tools for Java) plugin you can set some directives for the preprocessor in order to create different builds of your code, like in C/C++. My problem is that i'm unable to use this feature. I mean, when i build my sources, the resulting output contains every line of code, even ones contained within //#mdebug info ... //#enddebug

java - Preprocessing with Javacc/ push front some chars in the stream?

Using javacc can I push some new characters in front of the inputstream ? for example let's say that my parser parses the following syntax: #define Paragraphs "Paragraph+" #define Volume "(Title,(Chapter,${Paragraphs})+)" Book=${Volume}+; How can I tell javacc that its scanner should preprocess ${Volume} to (Title,(Chapter,Paragraph+)+) ...

java - Preprocessing the server response before passing it on to Axis

I'm in the very peculiar position of trying to access a SOAP service whose answers are wrapped in HTML. This is a bug with the service provider ( SourceForge ) , acknowledged for seven months, with no ETA so far. Nitty-gritty details. If I can intercept the actual response before passing it on to Axis, I would be able t...

java - Improving performance of preprocessing large set of documents

I am working on a project related to plagiarism detection framework using Java. My document set contains about 100 documents and I have to preprocess them and store in a suitable data structure. I have a big question that how am i going to process the large set of documents efficiently and avoiding bottlenecks . The main focus on my question is how to improve the preprocessing performance. Thanks Regards Nu...

java - how to use ant in eclipse for preprocessing

I need to know how to use an an Ant tool for preprocessing in Blackberry. I have a project which needs to be used in 4.6 and 5.0 versions of Blackberry, since JDE4.6 doesn't support preprocessor. I need to know how we can use Ant tool in Eclipse? Any sites or example will do. I referred this site.

itext - Document preprocessing libraries

I'm looking for libararies (and guides) for document preprocessing. I mean, for example like generating PDF, MS Office (Excel/Word), Open Office format files. I've seen that there's popular especially Apache POI and iText. Do you know any book that describes doc preprocessing libaries (for iText I found "iText in Action")? I also would like to compare libraries. Any clues?

java - hyphenation preprocessing

I need some leads for tools in PHP and/or java (Spring + Hibernate currently) to use for hyphenation of content. I have some text content in included files and some in a database. All text is utf-8 encoded and I need soft hyphens as the support for that is common in most browsers. So this stored original: &lt;p&gt; These words need hyphenation&lt;/p&gt; would turn up something lik...

java - Is Drools suitable for preprocessing a Resource Allocation Problem_

I have a quite big resource allocation problem to compute. I have just discovered Drools and I would like to know if it is a good candidate to work as a preprocessor and generate a list of forbidden allocations, based on some user rules. The Problem I have an optimisation engine that can allocate some activities to some resources, wh...

java - Json preprocessing performance issue

I have a question here on how to pre process java. I implemented some suggestions and i am able to get it to work. Only concern is performance. Json pre processing in java I experimented two ways: one way is using gson i dumped it into hash map. then in getters i look into map to read. It took 12 seconds to do it....

java - Preprocessing data before transforming it into XML?

I am using JAXB to save my objects to XML. I have one problem with this though: One of my classes has a String myclass.getFilePath() // returns an absolute file path which represents an absolute file path. Before writing this file path to an XML file, I want to relativize it, so I need some kind of preprocessing on my getter. Is that possible using JAXB? I know I could modify my ...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)