I want to generate an jar file including its dependencies. Here is my pom.xml:
<?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">
<parent>
<artifactId>vehicle</artifactId>
<groupId>org</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>vehicle.cli</artifactId>
<dependencies>
<dependency>
<groupId>org</groupId>
<artifactId>vehicle.model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
org.vehicle.cli.Main
</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
The dependency org.vehicle.model I take it from a custom nexus repository.
After running mvn package
I get the target folder with the libs folder and the jar file in the same hiarachy:
- target
- libs
- vehicle.model-0.0.1-SNAPSHOT.jar
- commons-cli-1.4.jar
- vehicle.cli-0.0.1-SNAPSHOT.jar
When I execute the vehicle.cli-0.0.1-SNAPSHOT.jar (java -jar vehicle.cli-0.0.1-SNAPSHOT.jar
) I get an exception java.lang.NoClassDefFoundError for the classes in the vehicle.model-0.0.1-SNAPSHOT.jar
It's strange because I have all the dependencies jar already in the libs folder, so I unpack the vehicle.cli-0.0.1-SNAPSHOT.jar to check the MANIFEST.MF and see the following:
Manifest-Version: 1.0
Created-By: Maven Jar Plugin 3.2.0
Build-Jdk-Spec: 16
Class-Path: . libs/vehicle.model-0.0.1-20210203.101620-3.jar libs/commons-cli-1.4.jar
Specification-Title: vehicle.cli
Specification-Version: 0.0
Implementation-Title: vehicle.cli
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: org.vehicle.cli.Main
What makes me confused is in the line for ClassPath, it's calling the jar file named libs/vehicle.model-0.0.1-20210203.101620-3.jar while this jar file is not existing. I expect it should call the jar file with name: libs/vehicle.model-0.0.1-SNAPSHOT.jar, because it is the one that was copied to the libs folder by the maven-dependency-plugin. I dont understand where is the version -0.0.1-20210203.101620-3 coming from.
Can anyone explain me what is the problem here and how to fix it? Thank you!
We have run into something like this before. There is a bug reported for the jar plugin that was not fixed. Luckily, there is a workaround. Adjust the manifest in the jar
plugin like this:
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>org.vehicle.cli.Main</mainClass>
<useUniqueVersions>false</useUniqueVersions> <!-- important! -->
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
If that isn't enough, the dependency plugin has a similar <useBaseVersion>
property that may be enabled.