简体   繁体   English

如何创建包含所有 Maven 依赖项的不可执行 JAR 文件

[英]How to create a non-executable JAR file that includes all Maven dependencies

I have a Java project that doesn't have a main file but it has a lot of Maven dependencies.我有一个 Java 项目,它没有主文件,但它有很多 Maven 依赖项。

How I can create a JAR-file that contains my source-code and required maven dependencies?如何创建包含我的源代码和所需的 maven 依赖项的 JAR 文件?

My pom.xml:我的 pom.xml:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>NetworkCommunicationAPI</groupId>
    <artifactId>NetworkCommunicationAPI</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
      <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20210307</version>
        </dependency>
    </dependencies>
</project>

You can use the Maven shade plugin or Maven assembly plugin to create such a JAR.您可以使用 Maven 阴影插件或 Maven 组装插件来创建这样的 JAR。

In most cases, such JARs should be avoided.在大多数情况下,应避免使用此类 JARs。 You create a dependency hell for everyone who uses your JAR because Maven cannot manage the included dependencies any more and therefore conflicts with other dependencies (not from your JAR) may become unmanagable.您为使用 JAR 的每个人创建了一个依赖地狱,因为 Maven 无法再管理包含的依赖项,因此与其他依赖项(不是来自您的 JAR)的冲突可能变得难以管理。

On the other, such fat JARs are of little use because Maven or Gradle will find your transitive dependencies anyway, so there is no need to include them into the JAR. On the other, such fat JARs are of little use because Maven or Gradle will find your transitive dependencies anyway, so there is no need to include them into the JAR.

You can use the maven-assembly-plugin just as you could use it for creating an executable JAR with dependencies .您可以使用 maven-assembly-plugin 就像您可以使用它来创建带有 dependencies 的可执行 JAR 一样

The only thing you need to change is that you don't need to specify a main class:您唯一需要更改的是您不需要指定主要的 class:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

After that, you can create the JAR using mvn compile assembly:single .之后,您可以使用mvn compile assembly:single创建 JAR。

This compiles your sources and creates a JAR containing the compiled sources and all dependencies in the compile-scope.这将编译您的源代码并创建一个 JAR,其中包含已编译的源代码和编译范围内的所有依赖项。

暂无
暂无

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

相关问题 如何使用 Maven 创建具有依赖项的不可执行 JAR? - How can I create a non-executable JAR with dependencies using Maven? 如何创建Maven库(不可执行的JAR) - How to create a Maven library (non-executable JAR) Maven:生成具有所有依赖项的可执行jar文件 - Maven: Generating an executable jar file with all the dependencies 如何生成具有所有 maven 依赖项的可执行 jar 文件? - How to produce an executable jar file with all maven dependencies? 如何使用 maven 和系统依赖项创建可执行的 jar? - How to create executable jar with maven and system dependencies? 创建可执行 Jar,其中包含所有依赖项作为打包的 jar - Create Executable Jar that includes all the dependencies as packaged jars 如何创建具有所有依赖项和属性文件的单个可执行 jar? - How to create single executable jar with all dependencies and with a properties file? Gradle:使用依赖库创建不可执行的jar和可执行Shell脚本,并将其作为程序包分发 - Gradle: Create a non-executable jar and executable shell script with dependent libraries as a package distribution 创建一个可执行 JAR 及其所有依赖项,并使用 maven 程序集放入 ZIP 文件 - Create an executable JAR with all its dependencies and put in the ZIP file using maven assembly 如何使用Maven创建一个jar,其中每个依赖项在单独的文件夹中包括依赖项 - How to create a jar with Maven that includes dependencies in a separate folder for each dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM