简体   繁体   English

如何将 lib 中的 jars 包含到 Netbeans 8.2 中的项目 jar 文件中?

[英]How to include jars in lib into project jar file in Netbeans 8.2?

When I build the jar file, it creates a separate folder, lib, but since I can make it combine everything in one file, I saw how to do it here , but in this new version(8.2) it doesn't work because the site menus have changed and when I add in the build.xml I can't right click > run target since it "blocks" that element当我构建 jar 文件时,它会创建一个单独的文件夹 lib,但由于我可以将所有内容合并到一个文件中,所以我在这里看到了如何操作,但在这个新版本(8.2)中它不起作用,因为站点菜单已更改,当我添加 build.xml 时,我无法右键单击 > 运行目标,因为它“阻止”了该元素

The goal is to have only one file, for greater convenience and not having to move the folder lib, generated automatically.目标是只有一个文件,为了更方便,并且不必移动文件夹 lib,自动生成。 The project type is Java Application and I haven't modified anything in build.xml项目类型是 Java 应用程序,我没有在 build.xml 中修改任何内容

Option One选项一

Assuming you have: (a) a standard Ant-based project (which it sounds like you have), and (b) you have your extra JAR files in the usual NetBeans "Libraries" folder, then you can do the following:假设您有:(a)一个标准的基于 Ant 的项目(听起来像您有),并且(b)您在通常的 NetBeans“库”文件夹中有额外的 JAR 文件,那么您可以执行以下操作:

1) Edit the project's build.xml file. 1) 编辑项目的build.xml文件。

2) Go to the end of the file, and add the following immediately before the final </project> tag: 2) Go 到文件末尾,并在最后的</project>标记之前添加以下内容:

    <target name="-post-jar">
        <jar destfile="${dist.jar}"
             update="true">
            <restrict> 
                <archives>
                   <zips>
                       <fileset dir="${dist.dir}" includes="lib/*.jar"/>
                   </zips>
                </archives>
            </restrict>
        </jar>
    </target>

This adds an extra step to the standard "clean and build" process in NetBeans.这为 NetBeans 中的标准“清理和构建”过程增加了一个额外的步骤。

It copies the contents of library files into your application's JAR file.它将库文件的内容复制到应用程序的 JAR 文件中。 Note that I explicitly say "the contents" here.请注意,我在这里明确地说“内容”。 It does not copy the JAR files themselves - because you would have to handle the classpath issues arising from that approach (see option 2).它不会复制 JAR 文件本身 - 因为您必须处理由该方法引起的类路径问题(请参阅选项 2)。 It just copies all of the class files from your library JARs into your application's JAR.它只是将库 JARs 中的所有 class 文件复制到应用程序的 JAR 中。

The above is based on the examples found here in the Ant documentation.以上基于Ant文档中的示例。 See the "Merging archives" examples.请参阅“合并档案”示例。

Then, you can run the JAR file with something like this:然后,您可以运行 JAR 文件,如下所示:

"C:\Program Files\Java\jdk1.8.0_211\bin\java" -jar C:\tmp\DemoOne.jar

Option Two选项二

Use Maven, not Ant, for your build process (and JAR dependency management).在构建过程中使用 Maven,而不是 Ant(和 JAR 依赖项管理)。 The specifics are outside the scope of this question, but look for the Maven Shade plug-in.具体细节在本题的scope之外,但要找Maven Shade插件。

That plug-in was designed specifically to handle the situation in your question - and to deal with more complex situations which may not be handled by my option 1 above.该插件专门设计用于处理您的问题中的情况 - 并处理我上面的选项 1 可能无法处理的更复杂的情况。 Using Maven to manage your dependent JARs can also make your life much, much easier.使用 Maven 管理您的依赖 JARs 也可以让您的生活变得更加轻松。

Other Notes其他注意事项

It may feel inconvenient (ie less portable) to have a separate "lib" directory containing required library JARs, but it does make sense from the point of view of keeping your code separated (in its own JAR) from other libraries and dependencies.拥有一个包含所需库 JARs 的单独“lib”目录可能会感觉不方便(即不太可移植),但从将代码与其他库和依赖项分开(在其自己的 JAR 中)的角度来看,它确实有意义。

NetBeans 8.2 was released in October 2016 - it is (by the standards of IDEs) quite old. NetBeans 8.2 于 2016 年 10 月发布 - 它(按照 IDE 的标准)已经相当老了。 You may want to consider upgrading to a later version, if that is an option.如果可以的话,您可能需要考虑升级到更高版本。 See here for a list of recent releases.有关最新版本的列表,请参见此处

With a more recent version of NetBeans you will more easily be able to take advantage of newer versions of Java.使用更新版本的 NetBeans,您将能够更轻松地利用更新版本的 Java。

Edit - Maven Shade编辑 - Maven 阴影

Here is a Maven-based example (using Java 11), in case it encourages you to take another look at Maven.这是一个基于 Maven 的示例(使用 Java 11),以防它鼓励您再看看 Maven。

This is the Java program:这是 Java 程序:

package org.ajames.uberjar;

import org.apache.commons.lang3.CharUtils;

public class App {

    public static void main(String[] args) {
        System.out.println("The result is " + CharUtils.isAscii('x') + "!");
    }

}

As you can see, it is very simple.如您所见,它非常简单。 It requires one external JAR file: commons-lang3 .它需要一个外部 JAR 文件: commons-lang3

A Maven POM file (pom.xml) is created for you by NetBeans when you choose to create a new Maven-based Java project.当您选择创建一个新的基于 Maven 的 Java 项目时,NetBeans 会为您创建一个 Maven POM 文件 (pom.xml)。

You have to edit that POM file.您必须编辑该 POM 文件。 Here is mine, which creates a single executable JAR containing everything I need:这是我的,它创建了一个可执行的 JAR 包含我需要的一切:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.ajames</groupId>
    <artifactId>UberJarExample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.10</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>UberJarExample</finalName>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Built-By>ajames</Built-By>
                                        <Main-Class>org.ajames.uberjar.App</Main-Class>
                                        <Build-Number>123</Build-Number>
                                        <!-- https://planet.jboss.org/post/building_multi_release_jars_with_maven -->
                                        <Multi-Release>false</Multi-Release>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>            

        </plugins>
    </build>

</project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将jar包含到APK的lib或user lib android项目中? - How to include jars into the lib or user lib android project on APK? 如何在另一个项目中包含具有依赖jar的jar文件本身作为依赖关系? - How to include a jar file that has dependent jars with it as a dependency itself in an another project? 如何包括依赖项目并将jar分为两个jar? - How to include dependent project and splitting jar into two jars? 如何在我自己的Project JAR中包含外部JAR - How do I include external JARs in my own Project JAR 如何在内置的jar文件中包含一个文本文件? [NetBeans的] - How to include a text file in the built jar? [NetBeans] 如何在netbeans中将sqlite db文件包含到jar中? - How to include sqlite db file into jar in netbeans? 如何使用添加到netbeans项目的.jar库中的类? - How to use classes from the .jar lib added to netbeans project? 如何在 jar 文件的清单文件中包含不同驱动器中的 jars - How to include jars in different drives in to the manifest file in a jar file NetBeans-如何还原JSTL 1.1库 - NetBeans - how to restore JSTL 1.1 lib jars 如何在使用build.xml文件的Ant生成的jar文件中包含Eclipse项目的“Java Build Path”部分中引用的jar文件 - How to include the jars referenced in “Java Build Path” section of an Eclipse project in a jar file generated with Ant using a build.xml file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM