9

I am trying to use the maven-jar-plugin and maven-dependency-plugin to create an runnable "bundle" of my application. It works fine in most cases, but when I have a snapshot in the dependency hierarchy, the copy-dependencies goals seems to translate the snapshot-dependencies into locked snapshots (snapshots with timestamp)

However, addClasspath from archiver-plugin does not translate snapshot dependencies:

  • in lib, there is foolib-1.0.1-20130108.143522-8.jar
  • the classpath contains lib/foolib-1.0.1-SNAPSHOT.jar

so I can't run the application.

I can't find a way to tell the copy-dependencies to not translate SNAPSHOTs or one to tell the archiver-plugin to translate SNAPSHOTs.

Here is the relevant snippet of the pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>          
    <artifactId>maven-dependency-plugin</artifactId>           
    <version>2.5.1</version>                  
    <executions>
        <execution>
            <id>copy-libs</id>
            <phase>package</phase>    
            <goals>
                <goal>copy-dependencies</goal>                          
            </goals>
            <configuration>
                <excludeScope>provided</excludeScope>
                <outputDirectory>${package.dest}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
        <outputDirectory>${package.dest}</outputDirectory>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>${main.class}</mainClass>
            </manifest>
        </archive>
        </configuration>
</plugin>
approxiblue
  • 6,982
  • 16
  • 51
  • 59
  • I had the opposite problem (like Coder Nr 23). useBaseVersion on dependency-plugin seemed to have no effect, but useUniqueVersions in jar-plugin/archiver config worked for me. – Gustave Nov 03 '20 at 18:09

2 Answers2

11

A new option (useBaseVersion) in maven-dependency-plugin 2.6 can fix this. So you need at least version 2.6.

Note: I needed the useBaseVersion option set to false, as my problem seems to be the opposite of the original question. So the original version probably requires useBaseVersion is set to true, which is the default value.

Below is an example on how to change the version number and set useBaseVersion to false in the pom:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>          
    <artifactId>maven-dependency-plugin</artifactId>
    <!-- need at least 2.6 for useBaseVersion-->           
    <version>2.6</version>                  
    <executions>
        <execution>
            <id>copy-libs</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>                          
            </goals>
            <configuration>
                <excludeScope>provided</excludeScope>
                <outputDirectory>${package.dest}/lib</outputDirectory>

                <!-- useBaseVersion=false makes the jar names match those 
                     that maven-jar-plugin puts in the manifest classpath -->
                <useBaseVersion>false</useBaseVersion>
            </configuration>
        </execution>                    
    </executions>
</plugin>
approxiblue
  • 6,982
  • 16
  • 51
  • 59
Coder Nr 23
  • 709
  • 2
  • 13
  • 23
0

The other alternative is described here: https://stackoverflow.com/a/45642898/232175

Setting useUniqueVersions to false for the maven-jar-plugin:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <configuration>
      <!-- ... -->
      <archive>
         <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>${main.class}</mainClass>
            <useUniqueVersions>false</useUniqueVersions>
         </manifest>
      </archive>
   </configuration>
</plugin>
Matthias
  • 12,053
  • 4
  • 49
  • 91