0

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>
OffRange
  • 15
  • 1
  • 9
  • You can try the maven shade or assembly plugin. – dan1st Apr 25 '21 at 11:42
  • @dan1st how I can use these plugins because for me these don't work – OffRange Apr 25 '21 at 12:02
  • Can you explain why you want to create such a JAR? This is usually not done. – J Fabian Meier Apr 25 '21 at 13:54
  • @JFabianMeier I wrote an API that used org.json. I added this dependency via maven. Now I want use my API in other projects so if I basicly export the API the JAR contains only my sourcecode and not the maven dependencies. To use them I would need to add the maven dependencies in each project I used my API. But I want have the maven dependencies in the JAR with my API sourcecode together so I don't need to add the maven dependencies again. – OffRange Apr 25 '21 at 14:02
  • You never need to add the Maven dependencies again. Maven finds them automatically. This is the transitive dependency resolution feature of Maven. There is no need to package dependencies inside your project. – J Fabian Meier Apr 25 '21 at 14:06
  • @JFabianMeier so if I basicly export my API and add the JAR to my projects as a library the maven dependencies would automatically added? But this don't worked for me – OffRange Apr 25 '21 at 14:09
  • @JFabianMeier Do you mean when I export my API in my case in eclipse and add it to the `build path` in my other project that the other project will automatically load the maven dependencies I used in my API? Do I need convert the other project to an maven project? – OffRange Apr 25 '21 at 14:28
  • Both projects need to be Maven projects. You need to build the projects with `mvn clean install`. – J Fabian Meier Apr 25 '21 at 14:49

2 Answers2

1

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.

dan1st
  • 12,568
  • 8
  • 34
  • 67
0

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.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • I totally agree to that opinion, but actually I found a case where a non-executable fat jar makes sense. I need to package a library jar with all dependencies to make sure it can be scanned for vulnerabilities by tools like Blackduck (binary dependency scanner). Without building a fat jar of the lib, Blackduck scans nothing. – LastZolex Jul 20 '23 at 07:43