使用maven-assembly-plugin包含所需的驱动程序时找不到合适的驱动程序 [英] No suitable driver found when including the needed drivers with maven-assembly-plugin
问题描述
可能重复:
java错误(找不到合适的驱动程序)
我有一个非常小的可以使用PostgreSQL DB,将它用作单个jar非常方便。所以我确实尝试过像这样使用maven-assembly-plugin:
I have a very small too that works with a PostgreSQL DB and it would be very convenient to use it as a single jar. So indeed I've tried using the maven-assembly-plugin like so:
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<mainClass>pack.name.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
它工作得很好,我可以看到我需要添加到jar文件中的所有文件,包括驱动程序的文件,但是当我尝试运行它时,我得到一个:
And it works perfectly fine, I can see all the files I require added to the jar file, including the driver's files but when I'm trying to run it I get a:
java.sql.SQLException: No suitable driver found for jdbc:postgresql://<ip>:5432/dbname
我有:
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
在依赖关系中,URL与我上面写的完全一样(除了删失地址:))。
我缺少什么?
In the dependencies and the URL is exactly as I wrote above (except the censored address :) ). What am I missing?
谢谢!
推荐答案
如果您不使用 Class.forName(...)
手动加载驱动程序,那么我认为您遇到了 maven的臭名昭着的问题-assembly-plugin
- 当它们来自不同的 jar
时,它会覆盖具有相同名称的文件。
If you don't use Class.forName(...)
to load the driver manually, then I think you faced an infamous problem with maven-assembly-plugin
- it overwrites files with the same name when they come from different jar
s.
在您的情况下,JDBC驱动程序发现机制依赖于名为 /META-INF/services/java.sql.Driver
的文件,并且您至少有两个 jar
在你的依赖项中包含这样一个文件(Oracle和Postgres驱动程序),因此在运行 maven-assembly-plugin $后会丢失其中一个文件c $ c>。
In your case JDBC driver discovery mechanism relies on a file named /META-INF/services/java.sql.Driver
, and you have at least two jar
s containing such a file in your dependencies (Oracle and Postgres drivers), therefore one of them is lost after running maven-assembly-plugin
.
您可以使用 maven-shade-plugin
代替 maven -assembly-plugin
正确地合并这些文件,如这里。
You can use maven-shade-plugin
instead of maven-assembly-plugin
to merge these files correcly, as described here.
或者,您可以使用 Class.forName(...)
来回避失败的自动发现机制。
Alternatively, you can use Class.forName(...)
to sidestep the failing autodiscovery mechanism.
这篇关于使用maven-assembly-plugin包含所需的驱动程序时找不到合适的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!