【问题标题】:How to use Oracle JDBC driver in Gradle project如何在 Gradle 项目中使用 Oracle JDBC 驱动程序
【发布时间】:2016-09-24 07:52:59
【问题描述】:

我是 Gradle 项目的新手,我有一个问题。我在互联网上搜索过,但找不到我需要的东西,或者我不知道如何搜索它。 首先我要告诉你我的情况。我有一个 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'
}

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

【问题讨论】:

标签: jdbc gradle build.gradle ojdbc


【解决方案1】:

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 链接以检查该链接上是否存在 jar

在 cmd 中运行此命令以检查是否添加了所有依赖项

 gradle dependencies 

【讨论】:

    【解决方案2】:

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

    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'
    

    【讨论】:

      【解决方案3】:
      repositories {
         flatDir { dirs "libs" }
         }
         dependencies {
           compile files( 'libs/ojdbc-16.jar')
         }
      

      在项目根目录下创建“libs”目录并放入其中。

      【讨论】:

        【解决方案4】:

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

        例如,如果您想在 Java 8 中使用 OJDBC 版本 19,您可以在 Maven Central 中找到 ojdbc jar。请注意,组名中有错字。它应该是 com.oracle.ojdbc 而不是 com.oracle.jdbc

         repositories {
            mavenCentral()
        }
        
        dependencies {
            compile "com.oracle.ojdbc:ojdbc8:19.3.0.0"
        }
        

        【讨论】:

          【解决方案5】:

          除了 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

          【讨论】:

            【解决方案6】:

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

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

            1. 在 build.gradle 中添加:

            依赖{

            ...

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

            ...

            }

            【讨论】:

              【解决方案7】:

              您可以尝试为 Gradle 重用本地 Maven 存储库:

              • 从 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 以进行编译

              【讨论】:

              • 我不想使用 maven,有没有办法只有 gradle 也可以。
              【解决方案8】:

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

              目前您有 3 种选择:

              (+1 使用 maven)

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

              【讨论】:

                【解决方案9】:

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

                compile files('libs/ojdbc7.jar')
                

                在这种情况下,无需添加 flatDir 存储库。阅读official user guide

                【讨论】:

                • 不应在源代码控制中存储/版本控制 jar。请改用存储库管理器或 Maven 坐标。
                猜你喜欢
                • 1970-01-01
                • 2011-04-23
                • 2012-03-30
                • 2015-12-14
                • 2010-10-15
                • 2015-12-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多