How to use Oracle JDBC driver in Gradle project - jdbc

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

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

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

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"
}

In addition to correct answer, I want to share my experience how I solve a problem with ojdbs dependence (used gradle and Intellij Idea).
Go to the oracle site and download jdbs file(s). I chose to download the full archive - ojdbc8-full.tar.gz
Unpack the archive in someone directory (for example c:\folder\OJDBC8-Full)
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:
In build.gradle add:
dependencies {
...
compile files('libs/OJDBC8-Full') //OJDBC8-Full - it is name what you specify for librare
...
}

Since SSO-based authentications are not available in gradle:
Currently you have 3 alternatives:
download manually and copy the file (see above)
use a proxy to authenticate (and register an account for oracle maven repo)
if you have an internal repository: you can use your repo to proxy/cache oracle's one (e.g.: Nexus Oracle settings: https://support.sonatype.com/hc/en-us/articles/213465728-How-to-configure-a-proxy-repository-to-maven-oracle-com)
(+1 use maven)
see: https://discuss.gradle.org/t/support-for-maven-repositories-that-use-realm-based-sso/14456

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

repositories {
flatDir { dirs "libs" }
}
dependencies {
compile files( 'libs/ojdbc-16.jar')
}
create "libs" directory under project root and put that into it.

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'

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

Related

Gradle does not use the Maven Local Repository for a new dependency

I have Maven with M2_HOME defined to /Users/manuelj/apache/maven/3.2.5
I have the settings.xml file, located on /Users/manuelj/apache/maven/3.2.5/conf/settings.xml
where I have the following declared:
<localRepository>/Users/manuelj/apache/maven/repository</localRepository>
Until here with Maven all works fine. Any new dependency goes there.
I have a project based with Gradle, among many things in my build.gradle, exists the following:
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'application'
version = '1.0.0'
sourceCompatibility = '1.8'
repositories {
mavenLocal()
mavenCentral()
}
… more
Until here, all works fine too. Code compile, executes well.
My confusion is the following.
According with my understanding is that Gradle's mavenLocal() should use the same path than <localRepository> defined on Maven's settings.xml file.
Now confirming that in the Maven local repository exists some dependencies already downloaded.
When I execute for example gradle build, I did realize that
If a dependency already exists from the Maven Local Repository, it is used from there.
If a dependency does not exist from the Maven Local Repository Gradle download the new dependency to: /Users/manuelj/.gradle/caches/modules-2/files-2.1
I want that the new dependency go directly to the same Maven Local Repository.
Therefore, what extra configuration is need it?
Resolving Dependencies From Local Maven Repository
Gradle is able to resolve artifacts stored in the local Maven repository (usually ~/.m2/repository) via mavenLocal().
According to the documentation, mavenLocal() is resolved like this:
Gradle uses the same logic as Maven to identify the location of your local Maven cache. If a local repository location is defined in a settings.xml, this location will be used. The settings.xml in USER_HOME/.m2 takes precedence over the settings.xml in M2_HOME/conf. If no settings.xmlis available, Gradle uses the default location USER_HOME/.m2/repository.
To resolve artifacts from a non-standard local Maven repository, you can use the following configuration in your build.gradle:
repositories {
maven {
url '/Users/manuelj/apache/maven/repository'
}
}
(From: How does Gradle resolve the directory of the local maven repository?)
Custom Maven repositories are documented here.
Storing Artifacts in the Local Maven Repository
Gradle stores resolved dependencies in its own Dependency Cache. The dependency cache is so much more than just a simple Maven artifact repository:
Stores binaries (jars), artifact meta-data (POM, Ivy files), dependency resolution results and module descriptors.
Tuned for performance, for example shorter file paths.
De-duplicates artifacts: Same binaries are stored only once.
Tracks where a dependency came from. A dependency resolved from jcenter() might be different to the one resolved from mavenCentral().
Automatic, time and usage bases, cache cleanup.
Artifacts produced by the build can be easily pushed to the local Maven repository via publishToMavenLocal task contributed by the Maven Publish Plugin.
But what about resolved dependencies? For the aforementioned reasons, Gradle cannot store dependencies in the local Maven repository. There's currently no built-in functionality to even publish dependencies to the Maven's local repository from the build script. So what are your options:
Create a shell script that does the necessary legwork. Daniel Dietrich once wrote one and published it on Twitter (Gist).
Use an artifact proxy like Nexus or Artifactory. Maven and Gradle can be configured to consume dependencies from the same proxy. This setup is quite common in professional environments and my personal preference.
Use
mavenLocal()
for example:
buildscript {
ext {
springBootVersion = '2.0.0.M1'
}
repositories {
mavenCentral()
mavenLocal()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
mavenLocal()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
compile('com.oracle:ojdbc6:11.2.0.4')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
I am using Gradle 3.5
This drove me to drink.
If I do mvn install for a project having a version of 1.1.1.SNAPSHOT it goes into my local maven repository (~/m2/repository/...) with no errors. However, Gradle using mavenLocal() will not attempt to locate it in the local maven repository (having used ./gradlew bootRun --debug and inspecting the logs).
If I change the version to 1.1.1-SNAPSHOT (note the dash), then Gradle will attempt, and will find the repository.
It doesn't make sense to me that Maven finds this to be a valid version number for local use, but Gradle completely ignores it.
I came across this issue because I'm working on a legacy project where I need to run my build with the sudo gradle build command. The build involves copying XSD files, which require root permissions. I opted not to employ the solutions of the previous answers because I didn't want to change the build file; I didn't want to accidentally checkin my build.gradle changes. What I found was that Gradle was checking for mavenLocal in the /var/root/.m2 folder. My solution was to copy /Users/me/.m2/settings.xml to /var/root/.m2 and add a line for the localRepository to point back to my /Users/me/.m2 folder. A sample line and where to add it is:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/Users/me/.m2/repository</localRepository>
<profiles>

Gradle dependency management using pom.xml

In build.gradle we specify the dependencies as
compile group: 'org.apache.pig', name: 'pigunit', version: '0.11.0-cdh4.5.0', transitive: true
Running gradle cleanEclipse eclipse sets up the projects(adds the jar to classpath)
However there are only maven dependencies available for some APIs
(I am trying to run jersey 2.x examples bundle from https://jersey.java.net/download.html and it provides only pom.xml)
EDIT:
I know i can specify
compile group: 'groupId', name: 'artifactId', version: 'version' gradle but doing it manually for all dependencies or writing a program to do so should not be natural gradle way.
Gradle provides a maven plugin http://gradle.org/docs/current/userguide/maven_plugin.html.I haven't tried it out but it should be able to do it
Gradle supports Maven dependencies. Just specify the dependencies in the same way as your example:
compile group: 'groupId', name: 'artifactId', version: 'version'
To lookup the the artifact coordinates, you can use sites like http://search.maven.org
The only thing you have to make sure is to include either your internal Maven repository (if you are in a company which has one) or Maven Central:
repositories {
mavenCentral()
}
or
repositories {
maven {
url "http://repo.mycompany.com/maven2"
}
}

Dependenciies in Gradle not working correctly

We have a project that is using Java 1.5 and we are trying to convert from Maven to Gradle.
We have a repository that is local to us containing all the versions of all the jars we need as the dev environment has no access to the internet.
The problem we are seeing is that it cannot find the commons-io jar and keeps trying to goto the external maven repo. we have not even set that up so where is it finding it from?
we have repositories and dependencies set up in the All projects section as follows
allprojects {
apply plugin: 'java'
sourceCompatibility = 1.5
targetCompatibility = 1.5
project.tasks.withType(AbstractCompile, { AbstractCompile ac -> ac.options.bootClasspath = "C:/Program Files/java/1.5.0_14/jre/lib/rt.jar" })
repositories {
mavenLocal()
maven { url "http://internalrepo/maven-local" }
}
dependencies {
compile "org.apache.commons:commons-io:1.3.2"
}
But its reporting
Could not resolve org.apache.commons:commons-io:1.3.2.
inconsistent module metadata found
even though it works fine in Maven using mvn install
Gradle will never query a repo that isn't set up. mavenlocal() is misspelled (should be mavenLocal()), which will make the build fail. "Inconsistent metadata" could mean that the group ID, artifact ID, or version in the POM doesn't match the one in the build script. mavenLocal() should only be used if the Gradle build needs to exchange artifacts with local Maven builds.
Found the issue,
Unbeknownst to me there was a hidden repo in the maven settings.xml in the maven install folder.
Adding that resolved the issue.

How do I define a simple Gradle project with only a single jar file?

I have a Gradle project that depends on an external jar file. Currently I'm defining the dependency like this:
dependencies {
compile files('/path/to/my/jar/library.jar')
}
However I want to include it as a project dependency instead, like this:
dependencies {
compile project(':whatGoesHere?')
}
I assume I need to define a new Gradle project that contains the jar file but I don't know how to do this. I'm wondering about things like:
Do I just need to create a new build.gradle or are there more steps?
What would go in the build.gradle file?
Assume the new project contains nothing but the jar file (since it does). Also assume I know almost nothing about Gradle (because I don't!).
P.S. If it matters, this is an Android Gradle project.
As a roundup for our discussion, I'll bring simple example of "build.gradle" file, using maven local and maven central repositories:
apply plugin: 'maven'
apply plugin: 'java'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'commons-io:commons-io:2.4'
testCompile 'junit:junit:4.11'
}
Explanation:
"apply plugin: 'maven'" enables maven plugin, which is needed for dependency download.
"apply plugin: 'java'" enables java compilation tasks for your project.
"repositories" declares one or more repositories (maven or ivy), from where artifacts (jar libraries) will be downloaded.
"mavenLocal" refers to so-called local maven repository, which is located in "~/.m2/repository" folder on your computer. local maven repository effectively caches external repositories, but it also allows installation of local-only artifacts.
"mavenCentral" refers to maven central.
"dependencies" lists your project dependencies, either other projects or artifacts (jars).
"compile" is a configuration supported by "java" and "groovy" plugins, it tells gradle: "add these libraries to the classpath of the application during compilation phase".
"testCompile" is another configuration supported by "java" and "groovy" plugins, it tells gradle: "add these libraries to the classpath of the application during test phase".
'commons-io:commons-io:2.4' is "coordinates" of the artifact within maven repository, in form group:name:version.
You can search for well-known java libraries at address: http://mvnrepository.com/ and then include their coordinates in "build.gradle". You don't need to download anything - gradle does it for you automatically.

How to publish in order to resolve latest.integration with gradle?

What I have is a maven repository (nexus) to which maven has been publishing. In each artifact version folder in my artifact repository folder there are the standard maven artifacts: a maven-metadata.xml, a jar, and a pom.xml, etc.
Now I want to resolve these using gradle. In my gradle.build file if I list them as:
dependencies {
compile group: 'com.company', name: 'artifact', version: '1.0-SNAPSHOT'
}
Then they will resolve correctly. However, I want to use the version "latest.integration" so that I can automatically integrate the latest versions of my dependencies. When I do this though, gradle fails to resolve it.
I imagine that gradle is looking for some ivy specific files that maven is not publishing up to the repository in order to resolve latest.integration, but I am not sure. Should I go back and re-publish all of my upstream dependencies with gradle before trying to resolve down stream? It would seem that since gradle supports maven repositories under the repositories element that it should already know how to interpret "latest.integration" for that repository type.
This is my repositories section:
repositories {
mavenCentral()
maven { url "http://<server>/nexus/content/repositories/snapshots" }
}
Thank you for any help you can provide
latest.integration is an Ivy concept, and only works for Ivy repositories. In other words, both publication and consumption would have to happen in an Ivy-compatible manner. (Gradle is capable of this; not sure about Nexus.)
The obvious alternative is to use Maven snapshot dependencies. What do you hope to gain from using latest.integration?

Resources