我正在尝试使用 neo4j-jdbc-driver v3.0 连接到 Neo4j 2.2.10 服务器。我能够在 IDE 中毫无问题地进行连接,但是当我尝试从 Jar 文件执行相同操作时,我会收到“找不到合适的驱动程序”异常,就好像依赖项不可用一样。
java.sql.SQLException: No suitable driver found for jdbc:neo4j:http://localhost
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
....
这是我的 pom.xml 的相关部分:
<dependencies>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
我在论坛的其他地方研究过包含与 Maven 的依赖关系,所以这就是该插件的来源。但是,我认为它不包括基于错误的依赖关系。
以前,我使用的是:
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>1.0.4</version>
</dependency>
我能够通过 Jar 中的 Bolt 与 v3.0.3 Neo4j 服务器进行交互,没有出现任何问题。
我做错了什么?
感谢您的建议!
最佳答案
您可以自行验证驱动程序是否包含在生成的 Artifact 中(假设其名称为 assembly.jar):
jar tf target/assembly.jar | fgrep jdbc
不过,可能是这样。
我认为问题源于这样一个事实:驱动程序是使用服务提供程序接口(interface) (SPI) 自动注册的,方法是在 META-INF/services/java.sql.Driver
中列出类。但是,由于程序集插件将依赖项 jar 打包在程序集 jar 内,而不是分解它们并创建 uber-jar(就像阴影插件一样),因此不会扫描 SPI 文件。请参阅this blog post例如。
我建议您尝试一下使用 ServicesResourceTransformer
配置的阴影插件。
更新
如果 SPI 机制在您的上下文中不起作用(由容器应用程序作为插件加载的 jar),如果您知道在使用驱动程序之前将调用代码中的某个位置,您可以自己注册驱动程序(插件的一些初始化服务,例如,可能通过实现 Plugin.onLoad()
?):只需创建驱动程序的实例,它就会自行注册。
try {
new org.neo4j.jdbc.http.HttpDriver();
} catch (SQLException e) {
logger.error("Could not load HttpDriver for Neo4j", e);
}
关于java - Maven 不包括 Jar 文件中的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39183920/