从java项目创建.jar文件的最佳实践是什么?

时间:2023-02-05 14:06:38

what is the best practice to create a .jar file from a java project??

从java项目创建.jar文件的最佳实践是什么?

6 个解决方案

#1


Eclipse IDE is the best way for new comers. Just select the project right click export File, select jar anf filename.

Eclipse IDE是新来者的最佳方式。只需选择项目右键单击导出文件,选择jar anf filename。

#2


Some points to consider:

有些要考虑的要点:

  • Create proper MANIFEST.MF, which should contain:
    • Build date;
    • Author;
    • Implementation-version;
  • 创建适当的MANIFEST.MF,其中应包含:构建日期;作者;实现版本;

  • Use a script/program - like an Ant task - which takes care of properly creating a manifest file.
  • 使用脚本/程序 - 如Ant任务 - 负责正确创建清单文件。


Here's a simple ant file which builds a java project, adapted from the Ant tutorial:

这是一个简单的ant文件,它构建了一个java项目,改编自Ant教程:

<project>

<target name="clean">
    <delete dir="build"/>
</target>

<target name="compile">
    <mkdir dir="build/classes"/>
    <javac srcdir="src" destdir="build/classes"/>
</target>

<target name="jar">
    <mkdir dir="build/jar"/>
    <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
        <manifest>
            <attribute name="Implementation-Version" value="1.0"/>
        </manifest>
    </jar>
</target>

<target name="run">
    <java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>

</project>

#3


Read up on the basics of Sun's JAR format at this tutorial. In my opinion you should learn the basics -- namely MANIFESTs, the SDK tools -- before jumping into third party tools such as the following.

在本教程中阅读Sun的JAR格式的基础知识。在我看来,你应该学习基础知识 - 即MANIFESTs,SDK工具 - 然后再进入第三方工具,如下所示。

The Jar command line tool distributed with the Sun JDK:

与Sun JDK一起分发的Jar命令行工具:

jar -cf myjar.jar base_src_folder

Where base_src_folder is the parent directory holding your source code.

其中base_src_folder是包含源代码的父目录。

Alternatively you could use a build tool such as Apache Ant. It has features such as the JAR task to do this for you.

或者,您可以使用Apache Ant之类的构建工具。它具有诸如JAR任务之类的功能,可以为您完成此任务。

#4


Apache Maven, for projects which are dependent on other projects.

Apache Maven,用于依赖于其他项目的项目。

Admittedly, Maven may be overkill for learning projects, but it is invaluable for larger, distributed ones.

不可否认,Maven对于学习项目可能过度,但对于较大的分布式项目来说,它是非常宝贵的。

#5


Apache Ant

#6


Ant is not quite easy to begin with. Instead, I would suggest using an IDE, for instance NetBeans, which creates an Ant script under the hood. All you have to do is to hit "build", and you get a .jar file as result.

Ant开始并不容易。相反,我建议使用IDE,例如NetBeans,它会在引擎盖下创建一个Ant脚本。你所要做的就是点击“build”,然后得到一个.jar文件。

#1


Eclipse IDE is the best way for new comers. Just select the project right click export File, select jar anf filename.

Eclipse IDE是新来者的最佳方式。只需选择项目右键单击导出文件,选择jar anf filename。

#2


Some points to consider:

有些要考虑的要点:

  • Create proper MANIFEST.MF, which should contain:
    • Build date;
    • Author;
    • Implementation-version;
  • 创建适当的MANIFEST.MF,其中应包含:构建日期;作者;实现版本;

  • Use a script/program - like an Ant task - which takes care of properly creating a manifest file.
  • 使用脚本/程序 - 如Ant任务 - 负责正确创建清单文件。


Here's a simple ant file which builds a java project, adapted from the Ant tutorial:

这是一个简单的ant文件,它构建了一个java项目,改编自Ant教程:

<project>

<target name="clean">
    <delete dir="build"/>
</target>

<target name="compile">
    <mkdir dir="build/classes"/>
    <javac srcdir="src" destdir="build/classes"/>
</target>

<target name="jar">
    <mkdir dir="build/jar"/>
    <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
        <manifest>
            <attribute name="Implementation-Version" value="1.0"/>
        </manifest>
    </jar>
</target>

<target name="run">
    <java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>

</project>

#3


Read up on the basics of Sun's JAR format at this tutorial. In my opinion you should learn the basics -- namely MANIFESTs, the SDK tools -- before jumping into third party tools such as the following.

在本教程中阅读Sun的JAR格式的基础知识。在我看来,你应该学习基础知识 - 即MANIFESTs,SDK工具 - 然后再进入第三方工具,如下所示。

The Jar command line tool distributed with the Sun JDK:

与Sun JDK一起分发的Jar命令行工具:

jar -cf myjar.jar base_src_folder

Where base_src_folder is the parent directory holding your source code.

其中base_src_folder是包含源代码的父目录。

Alternatively you could use a build tool such as Apache Ant. It has features such as the JAR task to do this for you.

或者,您可以使用Apache Ant之类的构建工具。它具有诸如JAR任务之类的功能,可以为您完成此任务。

#4


Apache Maven, for projects which are dependent on other projects.

Apache Maven,用于依赖于其他项目的项目。

Admittedly, Maven may be overkill for learning projects, but it is invaluable for larger, distributed ones.

不可否认,Maven对于学习项目可能过度,但对于较大的分布式项目来说,它是非常宝贵的。

#5


Apache Ant

#6


Ant is not quite easy to begin with. Instead, I would suggest using an IDE, for instance NetBeans, which creates an Ant script under the hood. All you have to do is to hit "build", and you get a .jar file as result.

Ant开始并不容易。相反,我建议使用IDE,例如NetBeans,它会在引擎盖下创建一个Ant脚本。你所要做的就是点击“build”,然后得到一个.jar文件。