Java预处理阶段

发布于 2024-10-12 23:52:12 字数 309 浏览 6 评论 0原文

我正在编写一个 Java 应用程序,需要大量存储在许多枚举类型中的静态数据。由于我想要一种用户友好的方式来使用例如 xml 或 json 文件来自定义此数据,但我不允许直接使用枚举来完成此操作,所以我一直在寻找一种优雅地完成此操作的方法。

也许一个好的解决方案是使用一个单独的 java 程序来读取 xml 文件并生成 java 源代码,然后将其与源代码的其余部分一起编译。我的疑问是如何以独立的方式自动化这个过程(例如ant?)以及如何将其与eclipse无缝集成,以便在我处理该项目时自动完成。.是否有与我类似的事情寻找已经存在?有什么建议可以解决我的问题吗?

谢谢!

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

明媚如初 2024-10-19 23:52:12

如果项目和整体结构在某种程度上是固定的(变化最大的是属性的值),您可以考虑为每个项目定义一个条目,并让枚举使用从外部源(XML / JSON)——在加载时或按需。

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.

凝望流年 2024-10-19 23:52:12

创建一个项目,其唯一的工作是从源代码生成 java。
确保生成阶段是由 Ant 完成的。

现在,将此项目包装到 Eclipse 中并使用自定义 ant 构建器,该构建器调用现有 build.xml 中的目标。

这是我们开发基础设施的标准部分,所以这绝对有效。

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.

涫野音 2024-10-19 23:52:12

您可以编写一个生成代码的 Maven 插件。有一些插件可以做到这一点。它不会自动工作,但您可以将其连接到标准 Maven 生命周期,以便它在编译之前执行。
我最近刚刚做了类似的事情。

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.

绅刃 2024-10-19 23:52:12

您可以将 ant 与 eclipse 无缝集成,以实现以下目的:

在 Eclipse 中打开项目属性,转到“Builders”,单击“New...”,选择“Ant Builder”,选择构建文件,转到“Targets”选项卡,然后单击“自动构建”的“设置目标...”。选择所需的目标即可完成。每次保存源文件时,目标都会运行(如果选择“自动构建”)。

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

谁的新欢旧爱 2024-10-19 23:52:12

您是否考虑过将 XML 文件包含在 jar 中,并在启动时将它们加载到使用枚举作为键的映射中?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文