如何在 Eclipse Java 项目中自动生成 .jar 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1119677/
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
How Do I Automatically Generate A .jar File In An Eclipse Java Project
提问by Dr. Faust
I have an Eclipse Java project. It contains a folder named "dist". In that folder is a .jar file.
我有一个 Eclipse Java 项目。它包含一个名为“dist”的文件夹。该文件夹中有一个 .jar 文件。
How can I set things up in this project to make sure this .jar file is updated any time one of the .java files in the project has been re-compiled?
如何在此项目中进行设置以确保在重新编译项目中的 .java 文件之一时更新此 .jar 文件?
Thanks.
谢谢。
回答by djna
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.
一个常见的模式是针对项目中的类文件(项目可以添加到其他项目构建路径并在测试时在运行时使用),因此您实际上在开发过程中不需要 jar 文件。
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. Seefor a starter.
添加自动构建步骤的一般方法是编写一个 ant 脚本,将其包含在您的项目中,然后您可以将 ant 脚本的执行包含在项目的构建中。因此,由于 ant 有一个非常简单的 jar 构建任务,如果您一直都需要 jar 文件,这并不是太多的努力。请参阅入门。
回答by VonC
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.)
您可以定义一个 Ant 构建器,它运行一个 jar 任务来 jar 项目中的所有类文件(使用“ Refresh project upon completion
”设置。)
(See also "Customizing Builds for Your Eclipse Projects")
(另请参阅“为您的 Eclipse 项目定制构建”)
See IBM article: How and why to create custom Ant tasks
请参阅 IBM 文章:如何以及为何创建自定义 Ant 任务
回答by Kelly S. French
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.
创建一个 J2EE 实用程序项目 (Util)。它允许您创建与 J2EE 项目 (ProjectX) 的关联。当您编辑 ProjectX 的属性以依赖于 Util 项目时,它将 Util 显示为 Util.jar。声明依赖项后,Eclipse 将在必须构建 Util 项目时构建 Util.jar。如果您为 Util 项目启用了自动构建,则每次构建项目时 .jar 文件都会保持同步。如果您的目标项目不是 J2EE 项目,您仍然可以使用此解决方案,但使用一个虚拟的 J2EE 父项目。
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
以下是使用 ANT 任务从 Eclipse 中构建 .zip 文件的帮助页面的链接:http: //help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.pde.doc。用户/任务/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.
另一种解决方案是使用 Zip 插件。我们在 5 年前使用过它,但是当 WSAD 包含对依赖项目的支持作为 .jar 文件时停止了。
回答by Thomas Bratt
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.
创建一个 Ant 文件并告诉 Eclipse 来构建它。只有两个步骤,每个步骤都很容易,按照下面的分步说明进行。
Step 1Create a build.xml file and add to package explorer:
步骤 1创建 build.xml 文件并添加到包资源管理器:
<?xml version="1.0" ?>
<!-- Configuration of the Ant build system to generate a Jar file -->
<project name="TestMain" default="CreateJar">
<target name="CreateJar" description="Create Jar file">
<jar jarfile="Test.jar" basedir="." includes="*.class" />
</target>
</project>
Eclipse should looks something like the screenshot below. Note the Ant icon on build.xml.
Eclipse 应该类似于下面的屏幕截图。请注意 build.xml 上的 Ant 图标。
Step 2Right-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 binfolder.
步骤 2右键单击项目中的根节点。- 选择 Properties - 选择 Builders - 选择 New - 选择 Ant Build - 在 Main 选项卡中,完成bin文件夹中build.xml 文件的路径。
Check the Output
检查输出
The Eclipse output window (named Console) should show the following after a build:
构建后,Eclipse 输出窗口(名为 Console)应显示以下内容:
Buildfile: /home/<user>/src/Test/build.xml
CreateJar:
[jar] Building jar: /home/<user>/src/Test/Test.jar
BUILD SUCCESSFUL
Total time: 152 milliseconds
回答by Mykael
Thomas's answer works, but the jar file it produces isn't one that you can use to actually run the application.
Thomas 的回答有效,但它生成的 jar 文件不是您可以用来实际运行应用程序的文件。
I ended up with:
我结束了:
<?xml version="1.0" ?>
<!-- Configuration of the Ant build system to generate a Jar file -->
<project name="TDSz Data Mover" default="CreateJar">
<target name="CreateJar" description="Create Jar file">
<delete file="DataMover.jar"/>
<jar jarfile="DataMover.jar" basedir="bin/" includes="**/*.class **/Messages*.*" " update="no">
<zipfileset dir="D:/Java/mylib" erroronmissingarchive="true">
<include name="*.jar" />
</zipfileset>
<manifest>
<attribute name="Main-Class" value="some.package.and.app"/>
</manifest>
</jar>
</target>
</project>
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...
不知道自从给出这个答案以来,ant 是否发生了一些变化,但需要进行一些挖掘才能使其真正发挥作用。教程中的很多解决方案只是部分答案......
Main changes:
主要变化:
- Added a delete for the jar file as it wasn't regenerating it when I reran the ant build after changing the build file.
- Added a manifest to set the executable file properly.
- Pull in some .jar files as libs
- Pull in the Message_ files for NLS support
- 添加了对 jar 文件的删除,因为当我在更改构建文件后重新运行 ant 构建时它没有重新生成它。
- 添加了一个清单以正确设置可执行文件。
- 拉入一些 .jar 文件作为库
- 拉入 Message_ 文件以获得 NLS 支持
Netbeans makes this so much easier - just check a couple of boxes.
Netbeans 使这变得更容易 - 只需选中几个框即可。
[Edited to fix issue with incorrectly terminated jar tag and pull in the .jar files]
[已编辑以修复错误终止的 jar 标记并拉入 .jar 文件的问题]