Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven jar plugin - Wrong Class-Path entry for SNAPSHOT dependency

Tags:

java

maven

jar

I am using maven-jar-plugin to build jar and maven-assembly-plugin to put all dependencies next to the JAR in lib/directory.

If I use snapshot dependency this project, the Class-Path entry points do different JAR of that dependency, then the actual packaged one.

Here is an example:

<dependency>
    <groupId>x.y.z</groupId>
    <artifactId>artifact</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

JAR that is packaged for that dependency inti lib direcotyr is artifact-1.0-SNAPSHOT but Class-Path entry in main JARs manifest is lib/artifact-1.0-20170201.104414-8.jar

What is happening here and why?

Thanks in advance.

My assembly.xml

<dependencySets>
    <dependencySet>
        <useProjectArtifact>false</useProjectArtifact>
        <useTransitiveDependencies>true</useTransitiveDependencies>
        <outputDirectory>lib</outputDirectory>
        <unpack>false</unpack>
    </dependencySet>
</dependencySets>

Plugins:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.2</version>
        <executions>
            <execution>
                <id>assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>attached</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                    <finalName>${dist.name}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptor>${basedir}/assembly.xml</descriptor>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                </manifest>
                <manifestEntries>
                    <Class-Path>.</Class-Path>
                </manifestEntries>
            </archive>
            <outputDirectory>${dist.dir}</outputDirectory>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>test-jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
like image 689
Antoniossss Avatar asked Feb 01 '17 14:02

Antoniossss


People also ask

What is Maven Jar plugin?

Maven Jar plugin is responsible for configuring where the project's main class is so it can add it to the Jar's manifest file. If you do not include this plugin when you try to run the jar file this error will appear : Exception in thread "main" java.lang.NoClassDefFoundError.

What does Maven dependency plugin do?

The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location.


1 Answers

You have been hit by MJAR-156, which is currently unresolved as of the latest 3.0.2. The core issue is with the downstream Maven Archiver library, most probably MSHARED-169.

You can workaround that quite easily by specifying Maven Archiver not to create unique versions for snapshots. This is controlled by the parameter useUniqueVersions under the manifest configuration, which defaults to true. As such, you can change the configuration of the Jar Plugin to:

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.0.2</version>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
        <useUniqueVersions>false</useUniqueVersions>
      </manifest>
      <!-- rest of configuration -->
    </archive>
    <outputDirectory>${dist.dir}</outputDirectory>
  </configuration>
  <!-- the executions -->
</plugin>

Note that version 2.3.1 of the Jar Plugin is quite old, you should consider updating to the latest 3.0.2.

like image 105
Tunaki Avatar answered Oct 22 '22 05:10

Tunaki