Java 有没有办法从 Eclipse 自动生成 ant build.xml 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4185921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:49:12  来源:igfitidea点击:

Any way to generate ant build.xml file automatically from Eclipse?

javaeclipseantbuild.xml

提问by Joanis

From Eclipse, I found I can easily export an Ant build file for my project. It provides references to 3rd party libraries and some base targets. I'm using it from my global build file. The only thing that bothers me about this, is that if something is modified in the project structure (like adding a new 3rd party library), we have to think (yes that can be hard sometimes!) about regenerating that build.xml file. I'm wondering if anyone here knows a way to have it updated automatically.By "automatically" I mean that it would not be necessary to explicitly ask Eclipse to regenerate it every time it's needed. I don't know what could be the trigger though...

从 Eclipse 中,我发现我可以轻松地为我的项目导出 Ant 构建文件。它提供了对 3rd 方库和一些基本目标的引用。我从我的全局构建文件中使用它。唯一让我烦恼的是,如果项目结构中的某些内容被修改(例如添加新的 3rd 方库),我们必须考虑(是的,有时可能很难!)重新生成该 build.xml 文件。我想知道这里是否有人知道自动更新的方法。“自动”是指没有必要在每次需要时明确要求 Eclipse 重新生成它。我不知道可能是什么触发虽然...

Any thoughts or knowledge on this?

对此有任何想法或知识吗?

Thanks!

谢谢!

MJ

兆焦

回答by Guillaume

Right-click on an Eclipse project then "Export" then "General" then "Ant build files". I don't think it is possible to customise the output format though.

右键单击 Eclipse 项目,然后单击“导出”,然后单击“常规”,然后单击“Ant 构建文件”。不过,我认为无法自定义输出格式。

回答by Jan Schiefer

Take a look at the .classpath file in your project, which probably contains most of the information that you want. The easiest option may be to roll your own "build.xml export", i.e. process .classpath into a new build.xml during the build itself, and then call it with an ant subtask.

查看项目中的 .classpath 文件,其中可能包含您需要的大部分信息。最简单的选择可能是滚动您自己的“build.xml 导出”,即在构建过程中将 .classpath 处理为新的 build.xml,然后使用 ant 子任务调用它。

Parsing a little XML sounds much easier to me than to hook into Eclipse JDT.

对我来说,解析一点 XML 听起来比挂接到 Eclipse JDT 容易得多。

回答by Tristan Ratchford

I have been trying to do the same myself. What I found was that the "Export Ant Buildfile" gets kicked off in the org.eclipse.ant.internal.ui.datatransfer.AntBuildfileExportPage.java file. This resides in the org.eclipse.ant.ui plugin.

我自己也一直在尝试做同样的事情。我发现“导出 Ant Buildfile”在 org.eclipse.ant.internal.ui.datatransfer.AntBuildfileExportPage.java 文件中被启动。它位于 org.eclipse.ant.ui 插件中。

To view the source, use the Plug-in Development perspective and open the Plug-ins view. Then right-click on the org.eclipse.ant.ui plugin and select import as > source project.

要查看源代码,请使用插件开发透视图并打开插件视图。然后右键单击 org.eclipse.ant.ui 插件并选择 import as > source project。

My plan is to create a Java program to programmatically kick off the ant buildfile generation and call this in an Ant file every time I build by adding the ant file to the builders of my projects (Right-click preferences on a projet, under the builders tab).

我的计划是创建一个 Java 程序,以编程方式启动 ant 构建文件的生成,并在每次构建时通过将 ant 文件添加到我的项目的构建器(在构建器下的项目上右键单击首选项)在 Ant 文件中调用它标签)。

回答by J2G

I'm the one who donated the Ant export filter to Eclipse. I added the auto export feature, but only to my personal plug-in eclipse2ant, which I still maintain to coordinate bug fixes.

我是将 Ant 导出过滤器捐赠给 Eclipse 的人。我添加了自动导出功能,但仅添加到我的个人插件eclipse2ant 中,我仍然维护它以协调错误修复。

Unfortunately I have no time to merge it to the official Eclipse builds.

不幸的是,我没有时间将它合并到官方的 Eclipse 版本中。

回答by scott

If all you need is the classpath entries, I do something like the following to use the eclipse build path.

如果您只需要类路径条目,我会执行以下操作以使用 eclipse 构建路径。

<xmlproperty file=".classpath" collapseAttributes="true" delimiter=";" />

Then set that value in the path

然后在路径中设置该值

<path id="eclipse.classpath">
    <pathelement path="${classpath.classpathentry.path}"/>
</path>


<target name="compile" depends="init">

    <javac srcdir="${src}" destdir="${build}" updatedProperty="compiled">
        <classpath refid="eclipse.classpath"/>
    </javac>
</target>

回答by JBishop

I've had the same problem, our work environment is based on Eclipse Java projects, and we needed to build automatically an ANT file so that we could use a continuous integration server (Jenkins, in our case).

我遇到了同样的问题,我们的工作环境基于 Eclipse Java 项目,我们需要自动构建一个 ANT 文件,以便我们可以使用持续集成服务器(在我们的例子中是 Jenkins)。

We rolled out our own Eclipse Java to Ant tool, which is now available on GitHub:

我们推出了自己的 Eclipse Java to Ant 工具,现在可以在 GitHub 上获得:

ant-build-for-java

ant-build-for-java

To use it, call:

要使用它,请调用:

java -jar ant-build-for-java.jar <folder with repositories> [<.userlibraries file>]

The first argument is the folder with the repositories. It will search the folder recursively for any .projectfile. The tool will create a build.xmlin the given folder.

第一个参数是存储库的文件夹。它将递归搜索文件夹中的任何.project文件。该工具将build.xml在给定的文件夹中创建一个。

Optionally, the second argument can be an exported .userlibraries file, from Eclipse, needed when any of the projects use Eclipse user libraries. The tool was tested only with user libraries using relative paths, it's how we use them in our repo. This implies that JARs and other archives needed by projects are inside an Eclipse project, and referenced from there.

可选地,第二个参数可以是从 Eclipse 导出的 .userlibraries 文件,当任何项目使用 Eclipse 用户库时需要该文件。该工具仅使用使用相对路径的用户库进行了测试,这就是我们在我们的存储库中使用它们的方式。这意味着项目所需的 JAR 和其他档案位于 Eclipse 项目中,并从那里引用。

The tool only supports dependencies from other Eclipse projects and from Eclipse user libraries.

该工具仅支持来自其他 Eclipse 项目和来自 Eclipse 用户库的依赖项。

回答by Premraj

  • Select File > Exportfrom main menu (or right click on the project name and select Export > Export…).
  • In the Export dialog, select General > Ant Buildfilesas follows: enter image description here

  • Click Next. In the Generate Ant Buildfilesscreen:

    • Check the project in list.
    • Uncheck the option "Create target to compile project using Eclipse compiler" - because we want to create a build file which is independent of Eclipse.
    • Leave the Name for Ant buildfile as default: build.xml
  • 从主菜单中选择文件 > 导出(或右键单击项目名称并选择导出 > 导出...)。
  • 在 Export 对话框中,选择General > Ant Buildfiles,如下所示: 在此处输入图片说明

  • 单击下一步。在生成 Ant 构建文件屏幕中:

    • 检查列表中的项目。
    • 取消选中“创建目标以使用 Eclipse 编译器编译项目”选项 - 因为我们要创建一个独立于 Eclipse 的构建文件。
    • 将 Ant 构建文件的名称保留为默认值:build.xml

enter image description here

在此处输入图片说明

  • Click Finish, Eclipse will generate the build.xmlfile under project's directory as follows:
    enter image description here
  • Double click on the build.xmlfile to open its content in Ant editor: enter image description here
  • 点击Finish,Eclipse 会build.xml在项目目录下生成如下文件:
    在此处输入图片说明
  • 双击该build.xml文件以在 Ant 编辑器中打开其内容: 在此处输入图片说明

source

来源