5

For my development station, I need my project to "see" the JDBC drivers. But when I deploy the project to the server, if the JDBC drivers are in the /lib folder they'll cause the container to misbehave.

I tried setting the <scope>provided</scope> to the drivers I don't want to package on my .war file, but I can't get my IDE to run the project.

What's a correct scope to declare the JDBC drivers on pom.xml so they don't get packaged for deploy and I can work with them on my development station? <scope>runtime</scope>?

Thanks in advance,

gtludwig
  • 5,411
  • 10
  • 64
  • 90

1 Answers1

5

The short answer for your question is: you should use provided scope.

Why not runtime? Let's check Maven docs:

provided

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.

runtime

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.

So, Maven may not expose runtime scoped dependencies in compile's classpath, thus you won't be able to use them in you code directly. However, the code like Class.forName("class.from.runtime.Scope") will compile ok.

I guess the problem is your IDE which didn't seized pom.xml changes. Usually, this problem is fixed by "cleaning cache" or "updating" / "syncing" your project. Here is how to do this in Eclipse and IDEA.

Community
  • 1
  • 1
madhead
  • 31,729
  • 16
  • 153
  • 201
  • The current revision of this answer differs from the opinion of [baeldung.com](https://www.baeldung.com/maven-dependency-scopes#3-runtime) (scope: runtime) and I would be pleased if someone reviews this answer. – Matthias Nov 26 '19 at 15:54
  • 2
    The OP said that the drivers are in his `lib` folder. I.e. they are provided by the container and it is not required to pack them in the distributable package. Thus, the scope should be `provided`. – madhead Nov 26 '19 at 17:11