简体   繁体   中英

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

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 JAR s 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>

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.

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