Maven not setting classpath for dependencies properly

女生的网名这么多〃 提交于 2019-12-02 20:38:46
Matthew

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?

Maelstrom

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.

Maven does set the classpath to the dependencies correctly, but not prefixed with repository location. It will look like this in your Manifest file.

Class-Path: mysql-connector-java-5.1.14.jar

It is upto you to place the dependant jars in the same folder as the jar which you are running.

Refer to the examples

Or if you are using maven-shade-plugin a workaround is to add a filter for including the missing files:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    ...
                    <configuration>
                        ...
                        <filters>
                            ...                                
                            <filter>
                                <artifact>com.sun.istack</artifact>
                                <includes>
                                    <include>**</include>
                                </includes>
                            </filter>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!