简体   繁体   中英

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.

How I can create a JAR-file that contains my source-code and required maven dependencies?

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

In most cases, such JARs should be avoided. 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.

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 .

The only thing you need to change is that you don't need to specify a main 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 .

This compiles your sources and creates a JAR containing the compiled sources and all dependencies in the compile-scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM