How to avoid "cannot load Java class oracle.jdbc.OracleDriver" in a JRuby Gradle project? - oracle

When trying to include the Oracle JDBC driver (ojdbc7.jar) in my JRuby Gradle project, I always get a "cannot load Java class oracle.jdbc.OracleDriver" at runtime. Here's my basic build.gradle:
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:[1.2.2,2.0)'
classpath 'com.github.jruby-gradle:jruby-gradle-plugin:%%VERSION%%'
classpath 'com.github.jruby-gradle:jruby-gradle-jar-plugin:1.3.3'
}
}
apply plugin: "com.github.jruby-gradle.jar"
repositories { jcenter() }
dependencies {
jrubyJar "rubygems:colorize:0.7.7+"
jrubyJar 'org.slf4j:slf4j-simple:1.7.12'
}
jrubyJar {
initScript "${projectDir}/entrypoint.rb"
}
And here's my entrypoint.rb:
require 'java'
java_import 'java.sql.DriverManager'
java_import 'oracle.jdbc.OracleDriver'
puts "Hello world"
Output of build + run steps:
frank$ ./gradlew jrubyJar
:prepareJRubyJar UP-TO-DATE
:jrubyJar UP-TO-DATE
BUILD SUCCESSFUL
Total time: 2.027 secs
frank$ java -jar build/libs/plsql-unit-tester-jruby.jar
NameError: cannot load Java class oracle.jdbc.OracleDriver
...
Following the advice in How to use oracle jdbc driver in gradle project, I tried adding this to my build.gradle:
dependencies {
compile files('lib/ojdbc7.jar')
}
But this causes an error at compile time:
Could not find method compile() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I then tried to add it as a runtime dependency as suggested in How to add OJDBC6.jar in build.gradle file?:
dependencies {
runtime files('lib/odjbc7.jar')
}
But this again raises a compile error:
Could not find method runtime() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
So I'm stuck - how can I correctly add odjbc7.jar (or any external jar) as a dependency in my JRuby Gradle project?

A workaround for the problem is to install the Oracle JDBC driver into the local Maven repository and add the mavenLocal() as repository and the driver jar as an additional dependency.
The driver can be installed like so:
mvn install:install-file -Dfile=ojdbc7.jar -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.2 -Dpackaging=jar
The final build.gradle looks like this:
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.github.jruby-gradle:jruby-gradle-plugin:1.3.3'
classpath 'com.github.jruby-gradle:jruby-gradle-jar-plugin:1.3.3'
}
}
apply plugin: "com.github.jruby-gradle.jar"
repositories {
mavenLocal()
jcenter()
}
dependencies {
jrubyJar "rubygems:colorize:0.7.7+"
jrubyJar 'org.slf4j:slf4j-simple:1.7.12'
jrubyJar 'com.oracle:ojdbc7:12.1.0.2'
}
jrubyJar {
initScript "${projectDir}/entrypoint.rb"
}

Related

Gradle is unable to locate the bintray repository for kotlinx-serialization

I am trying to use kotlinx.serialization and have had just no luck. Here is the pertinent portion of the build.gradle
buildscript {
ext.kotlin_version = '1.3.71'
repositories {
mavenCentral()
google()
jcenter()
maven("https://kotlin.bintray.com/kotlinx")
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}
}
This dies on the vine:
Could not find method maven() for arguments [https://kotlin.bintray.com/kotlinx]
on repository container of type org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler
What is an up-to-date way to incorporate kotlinx-serialization ?
It looks like the actual version for kotlinx.serialization is 1.3.70, not 1.3.71 At least their master branch depends on 1.3.70 kotlin components
The arguments of method maven in org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler is Closure,and the context object is type org.gradle.api.artifacts.repositories.MavenArtifactRepository,so the correct script would be like
repositories {
mavenCentral()
maven {
url="https://kotlin.bintray.com/kotlinx"
}
}
what's more,kotlinx.serialization exists in mavencentral,so there is no need to add your custom maven repository

Gradle plugin that loads org.springframework.boot into projects

I'm writing a custom gradle plugin Foo and I want to load the org.springframework.boot plugin
into projects that apply the Foo plugin. I can load various other plugins this way, but this
particular plugin doesn't want to behave the same way.
Foo build.gradle
buildscript {
ext {
springBootVersion = "2.1.3.RELEASE"
}
}
apply plugin: "groovy"
repositories {
maven { url "http://custom.repo/blah" }
}
dependencies {
implementation gradleApi()
implementation localGroovy()
implementation("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
Foo plugin
class BuildPlugin implements Plugin<Project> {
#Override
void apply(Project project) {
project.repositories {
maven { url "http://custom.repo/blah" }
}
project.plugins.apply("org.springframework.boot")
}
}
Project build.gradle
buildscript {
dependencies {
classpath files("/some/cool/path/foo-plugin.jar")
}
}
apply plugin: "com.whatever.foo-id"
Project build output
$ ./gradlew --stacktrace clean build
FAILURE: Build failed with an exception.
* Where:
Build file '/cool/project/location/bar/build.gradle' line: 40
* What went wrong:
A problem occurred evaluating root project 'bar'.
> Failed to apply plugin [id 'com.whatever.foo-id']
> Plugin with id 'org.springframework.boot' not found.
Is it possible to apply a plugin 1 from plugin 2 where plugin 1 is a classpath dependency?
This isn't possible. The classpath is what pulls in the plugins, so it is impossible for them to modify the thing before they are pulled in.

gradle error Could not find method dependencyManagement()

Below is my build.gradle
buildscript {
ext {
springBootVersion = '2.0.0.M3'
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'org.springframework.boot'
apply plugin: 'maven-publish'
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-starter-parent:Brixton.SR7'
}
}
dependencies {
compile("org.springframework.cloud:spring-cloud-starter-eureka")
compile "org.elasticsearch:elasticsearch:5.5.0"
testCompile('org.springframework.boot:spring-boot-starter-test')
}
I was using gradle 2.14 and got the below error
> Failed to apply plugin [id 'org.springframework.boot']
> Spring Boot plugin requires Gradle 3.4 or later. The current version is Gra
dle 2.14
Then I upgraded gradle to 3.4 as suggested in the error message.
Now I get the below error
Could not find method dependencyManagement() for arguments [build_79bcact4bkf1
sckkod1j3zl7l$_run_closure1#4a2d71c9] on root project 'myproject'
of type org.gradle.api.Project.
Is the method dependencyManagement() no longer available in gradle 3.4 ?
If anybody is aware of the alternate method to be used in gradle 3.4 , kindly revert
To use this DSL you have to provide the dependency-management-plugin:
buildscript {
repositories {
maven {
jcenter() //or mavenCentral()
}
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
}
}
apply plugin: "io.spring.dependency-management"
Or you can use:
plugins {
id "io.spring.dependency-management" version "1.0.3.RELEASE"
}
More details here.
For me the fix was replacing the distributionUrl in the gradle-wrapper.properties with:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
and updating the dependencies in the build.gradle file to:
dependencies { classpath "com.android.tools.build:gradle:7.0.4" }
In Gradle 7 this error is also caused by importing a BOM using:
dependencyManagement {
imports {
mavenBom "tech.jhipster:jhipster-dependencies:${jhipsterDependenciesVersion}"
}
}
In Gradle 7 you need to import your BOM in the following way:
implementation platform("tech.jhipster:jhipster-dependencies:${jhipsterDependenciesVersion}")

Not able to integrate testng with gradle. Error while building jar

I am new to gradle. Trying to integrate the testNG with Gradle and want to create jar file using "gradle build" command
This is how my build.gradle looks like
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version,
'Main-Class': 'org.testng.TestNG'
}
}
repositories {
mavenCentral()
}
dependencies {
testCompile 'org.testng:testng:6.9.10'
}
test {
useTestNG(){
include '**/*'
}
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
However when I run the "gradle build" command I am getting following error for each testNG annotation
: error: cannot find symbol
#Test
^
Symbol: class Test
Location: class helloWorld

build.gradle - error deploying artifact

I'm having issues with publishing to local nexus maven repository.
I admit, I don't have much experience with using gradle.
I will say that I have tried understanding the documentation and examples that are given through the gradle website (along with a few stackoverflow questions).
I am getting the following error when I try to publish:
Execution failed for task ':publishMavenPublicationToMavenRepository'.
> Failed to publish publication 'maven' to repository 'maven'
> Error deploying artifact 'com.myproject:myproject-sdk:jar': Error deploying artifact: Resource to deploy not found: File: http://git.site.com:8081/nexus/content/repositories/releases/com/myproject/myproject-sdk/3.0.0/myproject-sdk-3.0.0.jar does not exist
the entire build.gradle file looks like this:
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'build-version'
buildscript {
repositories {
maven { url "http://git.site.com:8081/nexus/content/groups/public" }
maven { url 'https://geonet.artifactoryonline.com/geonet/public-releases' }
mavenCentral()
}
dependencies {
classpath 'nz.org.geonet:gradle-build-version-plugin:1.+'
}
}
repositories {
maven {
url "http://git.site.com:8081/nexus/content/groups/public"
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'com.google.code.gson:gson:2.2.4'
compile 'commons-codec:commons-codec:1.9'
}
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
publishing {
publications {
maven(MavenPublication) {
artifactId = 'myproject-sdk'
groupId = 'com.myproject'
version '3.0.0'
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
repositories {
maven {
// nexus maven credentials
credentials {
username "MY_USERNAME"
password ""MY_PASSWORD"
}
if(version.endsWith('-SNAPSHOT')) {
url "http://git.site.com:8081/nexus/content/repositories/snapshots"
} else {
url "http://git.site.com:8081/nexus/content/repositories/releases"
}
}
}
}
I am using Android Studio and this project is a Java project.
I don't understand the message because it says "Resource to deploy not found" and it points to a url at nexus.
I would expect a message like "Resource to deploy not found: File: c:/directory_that_doesn't_exist/whatever.jar"
Additional Information -
The gradle tasks listed under 'All tasks' are:
assemble
build
buildDependents
buildNeeded
check
classes
clean
compileJava
compileTestJava
generatePomFileForMavenPublication
jar
javadoc
processResources
processTestResources
publish
publishMavenPublicationToMavenLocal
publishMavenPublicationToMavenRepository
publishToMavenLocal
sourceJar
test
testClasses

Resources