如何在Eclipse Java项目中自动生成.jar文件

发布于 2024-07-27 00:42:47 字数 159 浏览 3 评论 0 原文

我有一个 Eclipse Java 项目。 它包含一个名为“dist”的文件夹。 该文件夹中有一个 .jar 文件。

如何在此项目中进行设置,以确保在重新编译项目中的 .java 文件之一时随时更新此 .jar 文件?

谢谢。

I have an Eclipse Java project. It contains a folder named "dist". In that folder is a .jar file.

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?

Thanks.

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

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

发布评论

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

评论(5

千秋岁 2024-08-03 00:42:47

创建一个 Ant 文件并告诉 Eclipse 构建它。 只有两个步骤,按照下面的分步说明,每个步骤都很简单。


第 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 应该类似于下面的屏幕截图。 请注意 build.xml 上的 Ant 图标。
Eclipse 项目中的Build.xml

步骤 2
右键单击项目中的根节点。
- 选择属性
- 选择建设者
- 选择新建
- 选择Ant构建
- 在“主要”选项卡中,填写 bin 文件夹中 build.xml 文件的路径。

Ant 构建器配置
Build step - Targets Tab

检查输出

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

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:

<?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.
Build.xml in Eclipse Project

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.

Ant builder configuration
Build step - Targets Tab

Check the Output

The Eclipse output window (named Console) should show the following after a build:

Buildfile: /home/<user>/src/Test/build.xml

CreateJar:
         [jar] Building jar: /home/<user>/src/Test/Test.jar
BUILD SUCCESSFUL
Total time: 152 milliseconds
找个人就嫁了吧 2024-08-03 00:42:47

您可以定义一个 Ant 构建器,它运行一个 jar 任务来 jar 中的所有类文件
项目(设置“完成后刷新项目”。)

alt text

(另请参阅“< 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.)

alt text

(See also "Customizing Builds for Your Eclipse Projects")

See IBM article: How and why to create custom Ant tasks

alt text

彩扇题诗 2024-08-03 00:42:47

一种常见的模式是针对项目中的类文件进行工作(项目可以添加到其他项目构建路径并在测试时在运行时使用),因此在开发过程中实际上不需要 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.

悲歌长辞 2024-08-03 00:42:47

创建一个 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.

罪#恶を代价 2024-08-03 00:42:47

Thomas 的答案有效,但它生成的 jar 文件不是您可以用来实际运行应用程序的文件。

我最终得到的结果是:

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

不知道自从给出这个答案以来,蚂蚁是否发生了一些变化,但需要一些挖掘才能真正使其发挥作用。 教程中的很多解决方案只是部分答案...

主要更改:

  • 添加了对 jar 文件的删除,因为当我
    更改构建文件后重新运行 ant 构建。
  • 添加了清单以正确设置可执行文件。
  • 拉入一些 .jar 文件作为 libs
  • 拉入 Message_ 文件以获得 NLS 支持

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:

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

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

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]

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