Java preprocess phase

I am writing a Java application that needs a lot of static data that is stored in many types of enumerations. Since I would like to use a convenient way to configure this data using, for example, xml or json files, but I am not allowed to do this directly with enums, I was looking for a way to elegantly do this.

Maybe a good solution would be to have a separate java program that reads xml files and creates java sources, which are then compiled with the rest of the sources. My doubt is how to automate this process autonomously (for example, ant?) And how easy it is to integrate it with eclipse so that it automatically runs when working with a project. Something similar to what I'm already looking for? Any suggestion to solve my problem?

Thanks!

+4
source share
5 answers

Create a project whose sole task is to create Java from your sources. Verify that the generation phase is performed using Ant.

Now wrap this project in eclipse and use your own ant constructor, which invokes the target in the existing build.xml .

This is a standard part of our dev framework, so it definitely works.

+1
source

If the elements and the general structure are somehow corrected (and what most depends on the attribute values), you can consider defining an enumeration with one record for each of your elements and let the enumeration fill its own constants with data reading from an external source (XML / JSON) - at boot time or on demand.

+2
source

You can have ant seamlessly integrate with eclipse to achieve:

In the properties of the open Eclipse project, go to "Builders", click "Create ...", select "Ant Builder", select the build file, go to the "Goals" tab and click "Set Goals ... msgstr" for "Auto Build" . Choose your desired goal and you're done. The target will run every time you save the original file (if "Build Automatically" is selected).

+1
source

You can write a maven plugin that generates code. There are some plugins that do this. It will not work automatically, but you can connect it to the maven standard lifecycle so that it runs just before compilation. Recently, I recently did something.

+1
source

Have you considered including XML files in your jar and loaded them at launch on cards that use the enumeration as a key?

0
source

Source: https://habr.com/ru/post/1336519/


All Articles