Maven没有正确设置依赖关系的类路径 [英] Maven not setting classpath for dependencies properly
问题描述
操作系统名称:linux版本:2.6.32-27-genericarch:i386系列:unix
OS name: "linux" version: "2.6.32-27-generic" arch: "i386" Family: "unix"
Apache Maven 2.2.1 (r801777; 2009-08-06 12:16:01-0700)
Apache Maven 2.2.1 (r801777; 2009-08-06 12:16:01-0700)
Java版本:1.6.0_20
Java version: 1.6.0_20
我试图在ubuntu中使用maven中的mysql依赖关系。如果我将maven下载到我的$ JAVA_HOME / jre / lib / ext /文件夹中的mysql-connector-java-5.1.14.jar文件,我运行jar时一切都很好。
I am trying to use the mysql dependency in with maven in ubuntu. If I move the "mysql-connector-java-5.1.14.jar" file that maven downloaded into my $JAVA_HOME/jre/lib/ext/ folder, everything is fine when I run the jar.
我想我应该能够在pom.xml文件中指定依赖关系,并且maven应该自动设置依赖关系jar的类路径。这是不正确的吗?
I think I should be able to just specify the dependency in the pom.xml file and maven should take care of setting the classpath for the dependency jars automatically. Is this incorrect?
我的pom.xml文件如下所示:
My pom.xml file looks like this:
<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构建它没有任何问题,我可以运行它,但是当应用程序尝试访问数据库时,会显示此错误:
The command "mvn package" builds it without any problems, and I can run it, but when the application attempts to access the database, this error is presented:
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");
任何人都可以告诉我我在做错什么或如何解决?
Can anyone tell me what I'm doing wrong or how to fix it?
推荐答案
Raghuram给了我一个正确的方向。让maven自动处理复制jar的方法是在pom.xml文件的标签内添加这个代码:
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>
可以在这里找到更多细节:
http://maven.apache.org/plugins/maven-dependency-plugin/howto.html
More details on this can be found here: http://maven.apache.org/plugins/maven-dependency-plugin/howto.html
将maven打包在一起将会很好,但这是足够好的回答这个问题。关于stackoverflow的相关答案:
Getting maven to package the jars together would be nice, but this is good enough to answer this question. Related answers on stackoverflow:
这篇关于Maven没有正确设置依赖关系的类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!