Gradle - unable to track down transitive dependency - spring

I have two modules: common and domain. Domain is a dependency of common. In domain, I'm trying to add the latest version of Spring Data Elasticsearch but it keeps reverting back to an old version. My domain's build.gradle file looks like this:
domain build.gradle
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
}
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-redis")
compile("org.springframework.data:spring-data-elasticsearch:2.0.1.RELEASE")
compile 'org.slf4j:slf4j-api'
compile 'com.google.guava:guava:19.0'
compile 'com.google.code.gson:gson:2.4'
testCompile "org.mockito:mockito-core:1.+"
}
The version for elasticsearch here is 2.0.1.RELASE However, if I run dependencyInsight in common, it is retrieving 1.3.4.RELEASE instead:
gradle dependencyInsight --dependency elasticsearch --configuration compile
:common:dependencyInsight
Download https://repo1.maven.org/maven2/org/springframework/data/spring-data-elasticsearch/1.3.4.RELEASE/spring-data-elasticsearch-1.3.4.RELEASE.pom
org.elasticsearch:elasticsearch:1.5.2 (selected by rule)
\--- org.springframework.data:spring-data-elasticsearch:1.3.4.RELEASE
\--- project :domain
\--- compile
org.springframework.data:spring-data-elasticsearch:1.3.4.RELEASE (selected by rule)
org.springframework.data:spring-data-elasticsearch:2.0.1.RELEASE -> 1.3.4.RELEASE
\--- project :domain
\--- compile
common build.gradle
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
}
}
dependencies {
compile project(':domain')
compile 'com.google.code.gson:gson:2.4'
compile 'org.owasp.encoder:encoder:1.2'
compile 'com.ning:async-http-client:1.9.31'
compile 'org.slf4j:slf4j-api'
compile 'org.springframework.security:spring-security-core'
compile 'org.springframework.security:spring-security-acl:4.0.3.RELEASE'
compile 'javax.mail:javax.mail-api:1.5.4'
compile 'com.sun.mail:javax.mail:1.5.4'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile "org.mockito:mockito-core:1.+"
}
Is there a reason why version 1.3.4.RELEASE is replacing 2.0.1.RELEASE?

You're applying Spring Boot's Gradle plugin to your common project. That means that its dependency management will be controlling the versions of the project's dependencies. To get the version of Spring Data Elasticsearch that you want, you can override Boot's dependency management by adding the following:
dependencyManagement {
dependencies {
dependency 'org.springframework.data:spring-data-elasticsearch:2.0.1.RELEASE'
}
}

Related

Combine resolutionStrategy with exclusion

I am using Gradle 6.5.1. I have added a custom resolutionStrategy to a build.gradle file - but now an exclusion like this is not being applied:
testImplementation("com.example:foo-bar_2.12:$dependencies_version"){
exclude group: 'org.scala-lang', module: 'scala-library'
exclude group: 'org.typelevel', module: 'cats-core_2.12' // <- !! NOT WORKING !!
}
So it seems that custom resolutionStrategies and exclusions are not composable, at least not by default, in Gradle 6.5.1. Is there some way I can make Gradle fall back to its "default" resolutionStrategy if mine is not relevant? If not, what should I do?
Issue
You have to have some special resolutionStrategy in place in order to overwrite the exclusion for cats-core_2.12.
Or
Dependency on cats-core_2.12 is being resolved as transitive dependency from other dependency and not com.example:foo-bar_2.12 as you expect. You should use gradle dependency command and post here the result of where cats-core is being resolved.
Example
I have following simple build.gradle build script with similar exclusion rule and resolutionStrategy as you can see below and cats-core will still be excluded from dependencies as expected
dependencies {
testImplementation('com.github.julien-truffaut:monocle-core_2.13:3.0.0-M5'){
exclude group: 'org.scala-lang', module: 'scala-library'
exclude group: 'org.typelevel', module: 'cats-core_2.13' // <- WORKS
}
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
configurations.all {
resolutionStrategy {
failOnVersionConflict()
preferProjectModules()
force 'org.typelevel:cats-core_2.13:2.4.0'
// cache dynamic versions for 10 minutes
cacheDynamicVersionsFor 10*60, 'seconds'
// don't cache changing modules at all
cacheChangingModulesFor 0, 'seconds'
}
}
Dependencies:
Running following command ./gradlew dependencies
You can see that cats-core is not listed in dependencies as it's excluded.
...
testRuntimeClasspath - Runtime classpath of source set 'test'.
+--- com.github.julien-truffaut:monocle-core_2.13:3.0.0-M5
| \--- org.typelevel:cats-free_2.13:2.6.0
| \--- org.typelevel:simulacrum-scalafix-annotations_2.13:0.5.4
+--- org.junit.jupiter:junit-jupiter-api:5.7.0
| +--- org.junit:junit-bom:5.7.0
| | +--- org.junit.jupiter:junit-jupiter-api:5.7.0 (c)
...
Alternative
If exclusion in your case is not forking for specific dependency, maybe exclusion for all configurations might help like example below:
configurations {
all.collect { configuration ->
configuration.exclude group: 'org.typelevel', module: 'cats-core_2.13'
}
}

IntelliJ doesn't recognize dependencies downloaded via Gradle

I'm having a problem importing dependencies into my code in IntelliJ
My build.gradle file is as follows:
plugins {
id 'groovy'
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.codehaus.groovy:groovy-all:2.3.11'
testImplementation group: 'junit', name: 'junit', version: '4.12'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
}
test {
useJUnitPlatform()
}
Gradle doesn't seem to struggle to find and download the dependencies as I can find them locally in
C:\Users%USER%.gradle\caches\modules-2\files-2.1\com.squareup.retrofit2\retrofit\2.9.0
I've updated IntelliJ to make sure it's up-to-date and have also tried invalidating cache and restarting several times. I'm out of ideas at this point, any help would be kind.

How do I view the dependency tree for Gradle's buildSrc?

I can normally view a dependency tree for a project by running ./gradlew dependencies, but I cannot figure out how to view the dependency tree for the Gradle buildSrc directory.
I have tried accessing it as a sub project, ./gradlew buildSrc:dependencies but that does not work.
build.gradle (for buildSrc)
repositories { mavenCentral() }
dependencies {
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'junit:junit:4.12'
}
Consider the following (the only way that I know of):
$ cd buildSrc
$ gradle dependencies
Note, given your build.gradle example, that buildSrc is its own project in Gradle and needs a proper file. Your example doesn't declare any plugins. Assuming you are using Java, a fix is:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'junit:junit:4.12'
}

Confused with Gradle Dependencies

I am trying to add my dependencies to Gradle Dependencies library in eclipse, and when I run this, it downloads these dependencies, however my other dependencies are in the Gradle Dependencies folder under Gradle Project in eclipse but this one is not. Please help, I just need to add a Gradle Dependency in eclipse.
repositories {
mavenCentral()
maven {
url "http://clojars.org/repo"
}
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile "org.clojars.jmeeks:jfugue-with-musicxml:4.0.3"
testCompile group: 'junit', name: 'junit', version: '4.+'
}
You should tell to gradle about your local repository adding mavenLocal() first in repositories section. Using your current configuration you are telling to gradle that everything is stored in mavenCentral or the custom repo.

Gradle Build Error

My build failed due to this error:
A problem occurred evaluating project ':DBSupport'. > Could not find
method providedCompile() for arguments [project ':Core:Platform '] on
project ':DBSupport'.
Any idea what that means?
description = 'DBSupport main component of DBSupportTool'
dependencies {
providedCompile project(':Core:Platform')
providedCompile project(':Core:Verification')
providedCompile project(':DBSupportWeb')
providedCompile project(':DBSupportEJB')
compile(group: 'commons-lang', name: 'commons-lang', version:'1.0.1') {
/* This dependency was originally in the Maven provided scope, but the project was not of type war.
This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
Please review and delete this closure when resolved. */
}
compile(group: 'commons-logging', name: 'commons-logging', version:'1.0.4') {
/* This dependency was originally in the Maven provided scope, but the project was not of type war.
This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
Please review and delete this closure when resolved. */
}
compile(group: 'javax', name: 'j2ee', version:'1.0') {
/* This dependency was originally in the Maven provided scope, but the project was not of type war.
This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
Please review and delete this closure when resolved. */
}
I assume these modules should in fact be treated as provided (e.g. should not be packages in a WAR archive). If not just change it to compile.
providedCompile configuration is not available in Gradle out of the box. If this is a web module you can just add/apply a war plugin:
apply plugin: 'war'
If not you should be able to add this configuration manually:
configurations {
providedCompile
}
dependencies {
providedCompile project(':Core:Platform')
...
}
sourceSets.main.compileClasspath += configurations.providedCompile
sourceSets.test.compileClasspath += configurations.providedCompile
sourceSets.test.runtimeClasspath += configurations.providedCompile
There is also a propdeps-plugin which claims to do the same thing transparently.
To define a dependency provided as in Maven you need to go the following way:
project(':webgui') {
apply plugin: 'war'
dependencies {
compile project (':domain')
providedCompile 'javax:javaee-api:6.0'
}
}
or an other way would be in case of a project (module) like this:
dependencies {
compile module(":compile:1.0") {
dependency ":compile-transitive-1.0#jar"
dependency ":providedCompile-transitive:1.0#jar"
}
providedCompile "javax.servlet:servlet-api:2.5"
providedCompile module(":providedCompile:1.0") {
dependency ":providedCompile-transitive:1.0#jar"
}
runtime ":runtime:1.0"
providedRuntime ":providedRuntime:1.0#jar"
testCompile "junit:junit:4.11"
moreLibs ":otherLib:1.0"
}

Resources