如何在Eclipse Java项目中自动生成.jar文件
我有一个 Eclipse Java 项目。 它包含一个名为“dist”的文件夹。 该文件夹中有一个 .jar 文件。
如何在此项目中进行设置,以确保在重新编译项目中的 .java 文件之一时随时更新此 .jar 文件?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
创建一个 Ant 文件并告诉 Eclipse 构建它。 只有两个步骤,按照下面的分步说明,每个步骤都很简单。
第 1 步
创建一个 build.xml 文件并添加到包资源管理器:
Eclipse 应该类似于下面的屏幕截图。 请注意 build.xml 上的 Ant 图标。
步骤 2
右键单击项目中的根节点。
- 选择属性
- 选择建设者
- 选择新建
- 选择Ant构建
- 在“主要”选项卡中,填写 bin 文件夹中 build.xml 文件的路径。
检查输出
Eclipse 输出窗口(名为 Console)应显示构建后如下:
Create an Ant file and tell Eclipse to build it. There are only two steps and each is easy with the step-by-step instructions below.
Step 1
Create a build.xml file and add to package explorer:
Eclipse should looks something like the screenshot below. Note the Ant icon on build.xml.
Step 2
Right-click on the root node in the project.
- Select Properties
- Select Builders
- Select New
- Select Ant Build
- In the Main tab, complete the path to the build.xml file in the bin folder.
Check the Output
The Eclipse output window (named Console) should show the following after a build:
您可以定义一个 Ant 构建器,它运行一个 jar 任务来 jar 中的所有类文件
项目(设置“
完成后刷新项目
”。)(另请参阅“< a href="http://bewarethepenguin.blogspot.com/2008/08/customizing-builds-for-your-eclipse.html" rel="noreferrer">为您的 Eclipse 项目定制构建")
请参阅 IBM文章:如何以及为何创建自定义 Ant 任务
You can define an Ant builder which runs a jar task to jar all the class files in the
project (With "
Refresh project upon completion
" set.)(See also "Customizing Builds for Your Eclipse Projects")
See IBM article: How and why to create custom Ant tasks
一种常见的模式是针对项目中的类文件进行工作(项目可以添加到其他项目构建路径并在测试时在运行时使用),因此在开发过程中实际上不需要 jar 文件。
添加自动构建步骤的一般方法是编写一个 ant 脚本,将其包含在您的项目中,然后您可以将 ant 脚本的执行包含在项目的构建中。 因此,由于 ant 有一个非常简单的 jar 构建任务,如果您确实一直需要 jar 文件,那么这并不是太多的工作。 请参阅作为初学者。
A common pattern is to work against the class files in the project (projects can be added to other projects build paths and used at runtime while testing), so you don't actually need the jar files during development.
The geeneral approach for adding an automatic build step is to write an ant script, include that in your project and you can then have the execution of your ant script included in the project's build. So as ant has a pretty straighforward jar building task this isn't too much effort if you do need the jar file all the time. See for a starter.
创建一个 J2EE 实用程序项目 (Util)。 它允许您创建与 J2EE 项目 (ProjectX) 的关联。 当您编辑 ProjectX 的属性以依赖于 Util 项目时,它会将 Util 显示为 Util.jar。 声明依赖项后,Eclipse 在必须构建 Util 项目时将构建 Util.jar。 如果您为 Util 项目启用了自动构建,则每次构建项目时 .jar 文件都将保持同步。 如果您的目标项目不是 J2EE 项目,您仍然可以使用此解决方案,但使用虚拟 J2EE 父项目。
以下是使用 ANT 任务从 Eclipse 中构建 .zip 文件的帮助页面的链接: http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.pde.doc.user/tasks /pde_feature_generate_ant.htm
另一种解决方案是使用 Zip 插件。 我们在 5 多年前就使用了它,但当 WSAD 包含对依赖项目作为 .jar 文件的支持时,我们就停止了。
Create a J2EE Utility project (Util). It lets you create an association with a J2EE project (ProjectX). When you edit the properties of ProjectX to depend upon the Util project, it shows Util as Util.jar. With the dependency declared, Eclipse will build the Util.jar when it has to build the Util project. If you have auto-build active for the Util project, the .jar file will be kept in sync each time the project is built. If your target project isn't a J2EE one, you can still use this solution but use a dummy J2EE parent project.
Here is the link to the help page for using the ANT task for building a .zip file from within Eclipse: http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.pde.doc.user/tasks/pde_feature_generating_ant.htm
An alternate solution is to use the Zip plugin. We used this over 5 years ago but stopped when WSAD included support for dependent projects as .jar files.
Thomas 的答案有效,但它生成的 jar 文件不是您可以用来实际运行应用程序的文件。
我最终得到的结果是:
不知道自从给出这个答案以来,蚂蚁是否发生了一些变化,但需要一些挖掘才能真正使其发挥作用。 教程中的很多解决方案只是部分答案...
主要更改:
更改构建文件后重新运行 ant 构建。
Netbeans 使这变得更加容易 - 只需选中几个框即可。
[编辑以修复错误终止 jar 标签的问题并拉入 .jar 文件]
Thomas's answer works, but the jar file it produces isn't one that you can use to actually run the application.
I ended up with:
Don't know if something has changed in ant since this answer was given, but it took some digging to actually get it working. A lot of the solutions in tutorials were only partial answers...
Main changes:
reran the ant build after changing the build file.
Netbeans makes this so much easier - just check a couple of boxes.
[Edited to fix issue with incorrectly terminated jar tag and pull in the .jar files]