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

如何在Gradle项目中使用OracleJDBC驱动程序,jdbc,gradle,build.gradle,ojdbc,Jdbc,Gradle,Build.gradle,Ojdbc,我是Gradle项目的新手,我有一个问题。我在网上搜索过,但找不到我需要的东西,或者我不知道如何搜索。 首先我要告诉你我的情况。我有一个Gradle项目,我想在将来与jenkins一起执行几个自动化测试,但现在我想在Eclipse上进行尝试。 我在/lib目录中有oracle jdbc驱动程序,这是我的build.gradle apply plugin: 'java' // In this section you declare where to find the dependenci

我是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时,我使用了“importoracle.jdbc.driver.OracleDriver”这种方式,但我想这对Gradle项目是无效的。 你能帮帮我吗?
提前感谢

您只需添加一个jar作为依赖项,如下所示:

compile files('libs/ojdbc7.jar')

在这种情况下,不需要添加flatDir存储库。请在

中阅读,因为基于SSO的身份验证在gradle中不可用:

目前,您有3种选择:

  • 手动下载并复制文件(见上文)
  • 使用代理进行身份验证(并为oracle maven repo注册帐户)
  • 如果您有内部存储库:您可以使用您的repo代理/缓存oracle的存储库(例如:Nexus oracle设置:)
(+1使用maven)


请参阅:

您可以尝试为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进行编译


除了正确的答案,我还想分享我如何解决OJDB依赖性问题的经验(使用gradle和Intellij Idea)

  • 转到并下载jdbs文件。我选择下载完整的归档文件-ojdbc8-full.tar.gz
  • 解压缩某人目录中的存档文件(例如c:\folder\OJDBC8 Full)
  • 在Intellij Idea中,转到项目结构/库,按“+”符号并指定文件夹的路径(OJDBC8 Full)。指定名称:
  • 在build.gradle中添加:
  • 依赖关系{

    编译文件('libs/OJDBC8 Full')//OJDBC8 Full-这是您为libare指定的名称


    }

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

    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站点:

    Maven站点:


    时间是2019年,甲骨文公司最终决定将其出租

    例如,如果您想在Java8中使用OJDBCVersion19,可以在中找到OJDBCJAR。请注意组名中有输入错误。它应该是com.oracle。ojdbc而不是com.oracle。jdbc

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

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

    下面是一个简单的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'
    

    我不想使用maven,有没有一种方法只有gradle也这么做。的可能副本不应该是在源代码管理中存储/版本控制JAR。改为使用存储库管理器或maven坐标。
    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'