有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Maven JDBC jar文件的正确作用域是什么?

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

我试着将<scope>provided</scope>设置为我不想在计算机上打包的驱动程序。war文件,但我无法让我的IDE运行该项目

pom.xml上声明JDBC驱动程序的正确作用域是什么,这样它们就不会打包部署,我就可以在我的开发站上使用它们了<scope>runtime</scope>

提前感谢,


共 (1) 个答案

  1. # 1 楼答案

    您的问题的简短答案是:您应该使用provided范围

    为什么不runtime?让我们检查一下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可能不会在compile的类路径中公开runtime范围的依赖项,因此您将无法在代码中直接使用它们。但是,像Class.forName("class.from.runtime.Scope")这样的代码可以编译

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