Maven not setting classpath for dependencies properly

I know this question is old, but it shows up at the top of searches for getting Maven to set dependencies properly with -SNAPSHOT versions and I had to refine the accepted solution to get my classpath resolution working correctly.

The problem I ran into was that the maven-jar-plugin included the resolvedVersion of a dependency (e.g. --.jar) while the maven-dependency-plugin (as of version 2.5.1) copies dependencies preserving their baseVersion --SNAPSHOT.jar). (See https://jira.codehaus.org/browse/MDEP-380 for more info about that enhancement.)

To get things working, I had to turn off this behaviour as follows:

    ...
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>dependency/</classpathPrefix>
                        <mainClass>com.example.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <useBaseVersion>false</useBaseVersion>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    ...

This configuration caused the dependencies to be copied into ${project.build.directory}/dependency with their resolvedVersion matching the blasspath being set into the META-INF/MANIFEST.MF by the maven-jar-plugin. Hopefully this helps someone in the future.


Raghuram gave me a push in the right direction. The way to get maven to take care of copying the jars automatically is to add this code inside the tag in the pom.xml file:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals>
          <goal>copy-dependencies</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.directory}</outputDirectory>
          <overWriteReleases>false</overWriteReleases>
          <overWriteSnapshots>true</overWriteSnapshots>
        </configuration>
      </execution>
    </executions>
  </plugin>

More details on this can be found here: https://maven.apache.org/plugins/maven-dependency-plugin/usage.html

Getting maven to package the jars together would be nice, but this is good enough to answer this question. Related answers on stackoverflow:

Building executable jar with maven?

How can I create an executable JAR with dependencies using Maven?