开发者

Java preprocess phase

开发者 https://www.devze.com 2023-02-05 20:35 出处:网络
I\'m writing a Java application that needs a lot of static data that is stored in many enum types. Since I would like an user-friendly way to customize this data using for example开发者_C百科 xml or j

I'm writing a Java application that needs a lot of static data that is stored in many enum types. Since I would like an user-friendly way to customize this data using for example开发者_C百科 xml or json files but I'm not allowed to do it directly with enums I was looking for a way to elegantly do it.

Maybe a good solution would be to have a separate java program that reads the xml files and produces the java sources that are then compiled with the remaining part of the sources. My doubs is how to automatize this process in a stand alone way (eg ant?) and how to integrate it seamlessly with eclipse so that it is autOmatically done when I'm working with the project.. Does anything similar to what I'm looking already exists? Any suggestion to solve my problem?

Thanks!


If the items and the overall structure are somehow fixed (and what varies most is the values of the attributes), you could consider defining the enum with one entry for each of your items and let the enum populate its own constants with data read from an external source (XML / JSON) -- at load time or on demand.


Create a project whose sole job is to generate java from your sources. Make sure that the generation phase is done by Ant.

Now, wrap this project into eclipse and use a custom ant builder, that calls the target in your already existing build.xml.

This is a standard part of our dev infrastructure, so this definitely works.


You can write a maven plugin that generates the code. There are some plugins that do that. It won't work automatically, but you can connect it to the standard maven lifecycle so it gets executed just before compile. I just did something like that recently.


You can have ant seamlessly integrate with eclipse to achive that:

In Eclipse open project properties, go to "Builders", click "New...", select "Ant Builder", select a build file, go to "Targets" tab and click "Set Targets..." for "Auto Build". Select the desired target and you are done. The target will run every time you save a source file (if "Build Automatically" is selected).


Have you considered including the XML files in your jar, and loading them on startup into maps that use the enum as a key?

0

精彩评论

暂无评论...
验证码 换一张
取 消