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

时间:2023-01-13 11:13:23

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.

谢谢。

5 个解决方案

#1


34  

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 1 Create 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 Java项目中自动生成.jar文件

Eclipse应该看起来像下面的截图。注意build.xml上的Ant图标。

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.

步骤2右键单击项目中的根节点。 - 选择属性 - 选择构建器 - 选择新建 - 选择Ant构建 - 在主选项卡中,完成bin文件夹中build.xml文件的路径。

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

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

#2


17  

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

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

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

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

(另请参阅“自定义Eclipse项目的构建”)

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

请参阅IBM文章:如何以及为何创建自定义Ant任务

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

#3


4  

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. See for a starter.

添加自动构建步骤的geeneral方法是编写一个ant脚本,在项目中包含该脚本,然后您可以在项目的构建中包含ant脚本的执行。因此,如果你确实需要jar文件,那么ant有一个相当直接的jar构建任务,这不是太费劲。请参阅首发。

#4


2  

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 as 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文件时就停止了。

#5


0  

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

自从给出这个答案以来,不知道蚂蚁是否有变化,但实际上需要进行一些挖掘才能使其正常工作。教程中的很多解决方案只是部分答案......

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.
  • 添加了jar文件的删除,因为它在更改构建文件后重新编译ant构建时没有重新生成它。
  • Added a manifest to set the executable file properly.
  • 添加了清单以正确设置可执行文件。
  • Pull in some .jar files as libs
  • 将一些.jar文件作为libs引入
  • Pull in the Message_ files for NLS support
  • 拉入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文件]

#1


34  

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 1 Create 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 Java项目中自动生成.jar文件

Eclipse应该看起来像下面的截图。注意build.xml上的Ant图标。

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.

步骤2右键单击项目中的根节点。 - 选择属性 - 选择构建器 - 选择新建 - 选择Ant构建 - 在主选项卡中,完成bin文件夹中build.xml文件的路径。

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

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

#2


17  

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

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

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

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

(另请参阅“自定义Eclipse项目的构建”)

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

请参阅IBM文章:如何以及为何创建自定义Ant任务

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

#3


4  

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. See for a starter.

添加自动构建步骤的geeneral方法是编写一个ant脚本,在项目中包含该脚本,然后您可以在项目的构建中包含ant脚本的执行。因此,如果你确实需要jar文件,那么ant有一个相当直接的jar构建任务,这不是太费劲。请参阅首发。

#4


2  

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 as 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文件时就停止了。

#5


0  

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

自从给出这个答案以来,不知道蚂蚁是否有变化,但实际上需要进行一些挖掘才能使其正常工作。教程中的很多解决方案只是部分答案......

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.
  • 添加了jar文件的删除,因为它在更改构建文件后重新编译ant构建时没有重新生成它。
  • Added a manifest to set the executable file properly.
  • 添加了清单以正确设置可执行文件。
  • Pull in some .jar files as libs
  • 将一些.jar文件作为libs引入
  • Pull in the Message_ files for NLS support
  • 拉入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文件]