dependencies - Maven 未正确设置依赖项的类路径

标签 dependencies maven classpath

操作系统名称:“linux” 版本:“2.6.32-27-generic” arch:“i386” 系列:“unix”

Apache Maven 2.2.1 (r801777; 2009-08-06 12:16:01-0700)

Java 版本:1.6.0_20

我正在尝试在 ubuntu 中使用 maven 中的 mysql 依赖项。如果我将 maven 下载的“mysql-connector-java-5.1.14.jar”文件移动到我的 $JAVA_HOME/jre/lib/ext/文件夹中,当我运行 jar 时一切正常。

我想我应该能够在 pom.xml 文件中指定依赖项,而 maven 应该负责自动设置依赖项 jar 的类路径。这不正确吗?

我的 pom.xml 文件如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.ion.common</groupId>
  <artifactId>TestPreparation</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>TestPrep</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>

        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.ion.common.App</mainClass>
            </manifest>
          </archive>
        </configuration>

      </plugin>
    </plugins>
  </build>

  <dependencies>

    <!-- JUnit testing dependency -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- MySQL database driver -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.14</version>
      <scope>compile</scope>
    </dependency>

  </dependencies>
</project>

命令“mvn package”构建它没有任何问题,我可以运行它,但是当应用程序尝试访问数据库时,会出现此错误:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:186)
        at com.ion.common.Functions.databases(Functions.java:107)
        at com.ion.common.App.main(App.java:31)

它失败的线路是:
Class.forName("com.mysql.jdbc.Driver");

谁能告诉我我做错了什么或如何解决它?

最佳答案

拉古拉姆让我朝着正确的方向前进。让 maven 负责自动复制 jars 的方法是在 pom.xml 文件的标记中添加以下代码:

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

可以在此处找到有关此的更多详细信息:
https://maven.apache.org/plugins/maven-dependency-plugin/usage.html

让 Maven 将 jar 打包在一起会很好,但这足以回答这个问题。 stackoverflow 上的相关回答:

Building executable jar with maven?

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

关于dependencies - Maven 未正确设置依赖项的类路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4687609/

相关文章:

java - 无法部署 WAR : ClassNotFoundException - What am I still missing?

c++ - 如何使我的项目易于设置?

java - maven java编译: is there concurrent mode?

java - 如何配置不同打包子项目的Maven多模块依赖

java - 在Nexus储存库中找不到 Artifact (快照)

java - Gradle:验证类列表在构建时的类路径中

json - 使用 Spring 3.1.2 从 Jackson 1.9 升级到 2.5 时出现问题 - 未找到 ProviderBase 类

java - 具有重复依赖项的可运行 JAR

shell 中的 java 类路径

maven-2 - Maven 类路径顺序问题