Unable to resolve library using Gradle. Resolved using Grape - gradle

I'm fairly new to Groovy and I'm trying to wrap my head around Gradle. If I import the org.jvnet.hudson.plugins through Grapes it works perfectly and the dependency is resolved. But if I try to retrieve the dependency using Gradle the dependency is not resolved.
The package org.eclipse.hudson:hudson-core:3.2.1 works with both Gradle and Grape.
A dependency that is not resolved using Gradle
compile 'org.jvnet.hudson.plugins:checkstyle:3.42'
A dependency which is resolved using Grape
#Grab('org.jvnet.hudson.plugins:checkstyle:3.42')
A dependency which is resolved using Gradle
compile 'org.eclipse.hudson:hudson-core:3.2.1'
Error during Gradle build
line 3, column 1.
import hudson.plugins.checkstyle.CheckStyleResultAction;
^
The build.gradle
apply plugin: 'groovy'
repositories {
mavenCentral()
maven {
url "http://repo.jenkins-ci.org/releases/"
}
}
configurations {
ivy
}
sourceSets {
main {
groovy {
srcDirs = ['src/']
}
}
test {
groovy {
srcDirs = ['test/']
}
}
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.11'
compile "org.apache.ivy:ivy:2.4.0"
ivy "org.apache.ivy:ivy:2.3.0"
// Works
compile 'org.eclipse.hudson:hudson-core:3.2.1'
// Does not work
compile 'org.jvnet.hudson.plugins:checkstyle:3.42'
}
tasks.withType(GroovyCompile) {
groovyClasspath += configurations.ivy
}

You're probably not actually downloading the jar you think you are. Looks like the default artifact that comes back from the org.jvnet.hudson.plugins:checkstyle:3.42 dependency is actually a file named checkstyle-3.42.hpi.
To get the jar which contains the classes instead, use:
compile group: 'org.jvnet.hudson.plugins', name: 'checkstyle', version:'3.42', ext: 'jar'
Then that class will be found on your classpath (and you'll be on to locating the next missing dependency).

Related

How to define dependency version only once for whole Gradle multi-module project?

I made a decision to migrate from Dependency Management Plugin to Gradle built-in BOM import support. Since Gradle built-in BOM import support has better performance But
I run into the issue:
I cannot find alternatives for dependency and dependencySet in native Gradle:
dependencyManagement {
dependencies {
dependency("org.springframework:spring-core:4.0.3.RELEASE")
}
}
//or
dependencyManagement {
dependencies {
dependencySet(group:'org.slf4j', version: '1.7.7') {
entry 'slf4j-api'
entry 'slf4j-simple'
}
}
}
and then I could use dependency without version
dependencies {
compile 'org.springframework:spring-core'
}
How can I get the same behavior in naive Gradle? I mean: I'd like to define a version once as I did it when using Dependency Management Plugin
Solution below helps to avoid versions copy-paste. However it isn't the same with Dependency Management plugin.
For Gradle Kotlin Dsl:
You can create buildSrc with you own code, when you can place any constants.
Algorithm:
Create folder buildSrc/src/main/kotlin
Create file buildSrc/src/main/kotlin/Versions.kt with content:
object Versions {
const val junitVersion = "5.5.5" // just example
}
Create file buildSrc/build.gradle.kts with content:
plugins {
`kotlin-dsl`
}
Use the following syntax in your gradle.kts files:
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:${Versions.junitVersion}")
}
For Gradle Groovy:
Create file gradle.properties
Put versions there with syntax like okhttp_version=4.2.0
Use the following syntax in your gradle files:
dependencies {
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: okhttp_version
}
You can do so on the gradle.properties file. I.e.:
# APPLICATION PROPERTIES
name=projectName
group=com.domain
version=1.0.0
description=A brief description
gradleScripts=https://raw.githubusercontent.com/hexagonkt/hexagon/1.2.0/gradle
# DEPENDENCIES VERSIONS
kotlinVersion=1.3.61
kotlinCoroutinesVersion=1.3.2
Or in settings.gradle if you don't want to create another file:
rootProject.name = "hexagon-contact-application"
gradle.rootProject {
allprojects {
version = "1.0.0"
group = "org.hexagonkt"
description = "Contact application backend api"
}
extensions.gradleScripts = "https://raw.githubusercontent.com/hexagonkt/hexagon/1.0.18/gradle"
extensions.kotlinVersion = "1.3.50"
extensions.kotlinCoroutinesVersion = "1.3.2"
extensions.hexagonVersion = "1.0.21"
extensions.logbackVersion = "1.2.3"
extensions.bcryptVersion="0.8.0"
extensions.javaJwtVersion="3.8.2"
}
And if you want to avoid adding the version variable to all related dependencies, you can create a method in the build file:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.50'
}
apply from: "$gradleScripts/kotlin.gradle"
apply from: "$gradleScripts/service.gradle"
apply from: "$gradleScripts/junit.gradle"
defaultTasks("test")
mainClassName = 'com.hexagonkt.contact.ApplicationKt'
applicationDefaultJvmArgs = ["-Xms64M", "-Xmx2G", "-XX:+UseNUMA", "-XX:+UseParallelGC"]
dependencies {
httpkt(it, "http_server_jetty")
httpkt(it, "store_mongodb")
httpkt(it, "hexagon_web")
implementation("at.favre.lib:bcrypt:$bcryptVersion")
implementation("com.auth0:java-jwt:$javaJwtVersion")
testImplementation("com.hexagonkt:port_http_client:$hexagonVersion")
}
private void httpkt(final def dependencies, final String artifact) {
dependencies.implementation("com.hexagonkt:$artifact:$hexagonVersion")
}

package fat jar using gradle with transitive dependencies

I would like to create fat jar using gradle. I am using the shadowJar plugin(com.github.johnrengelman.shadow).
The problem that I am facing is that I cannot find a way to instruct the plugin to package a certain libraries with their transitive dependencies.
For example: I am using hive-jdbc which depends on org.apache.httpcomponents:httpclient:4.5.2, however the final jar packaged by shadowJar does not include httpclient
Another option is to drop the shadowJar section from the gradle.build file, but then the plugin will include everything that is part of the compile classpath and huge jar file will be created.
I am using this build file in order to package a jar for spark-submmit that includes many libraries as part of the runtime environment, so I do not need all the compile dependencies to be packaged (just the ones I am specifying explicitly and their transitive dependencies)
build.gradle:
import com.github.jengelman.gradle.plugins.shadow.transformers.AppendingTransformer
plugins {
id "com.github.johnrengelman.shadow" version "2.0.3"
}
apply from: "../Parent_2.2/build.gradle"
group 'com.ebates.bigdata'
version '1.0-SNAPSHOT'
dependencies{
compile("com.databricks:spark-avro_2.11:4.0.0"){force = true}
compile("org.apache.hadoop:hadoop-client:2.6.5"){force = true}
compile("org.apache.hadoop:hadoop-aws:2.6.5"){force = true}
compile("org.postgresql:postgresql:9.4.1207"){force = true}
compile("org.apache.hive:hive-jdbc:2.2.0"){force = true}
}
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
shadowJar {
mergeServiceFiles()
transform(AppendingTransformer) {
resource = 'reference.conf'
shadowJar.classifier = ""
zip64 true
dependencies {
include(dependency("com.databricks:spark-avro_2.11:4.0.0"))
include(dependency("org.postgresql:postgresql:9.4.1207"))
include(dependency("org.apache.hive:hive-jdbc:2.2.0"))
}
}
}

Is it possible to use #Grab inside a Gradle build.gradle?

Is it possible to use #Grab inside a Gradle build.gradle file?
My understanding is that Gradle build scripts are written in Groovy and Grape/#Grab is built into Groovy.
But if i attempt to add a #Grab annotation into a build.gradle file it doesn't work.
e.g. adding:
#Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE')
import org.springframework.jdbc.core.JdbcTemplate
it gives me the error
org.gradle.groovy.scripts.ScriptCompilationException: Could not compile build file.
Is this possible?
According to this post, the answer is no, we can't use #Grab.
However, there is an equivalent way to declare a dependency and add it to the classpath, as shown below (usage for the example: gradle go):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath group: 'org.springframework', name: 'spring-orm', version: '3.2.5.RELEASE'
}
}
import org.springframework.jdbc.core.JdbcTemplate
task go() {
doLast {
println 'TRACER : ' + JdbcTemplate.class.getSimpleName()
}
}

gradle protobuf plugin not functioning

I am using the below mentioned protobuf gradle plugin in one project where its working fine but when I referenced the same plugin in a different project, 'gradle clean' is consistently giving me the error copied below:
relevant parts of build.grade (v3.4)
apply plugin: 'com.google.protobuf'
buildscript {
repositories {
mavenCentral()
mavenLocal()
jcenter()
}
dependencies {
// classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.9"
// classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
}
}
def grpcVersion = '1.1.2'
dependencies {
compile "io.grpc:grpc-netty:${grpcVersion}"
compile "io.grpc:grpc-protobuf:${grpcVersion}"
compile "io.grpc:grpc-stub:${grpcVersion}"
}
protobuf {
protoc {
Artifact = 'com.google.protobuf:protoc:3.2.0'
}
plugins {
grpc {
Artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
all()*.plugins {
grpc {
// To generate deprecated interfaces and static bindService method,
// turn the enable_deprecated option to true below:
option 'enable_deprecated=false'
}
}
}
}
error when I run gradle clean
* What went wrong:
Could not compile build file '/xyz/xyz/build.gradle'.
> startup failed:
build file '/xyz/xyz/build.gradle': 102: you tried to assign a value to the class 'org.gradle.api.component.Artifact'
# line 102, column 9.
Artifact = 'com.google.protobuf:protoc:3.2.0'
^
build file '/xyz/xyz/build.gradle': 106: you tried to assign a value to the class 'org.gradle.api.component.Artifact'
# line 106, column 13.
Artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
I have tried protobuf plugins 0.8.0. and 0.8.1 but both give the same error. v0.8.0 works as is in a different project. Any thoughts on how to troubleshoot this further would be appreciated.
It should be artifact, not Artifact. The latter is a class that you try to assign to which will not work, the former is a property you assign to.

Bundling all dependencies in .aar file

I have an SDK project that is referencing quit alot of dependencies in gradle. I have to ask the SDK users to add those dependencies when they use the SDK in their projects. The problem is, every time I add some new dependency or replace current dependency with a new one, I'll have to ask the users to make changes as well, which is not a good practice in my opinion. Is there a way to bundle all the dependencies in the .aar. I am generating the artifact file with the following code.
uploadArchives{
repositories.mavenDeployer {
def deployPath = file(getProperty('aar.deployPath'))
repository(url: "file://${deployPath.absolutePath}")
pom.project {
groupId 'myPackageName'
artifactId 'wingoku-io'
version "0.0.6"
}
}
These are my dependencies:
dependencies {
compile 'com.github.nkzawa:socket.io-client:0.5.0'
compile 'com.android.support:cardview-v7:21.+'
compile 'de.greenrobot:eventbus:2.4.0'
}
EDIT:
I have added the following method in uploadArchives method. The size of the artifact file has increased but when I use the .aar file in app project, it can't find the files.
uploadArchives{
repositories.mavenDeployer {
def deployPath = file(getProperty('aar.deployPath'))
repository(url: "file://${deployPath.absolutePath}")
pom.project {
groupId 'myPackageName'
artifactId 'wingoku-io'
version "0.0.6"
}
**configurations.all {
transitive = true
}**
}
UPDATE:
I tried the following code because #aar was generating errors (couldn't find xx.xx.xx.aar on jcenter()). This still fails, the d
compile ('com.github.nkzawa:socket.io-client:0.5.0'){
transitive=true
}
compile ('com.android.support:cardview-v7:21.+#aar'){
transitive=true
}
compile ('de.greenrobot:eventbus:2.4.0'){
transitive=true
}
Dalvik throws following warnings for all classes:
dalvikvm﹕ Link of class 'Lio/wingoku/sdk/Fetch$6;' failed
It eventually throws exception ClassDefNotFound
Regarding your edit
configurations.all {
transitive = true
}
I'm not sure it as any effect.
I think you should write something like this:
dependencies {
compile 'com.github.nkzawa:socket.io-client:0.5.0'{
transitive=true
}
compile 'com.android.support:cardview-v7:21.+#aar'{
transitive=true
}
compile 'de.greenrobot:eventbus:2.4.0'{
transitive=true
}
}
It just say that
cardview is aar to include in this library.
eventbus and socket.io-client are jar lib to include in aar

Resources