20

I'm new with Gradle projects and I have one question. I've searched in Internet but I couldn't find what I need or maybe I couldn't know how to search it. First I'm going to tell you my case. I have a Gradle project and I would like to execute several automated tests, in the future with jenkins, but now I want to try on Eclipse. I have the oracle jdbc driver in /lib directory, and this is my 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'
}

I'd like to use this jdbc driver in one class but I don't know how to use it. When I tried with Maven I used this way "import oracle.jdbc.driver.OracleDriver;" but I guess this is not valid for Gradle project. Can you help me, please? Thanks in advance

Vy Do
  • 46,709
  • 59
  • 215
  • 313
javitxu
  • 303
  • 1
  • 2
  • 5
  • Possible duplicate of [How to add ojdbc7 to Java web app by Gradle?](https://stackoverflow.com/questions/37783669/how-to-add-ojdbc7-to-java-web-app-by-gradle) – WhiteKnight Jul 19 '18 at 10:08

9 Answers9

24

You can try reusing your local Maven repository for Gradle:

  • Download ojdbc7.jar from Oracle site
  • Install the jar into your local Maven repository:

    mvn install:install-file -Dfile=ojdbc7.jar -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.1 -Dpackaging=jar
    
  • Check that you have the jar installed into your ~/.m2/ local Maven repository

  • Enable your local Maven repository in your build.gradle file:

    repositories {  
        mavenCentral()  
        mavenLocal()  
    }  
    
    dependencies {  
        compile ("com.oracle:ojdbc7:12.1.0.1")  
    }  
    
  • Now you should have the jar enabled for compilation in your project

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Daniel Mora
  • 2,589
  • 1
  • 25
  • 21
23

You can simply add a jar as dependency, like so:

compile files('libs/ojdbc7.jar')

And there is no need to add a flatDir repository in that case. Read about it in the official user guide

Stanislav
  • 27,441
  • 9
  • 87
  • 82
12

Time is 2019 and Oracle finally decided to let "Maven Central becomes a distribution center for the Oracle JDBC drivers".

For example, if you want to use OJDBC version 19 with Java 8, you can find ojdbc jar in Maven Central. Please be aware there is a typo in group name. It should have been com.oracle.ojdbc instead of com.oracle.jdbc

 repositories {
    mavenCentral()
}

dependencies {
    compile "com.oracle.ojdbc:ojdbc8:19.3.0.0"
}
Yifan Wu
  • 420
  • 5
  • 10
3

In addition to correct answer, I want to share my experience how I solve a problem with ojdbs dependence (used gradle and Intellij Idea).

  1. Go to the oracle site and download jdbs file(s). I chose to download the full archive - ojdbc8-full.tar.gz
  2. Unpack the archive in someone directory (for example c:\folder\OJDBC8-Full)
  3. In Intellij Idea go to the Project Structure/Libraries, press "+" symbol and specify a path to the folder there archive unpacked (OJDBC8-Full). Specify name:

enter image description here

  1. In build.gradle add:

dependencies {

...

compile files('libs/OJDBC8-Full') //OJDBC8-Full - it is name what you specify for librare

...

}

Maksim Ryabovol
  • 361
  • 3
  • 8
2

Since SSO-based authentications are not available in gradle:

Currently you have 3 alternatives:

(+1 use maven)

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

BTakacs
  • 2,397
  • 3
  • 24
  • 26
0

other than mavenCentral use local maven repository as well for our dependencies. The reason for using the local maven repository is because the jdbc driver from Oracle is not publicly accessible. We will have to download the driver from Oracle and install it in our local 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 Site for driver:

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

Maven site:

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

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0
repositories {
   flatDir { dirs "libs" }
   }
   dependencies {
     compile files( 'libs/ojdbc-16.jar')
   }

create "libs" directory under project root and put that into it.

Mugeesh Husain
  • 394
  • 4
  • 13
0

Below is a simple gradle build that uses the new 19.7 JDBC driver from Maven central. gradle run will start com.oracle.demo.App which, of course, has to be changed to run your 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'
Jean de Lavarene
  • 3,461
  • 1
  • 20
  • 28
0

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'

}

simply add this maven dependency or any ojdbc version you want, please make sure to click on maven link too to check if jar is present on that link if you get error while building gradle

run this command in cmd to check if all dependencies added

 gradle dependencies