java - Maven - JDBC jar 文件的正确范围是什么?

标签 java maven jdbc scope

对于我的开发站,我需要我的项目来“查看”JDBC 驱动程序。但是当我将项目部署到服务器时,如果 JDBC 驱动程序位于/lib 文件夹中,它们将导致容器出现异常。

我尝试设置 <scope>provided</scope>我不想打包到我的 .war 文件中的驱动程序,但我无法让我的 IDE 运行该项目。

pom.xml 上声明 JDBC 驱动程序的正确范围是什么?这样他们就不会打包部署,我可以在我的开发站上与他们一起工作吗? <scope>runtime</scope>

提前致谢

最佳答案

您的问题的简短回答是:您应该使用提供的范围。

为什么不是运行时?让我们检查一下 Maven 文档:

提供

This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

运行时

This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

因此,Maven 可能不会在编译的类路径中公开runtime 范围内的依赖项,因此您将无法在代码中直接使用它们。但是,像 Class.forName("class.from.runtime.Scope") 这样的代码将编译正常。

我想问题出在您的 IDE 没有捕获 pom.xml 更改。通常,这个问题可以通过“清理缓存”或“更新”/“同步”您的项目来解决。以下是如何在 Eclipse 中执行此操作和 IDEA .

关于java - Maven - JDBC jar 文件的正确范围是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36851549/

相关文章:

javascript - 如何防止每次调用主机时都创建该类的新实例?

java - 如何检查JasperReports中BigDecimal字段的值是否为负数

java - Eclipse 调试 : Variable names are not showing, 但显示值

java - 如何在jsp中使用bean类向数据库中插入值?

java - 计算最后一个星期天前1周和今天到最后一个星期日

java - 如何使 RealmList 可打包

java - maven 上有 bctls (BouncycaSTLe TLS) 吗?

java - Intellij Maven 无法解决我的依赖关系

java - 无法连接到 MS Access 数据库?

java - 以所需格式列出 Maven 项目的依赖项