Mat*_*hew 18 dependencies classpath maven
操作系统名称:"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与maven相关联.如果我移动maven下载到我的$ JAVA_HOME/jre/lib/ext /文件夹中的"mysql-connector-java-5.1.14.jar"文件,那么当我运行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>
Run Code Online (Sandbox Code Playgroud)
命令"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)
Run Code Online (Sandbox Code Playgroud)
失败的路线是:
Class.forName("com.mysql.jdbc.Driver");
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我我做错了什么或如何解决它?
Mat*_*hew 19
拉古拉姆向我推进了正确的方向.让maven自动复制jar的方法是在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>
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请访问:https: //maven.apache.org/plugins/maven-dependency-plugin/usage.html
让maven将罐装在一起会很好,但这足以回答这个问题.stackoverflow的相关问题:
Mae*_*rom 14
我知道这个问题已经过时了,但它出现在搜索的顶部,让Maven使用-SNAPSHOT版本正确设置依赖关系,我不得不改进已接受的解决方案以使我的类路径解析正常工作.
我遇到的问题是maven-jar-plugin包含了依赖的resolveVersion(例如 - .jar),而maven-dependency-plugin(从2.5.1版本开始)复制了保留其baseVersion --SNAPSHOT的依赖项.罐).(有关该增强功能的更多信息,请参阅https://jira.codehaus.org/browse/MDEP-380.)
为了使工作正常,我不得不关闭此行为,如下所示:
...
<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>
...
Run Code Online (Sandbox Code Playgroud)
该配置引起的依赖性被复制到${project.build.directory}/dependency与其resolvedVersion相匹配的blasspath被设置到META-INF/MANIFEST.MF由行家-罐子的插件.希望这可以帮助将来的某个人.
| 归档时间: |
|
| 查看次数: |
63312 次 |
| 最近记录: |