Maven magic Hall: the pits on which Oracle jdbc driver depends

Time:2020-12-2

preface

Since Oracle does not provide any jar package of Oracle jdbc driver to the public Maven repository, we can’t directly load dependencies through Maven as easily as mysql, SQLite, etc.
However, manually downloading the Oracle jdbc driver jar package, installing it to the local warehouse (. M2 directory), and then loading the dependency through Maven is a common method. But in addition, we can pass<scope>system</scope>The way to introduce, but one of the pits below will be fine.

Manual installation to local warehouse

  1. Put ojdbc7-12.1.0.2.jar into the Lib directory of the root directory of the project, and carry out version management along with the project;
  2. stay POM.xml Add dependency definition to the file;
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.2</version>
</dependency>
  1. Install the jar package to the local repository.
#Execute in the project root directory
mvn install:install-file -Dfile=./lib/ojdbc7-12.1.0.2.jar -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.2 -Dpackaging=jar

Scope is the pit of system in springboot

In addition to the above methods, we can POM.xml Define dependencies as

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/ojdbc7-12.1.0.2.jar</systemPath>
</dependency>

Then, even if there is no Oracle jdbc driver dependency in the local warehouse (. M2 directory), execute themvn compileandmvn spring-boot:runIt was still successfully implemented.
butPlease note that, executemvn packageThe packaged springboot uberjar package does not contain the Oracle jdbc driver dependency. If it is directly deployed to the server, the following error will be reported:

application failed to start

Description:
…………………………….
Reason: Failed to load driver class oracle.jdbc.driver.OracleDriver in either of HikariConfig class loader or Thread context classloader.

Action:
Update your application’s configuration

resolvent:

  1. For the spring boot application with external web container, the Oracle jdbc driver jar package is placed in the Lib directory of the container;
  2. For the springboot application with built-in web container, you can modify the configuration of spring boot Maven plugin plug-in. Please refer to the following for details.

On the scope attribute in Maven dependency definition

Role: used to limit the scope of dependency in each life cycle of Maven project.

  • compileBy default, the dependency will participate in the compilation phase, and will be packaged into the Lib directory in the final release package (such as jar, war, uberjar). It has transitivity, that is, the dependency is effective for other projects that depend on the current project;
  • providedThe dependency will participate in the compilation phase but will not be packaged into the final release package, and the runtime will be provided by the container or JDK. There is no transitivity. (such as servlet API, JSP API, Lombok, etc.);
  • runtimeThe dependency does not participate in the compilation phase (that is, it does not join the classpath), but is packaged into the final release package to participate in the run and test phases. Dependencies (such as JDBC drivers) are usually used to load dynamically according to the configuration file or reflected by the interface;
  • testThe dependency participates in the compilation phase, but is not packaged to the final release package, and the dependency only participates in the test phase;
  • systemIndicates that the path of the dependency is a file system based jar package path and must pass through thesystemPathSpecifies the local file path. Dependencies participate in the compilation phase and are not packaged into the final release package by default.

    • For a spring boot project, if you want to package a jar package with a scope of system into a distribution package, you need to configure the spring boot Maven plugin

      <build>
          <plugins>
              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
                  <configuration>
                      <includeSystemScope>true</includeSystemScope>
                  </configuration>
              </plugin>
          </plugins>
      </build>
  • import, maven 2.0.9. Only in<dependencyManagement>To refer to the dependencies of other projects.

    • Examples

      <! -- Introducing Projects io.fsjohnhuang.deps Dependency of -- >
      <dependencyManagement>
          <dependency>
              <groupId>io.fsjohnhuang</groupId>
              <artifactId>deps</artifactId>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencyManagement>
    • On type POM
      If the project has a large number of dependencies, the POM.xml The file is bloated, even if the parent POM is adopted, the problem can not be solved well. The project with type POM can package the dependencies into independent projects, and then other business system projects can import related dependencies by introducing these projects, so as to simplify POM files.
    • About the type attribute
      The default value of type attribute is jar, that is, Maven packages the compilation of the project into jar package. When it is set to POM, it means that the project is just a package definition of a bunch of related dependencies. When it is set to APK or EJB, it means that Maven uses Android or EJB related plug-ins to perform tasks when compiling and packaging.

summary

A good memory is not as good as a bad pen. There are a lot of knowledge behind Maven’s powerful function. Write it down for future reference!

Please indicate from:https://www.cnblogs.com/fsjoh…—— ^_ ^Fat John