如何在 Gradle 项目中使用 Oracle JDBC 驱动程序

How to use Oracle JDBC driver in Gradle project

我是 Gradle 项目的新手,我有一个问题。我在 Internet 上搜索过,但找不到我需要的东西,或者我不知道如何搜索。 首先我要告诉你我的情况。我有一个 Gradle 项目,我想在未来使用 jenkins 执行几个自动化测试,但现在我想在 Eclipse 上尝试。 我在 /lib 目录中有 oracle jdbc 驱动程序,这是我的 build.gradle

    apply plugin: 'java'

// In this section you declare where to find the dependencies of your project
repositories {
    jcenter()
    //mavenCentral()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    compile 'org.slf4j:slf4j-api:1.7.21'
    compile 'org.seleniumhq.selenium:selenium-java:2.+'
    compile 'org.testng:testng:6.+'
    //compile 'com.oracle:ojdbc14:10.2.0.4.0'
    //testCompile 'net.sourceforge.jexcelapi:jxl:2.6.12'
    testCompile 'info.cukes:cucumber-core:1.+'
    testCompile 'info.cukes:cucumber-java:1.+'
    testCompile 'info.cukes:cucumber-junit:1.+'
    testCompile 'junit:junit:4.12'
}

repositories {
  flatDir(dir: 'libs')//, name: 'Local libs'
}

dependencies {
  compile name: 'ojdbc7'
}

我想在一个 class 中使用这个 jdbc 驱动程序,但我不知道如何使用它。当我尝试使用 Maven 时,我使用了这种方式 "import oracle.jdbc.driver.OracleDriver;" 但我猜这对 Gradle 项目无效。 你能帮我吗? 提前致谢

您可以简单地添加一个 jar 作为依赖项,如下所示:

compile files('libs/ojdbc7.jar')

并且在这种情况下无需添加 flatDir 存储库。在 official user guide

中阅读相关信息

由于基于 SSO 的身份验证在 gradle 中不可用:

目前您有 3 个备选方案:

(+1 使用 maven)

参见:https://discuss.gradle.org/t/support-for-maven-repositories-that-use-realm-based-sso/14456

您可以尝试将本地 Maven 存储库重新用于 Gradle:

  • 从 Oracle 站点下载 ojdbc7.jar
  • 将 jar 安装到本地 Maven 存储库中:

    mvn install:install-file -Dfile=ojdbc7.jar -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.1 -Dpackaging=jar
    
  • 检查您是否已将 jar 安装到 ~/.m2/ 本地 Maven 存储库

  • build.gradle 文件中启用本地 Maven 存储库:

    repositories {  
        mavenCentral()  
        mavenLocal()  
    }  
    
    dependencies {  
        compile ("com.oracle:ojdbc7:12.1.0.1")  
    }  
    
  • 现在你应该在你的项目中为编译启用了 jar

除了正确答案之外,我想分享一下我如何解决 ojdbs 依赖问题的经验(使用 gradle 和 Intellij Idea)。

  1. 转到 oracle site 并下载 jdbs 文件。我选择下载完整存档 - ojdbc8-full.tar.gz
  2. 在某个目录中解压存档(例如 c:\folder\OJDBC8-Full)
  3. 在 Intellij Idea 中转到项目 Structure/Libraries,按“+”符号并指定解压后的文件夹路径(OJDBC8-Full)。指定名称:

  1. 在build.gradle中添加:

依赖项{

...

compile files('libs/OJDBC8-Full') //OJDBC8-Full - 这是你为 librare 指定的名称

...

}

除了 mavenCentral 之外,我们的依赖项也使用本地 maven 存储库。 使用本地 maven 存储库的原因是因为来自 Oracle 的 jdbc 驱动程序不可公开访问。 我们必须从 Oracle 下载驱动程序并将其安装在我们本地的 maven 存储库中。

repositories {
    mavenLocal()
}

dependencies {
    compile ("com.oracle:ojdbc6:12.2.0.1")
}

mvn install:install-file -Dfile="\ojdbc6.jar" -DgroupId="com.oracle" -DartifactId="ojdbc6" -Dversion="12.2.0.1" -Dpackaging="jar" -DgeneratePom="true"

驱动程序的 Oracle 站点:

https://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html

Maven 站点:

https://maven.apache.org/download.cgi

时间是2019年,甲骨文终于决定让“Maven Central becomes a distribution center for the Oracle JDBC drivers”。

例如,如果要将 OJDBC 版本 19 与 Java8 一起使用,您可以在 Maven Central 中找到 ojdbc jar。请注意组名中有错别字。应该是 com.oracle.ojdbc 而不是 com.oracle.jdbc

 repositories {
    mavenCentral()
}

dependencies {
    compile "com.oracle.ojdbc:ojdbc8:19.3.0.0"
}
repositories {
   flatDir { dirs "libs" }
   }
   dependencies {
     compile files( 'libs/ojdbc-16.jar')
   }

在项目根目录下创建"libs"目录并将其放入其中。

下面是一个简单的 gradle 构建,它使用来自 Maven Central 的新 19.7 JDBC 驱动程序。 gradle run 将开始 com.oracle.demo.App,当然,必须将其更改为 运行 您的 class。

apply plugin: 'java'
apply plugin: 'application'

repositories {
    mavenCentral()
}

dependencies {
  implementation 'com.oracle.database.jdbc:ojdbc8-production:19.7.0.0'
  testImplementation 'junit:junit:4.+'
}

sourceCompatibility = 1.11
targetCompatibility = 1.11

mainClassName = 'com.oracle.demo.App'

repositories {
    mavenCentral()
}


dependencies {
  https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc10
  implementation group: 'com.oracle.database.jdbc', name: 'ojdbc10', version: '19.12.0.0'

}

只需添加这个 Maven 依赖项或任何你想要的 ojdbc 版本, 如果在构建 gradle

时出现错误,请确保也单击 maven link 以检查 link 上是否存在 jar

运行 cmd 中的此命令用于检查是否添加了所有依赖项

 gradle dependencies