如何将java源代码保存在xml文件中,并在IDE中编辑/编译? [英] How to save java source code in an xml file, and edit/compile it in an IDE?

查看:386
本文介绍了如何将java源代码保存在xml文件中,并在IDE中编辑/编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个java应用程序,用户可以在其中编写/执行自己的java代码,并使用从编译jar中导入 - 即它们编写它,并由应用程序编译和运行。他们还可以保存此代码(以及他们正在使用的各种其他信息) - 目前它保存到人类可读的xml文件。

We have an java application in which the user can write/execute their own java code and use imports from compiled jars - i.e. they write it, and it is compiled and run by the application. They can also save this code (along with various other information that they are using) - currently this is saved to a human-readable xml file.

我想能够在IDE中使用那些保存xml文件(主要是Intellij),所以如果用户在IDE中的编译jar中更改东西,这些更改也可以在保存xml文件中找到。

I want to be able to use those save xml files in an IDE (principally, Intellij), so that if the user changes things in their compiled jar in the IDE, these changes can also be picked up in the save xml file.

例如,如果保存文件从编译的jar中使用了一个类,它可能有以下import:

For example, if a save file used a class from the compiled jar, it may have the following import:

import com.company.project.package.subpackage.MyClass;

让我们说类被移动,所以导入是:

Let's say that class was moved, so the import was:

import com.company.project.package.subpackage.subsub.MyClass;

...这将更改使用该类并导入的所有保存xml文件 - IDE将为编译项目中的所有其他用途。

...this would change all the save xml files that used that class and import - just as the IDE would for all the other usages in the compiled project.

(这个和其他的例子出现,因为编译的jar不断在开发和使用上述应用程序。)

(This, and other examples, arises because the compiled jar is both constantly in development and in use using the aforementioned application.)

此时,如果我将保存xml文件添加到IDE中的子项目,用户可以手动编辑保存文件,可能利用'find / replace'或'search for在文本功能中的使用。这比没有什么好,但仍然是一个相当复杂的过程。此外,没有检查保存文件中的代码是否与编译项目中的代码一致。

At the moment, if I were to add the save xml files to a sub-project in the IDE, the user can edit the save files manually, possibly taking advantage of 'find/replace' or 'search for usage in text' functions. This is better than nothing, but still a rather involved/complicated process. Also, there is no checking that the code in the save files are consistent with the code in the compiled project.

我考虑的一种方法是脚本或测试类,它将解压缩保存xml文件,将java代码写入java文件,然后尝试并编译(可能运行/测试)这些java文件。

One approach that I am considering is a script or a test class that would unpack the save xml file, writing the java code to java files, and then try and compile (and possibly running/testing) those java files.

进一步的步骤是编写一个maven插件(我们使用maven来构建循环)或者一个ant脚本(ant仍然有它的使用...)这可能是我们构建过程的这一部分 - 即你不能编译项目,而不保证其子项目中的所有保存XML文件也编译。

A further step would be to write a maven plugin (we use maven for our build cycle) or an ant script (ant still has its uses...) to do this, and possibly make this part of our build process - i.e. you cannot compile the project without ensuring all of the save xml files in its sub-project also compile.

    $ b
  • Does this seem like a reasonable approach?
  • Are there alternative approaches that anyone could suggest?

推荐答案


..另存为.java文件不是我正在寻找的解决方案。

..saving as a .java file is not the solution I'm looking for.

将其保存为包含1个(或多个)XML文件以及任何需要的源(根据包的路径)的Zip。您还可以轻松添加其他文件,清单,帮助文件等。

Save it as a Zip with 1 (or more) XML files as well as any source (in paths according to package) that is required. You could even include other files easily, a manifest, help files etc.

这有很多优点:


  • 它允许源&包含要对XML进行不同编码的文件

  • 将项目的所有必要部分合并为一个文件,而不需要任何跳跃,使一种格式适合另一种格式。 / li>
  • 它允许对数据适当的不同压缩级别(例如,文本/ XML压缩很好,而序列化图像不)。

这篇关于如何将java源代码保存在xml文件中,并在IDE中编辑/编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆