Gradle force download a different version only for a particular Transitive dependency - gradle

I am force-downloading a dependency using the below gradle-script
implementation ('org.springframework.boot:spring-boot-starter-webflux:2.2.2.RELEASE') {
force = true
}
This dependency always brings me 3.2.13.RELEASE version of io.projectreactor:reactor-core (transitive dependency)
But i want to have 3.3.0.RELEASE of io.projectreactor:reactor-core
Below is my full build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.1.RELEASE'
id 'maven-publish'
id 'jacoco'
id 'org.sonarqube' version '2.7'
}
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Greenwich.SR3'
mavenBom 'io.pivotal.spring.cloud:spring-cloud-services-dependencies:2.1.1.RELEASE'
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.security:spring-security-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
//security
implementation 'org.springframework.boot:spring-boot-starter-security'
//other
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
How to force download only this particular dependency in gradle ?

Use a resolutionStrategy like:
configurations.all {
resolutionStrategy {
force 'io.projectreactor:reactor-core:3.3.0.RELEASE'
}
}

Related

Gradle Spring Boot Dependency Not found Multi Module Project

I am trying to set up multimodule project
RootProject Settings
rootProject.name = 'Abc'
include 'catalog'
include 'catalog-common'
Root Project Abc/build.gradle
plugins {
id 'org.springframework.boot' version '2.7.3' apply false
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
id 'java'
}
subprojects {
group = 'com.abc'
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
targetCompatibility = 1.8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2021.0.3"
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
}
Module catalog-common
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Module Catalog
plugins {
id 'org.springframework.boot' version '2.7.3'
}
dependencies {
implementation project(':catalog-common')
}
in Catalog Project it is expecting again to define spring dependencies but where as I am able to access java static classes
Please help
It looks like your catalog-common is a kind of "library" module, consumed by other sub-projects (catalog and maybe others). If so, you should use the Java Library plugin which can be used for this purpose. You will then need to configure all dependencies you want to "inherit" in consumer projects using api configuration instead of implementation. Dependencies declared in implementation will not leak into consumer projects, this is the expected Gradle behavior.
In you example, catalog-common build script should look like:
plugins {
id("java-library")
}
dependencies {
// choose between api or implementation, depending on the scope you want for each dependency
api 'org.springframework.boot:spring-boot-starter'
api 'org.springframework.boot:spring-boot-starter-actuator'
api 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'org.springframework.boot:spring-boot-starter-test'
}
Please note that it seems a bit strange to configure some of the Spring dependencies like actuator in this common library: this should declared only in the main "application" project ( catalog in your case), unless you want to implement some common code depending on actuator in the catalog-common module.

Use main Gradle project to define all dependencies versions

I want to create one main Gradle project which hosts all project dependencies:
Main Gradle project:
plugins {
id 'org.springframework.boot' version '2.6.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'maven-publish'
}
group = 'com.parent.dependencies'
version = '0.0.1'
sourceCompatibility = '11'
ext {
set('springCloudVersion', "2021.0.1")
artifactory_contextUrl = "http://192.168.1.112/repository"
}
repositories {
mavenCentral()
maven {
url "${artifactory_contextUrl}/plugins-release"
allowInsecureProtocol = true
}
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.boot:spring-boot-starter-validation'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.threeten:threetenbp:1.5.1'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// Lombok
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
testCompileOnly 'org.projectlombok:lombok:1.18.22'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'
// Swagger
implementation group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
implementation 'org.springframework.boot:spring-boot-starter-hateoas:2.6.3'
implementation group: 'com.netflix.hystrix', name: 'hystrix-core', version: '1.5.18'
implementation group: 'org.springframework.security', name: 'spring-security-core', version: '5.6.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
resolutionStrategy {
cacheDynamicVersionsFor 0, "seconds"
cacheChangingModulesFor 0, "seconds"
}
}
bootJar {
enabled = false
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
mavenLocal()
}
}
test {
useJUnitPlatform()
}
Sub project which will read all dependencies versions from parent Gradle project:
plugins {
id 'org.springframework.boot' version '2.6.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'maven-publish'
}
group = 'com.child'
version = '0.0.1'
sourceCompatibility = '11'
ext {
artifactory_contextUrl = "http://192.168.1.112/repository"
}
repositories {
mavenCentral()
maven {
url "${artifactory_contextUrl}/plugins-release"
allowInsecureProtocol = true
}
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.boot:spring-boot-starter-validation'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.threeten:threetenbp'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// Lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
// Swagger
implementation 'io.springfox:springfox-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'com.netflix.hystrix:hystrix-core'
implementation 'org.springframework.security:spring-security-core'
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
dependencyManagement {
imports {
mavenBom "com.parent:com-parent-dependencies:0.0.1"
}
}
bootJar {
enabled = false
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
mavenLocal()
}
}
test {
useJUnitPlatform()
}
When I try to compile the child project I get error:
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
> Could not find org.threeten:threetenbp:.
Required by:
project :
> Could not find io.springfox:springfox-boot-starter:.
Required by:
project :
> Could not find com.netflix.hystrix:hystrix-core:.
Required by:
project :
Do you know how I can define properly the parent Gradle project with all dependencies versions and use the child project without defining dependencies versions?
There’s a relatively new Gradle feature called “version catalogs”. With those you can centrally declare dependencies that you’d like to share between multiple projects of your build (or even between different builds).
In your concrete example, you’d add something like the following to your settings.gradle file:
dependencyResolutionManagement {
versionCatalogs {
libs {
library('spring-cloud-bom', 'org.springframework.cloud:spring-cloud-dependencies:2021.0.1')
library('spring-cloud-starter', 'org.springframework.cloud', 'spring-cloud-starter-netflix-eureka-client').withoutVersion()
library('spring-boot-starter-validation', 'org.springframework.boot', 'spring-boot-starter-validation').withoutVersion()
library('spring-boot-starter-test', 'org.springframework.boot', 'spring-boot-starter-test').withoutVersion()
library('threetenbp', 'org.threeten:threetenbp:1.5.1')
library('spring-boot-starter-actuator', 'org.springframework.boot', 'spring-boot-starter-actuator').withoutVersion()
library('lombok', 'org.projectlombok:lombok:1.18.22')
library('springfox-boot-starter', 'io.springfox:springfox-boot-starter:3.0.0')
library('spring-boot-starter-hateoas', 'org.springframework.boot:spring-boot-starter-hateoas:2.6.3')
library('hysterix-core', 'com.netflix.hystrix:hystrix-core:1.5.18')
library('spring-security-core', 'org.springframework.security:spring-security-core:5.6.1')
library('junit-jupiter-api', 'org.junit.jupiter:junit-jupiter-api:5.8.2')
library('junit-jupiter-engine', 'org.junit.jupiter:junit-jupiter-engine:5.8.2')
}
}
}
Then, in both build.gradle files, you’d replace your dependencies blocks with the following:
dependencies {
implementation libs.spring.cloud.starter
implementation libs.spring.boot.starter.validation
testImplementation libs.spring.boot.starter.test
implementation libs.threetenbp
implementation libs.spring.boot.starter.actuator
// Lombok
compileOnly libs.lombok
annotationProcessor libs.lombok
testCompileOnly libs.lombok
testAnnotationProcessor libs.lombok
// Swagger
implementation libs.springfox.boot.starter
implementation libs.spring.boot.starter.hateoas
implementation libs.hysterix.core
implementation libs.spring.security.core
testImplementation libs.junit.jupiter.api
testRuntimeOnly libs.junit.jupiter.engine
}
As you’re using a Maven BOM for Spring, you’ll also have to use the following updated dependencyManagement block in all projects:
dependencyManagement {
imports {
mavenBom libs.spring.cloud.bom.get().toString()
}
resolutionStrategy {
cacheDynamicVersionsFor 0, "seconds"
cacheChangingModulesFor 0, "seconds"
}
}
That should be it. I could successfully test your sample build configurations with my updates at least on Gradle 7.4.

How to import local external jar having spring cloud vault client dependency

I have a "multi-module" Gradle project named "utils". In that, I have created a new sub-project that will be used as a client for Hashicorp's Vault. I'll use this jar to my Spring Boot microservices as a dependency.
The goal is to have a common configuration for Vault and use it in all the services.
My problem is when I try to auto-wire the VaultTemplate object (presuming it will be added from this jar), in one of my Spring Boot microservices (say secrets-management-service), I could not achieve that.
For development purposes, I have included the jar as a local dependency. I have included both the build.gradle files.
External Jar: vault-client
group = 'com.example.learning'
version = '1.0.0-SNAPSHOT'
buildscript {
ext {
springVersion = '2.4.4'
}
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springVersion}"
}
}
apply plugin: 'java-library'
apply plugin: 'idea'
apply plugin: 'maven-publish'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = '1.8'
ext {
springCloudVersion = '2020.0.2'
}
repositories {
mavenCentral()
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-vault-config'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
Microservice: secrets-management-service
plugins {
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example.learning'
version = ''
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
bootJar {
setArchivesBaseName("secret-management-service")
version("")
}
dependencies {
compile fileTree(dir: "${projectDir}/libs", include: "*.jar")
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
I found it myself. I had missed the below block in my vault-client jar and also forgot to remove "main" class.
bootJar {
enabled = false
}

Spring Boot Multi Module Gradle Project classpath problem: Package Not Found, Symbol not Found

I have an spring boot gradle project etl with a dependency of common core classes in another project named common.
common/build.gradle
plugins {
id 'net.ltgt.apt' version '0.21'
id 'org.springframework.boot'
id 'io.spring.dependency-management'
id "io.freefair.lombok" version "5.1.0"
id 'java'
id 'idea'
id 'eclipse'
}
group = 'com.intelease'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
jcenter()
mavenCentral()
maven {
name "lightbend-maven-release"
url "https://repo.lightbend.com/lightbend/maven-releases"
}
maven {
name "Sonatype Snapshots"
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
ivy {
name "lightbend-ivy-release"
url "https://repo.lightbend.com/lightbend/ivy-releases"
layout "ivy"
}
}
dependencies {
implementation 'com.typesafe.play:play-java_2.13:2.7.3'
implementation 'com.typesafe.akka:akka-actor-typed_2.13:2.5.23'
implementation "com.typesafe.play:play-guice_2.12:2.6.15"
implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
implementation 'org.neo4j.driver:neo4j-java-driver:1.7.5'
implementation 'org.apache.poi:poi-ooxml:3.17'
implementation 'com.google.maps:google-maps-services:0.13.0'
implementation 'org.apache.commons:commons-text:1.8'
implementation 'com.google.cloud:google-cloud-language:1.19.0'
implementation 'com.google.cloud:google-cloud-vision:1.19.0'
implementation 'technology.tabula:tabula:1.0.2'
implementation 'com.amazonaws:aws-java-sdk:1.11.618'
implementation 'edu.stanford.nlp:stanford-corenlp:3.8.0'
implementation 'nz.ac.waikato.cms.weka:weka-stable:3.8.4'
// https://mvnrepository.com/artifact/com.typesafe.play/play-java
implementation 'org.joda:joda-money:0.11'
implementation 'com.feth:play-easymail_2.12:0.9.0'
implementation 'com.feth:play-authenticate_2.12:0.9.0'
implementation 'org.apache.commons:commons-imaging:1.0-alpha1'
implementation 'com.github.cloudyrock.mongock:mongock-core:3.3.2'
implementation 'com.github.cloudyrock.mongock:mongock-spring:3.3.2'
implementation 'net.logstash.logback:logstash-logback-encoder:5.3'
implementation 'ch.qos.logback:logback-core:1.2.3'
implementation 'ch.qos.logback:logback-classic:1.2.3'
implementation 'com.google.firebase:firebase-admin:6.13.0'
implementation 'org.mapstruct:mapstruct:1.3.1.Final'
implementation 'org.jgrapht:jgrapht-core:1.4.0'
implementation 'org.jgrapht:jgrapht-io:1.4.0'
implementation 'commons-io:commons-io:2.6'
implementation 'org.springframework.guice:spring-guice:1.1.4.RELEASE'
implementation 'com.google.inject:guice:4.2.3'
implementation 'commons-collections:commons-collections:3.2.2'
implementation 'org.aspectj:aspectjrt:1.9.5'
implementation 'org.ghost4j:ghost4j:1.0.1'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
testAnnotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final' // if you are using mapstruct in test code
testImplementation 'io.projectreactor:reactor-test:3.3.0.RELEASE'
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.2.0.RELEASE'
testImplementation 'cloud.localstack:localstack-utils:0.1.22'
testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.3'
testImplementation 'net.aichler:jupiter-interface:0.8.3'
testImplementation 'org.mockito:mockito-junit-jupiter:3.0.0'
testImplementation 'org.assertj:assertj-core:3.13.2'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.5.2'
}
etl/settings.gradle:
rootProject.name = 'etl'
includeFlat ('common')
etl/build.gradle:
plugins {
id 'org.springframework.boot' version '2.2.7.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'eclipse'
id 'idea'
id 'java'
}
group = 'com.intelease'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
jcenter()
maven {
name "lightbend-maven-release"
url "https://repo.lightbend.com/lightbend/maven-releases"
}
ivy {
name "lightbend-ivy-release"
url "https://repo.lightbend.com/lightbend/ivy-releases"
layout "ivy"
}
}
ext {
set('springCloudVersion', "Hoxton.SR4")
}
dependencies {
implementation project(':common')
implementation 'org.apache.kafka:kafka-streams'
implementation 'io.debezium:debezium-core:1.1.1.Final'
implementation 'org.springframework.cloud:spring-cloud-stream'
implementation 'org.springframework.cloud:spring-cloud-stream-binder-kafka-streams'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.cloud:spring-cloud-stream-test-support'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test {
useJUnitPlatform()
}
The etl project and common project are just side by side in the same level in file system.
The problem is that whenever I try to build the etl project which is dependent on common, It shows some problem regarding classes that I've expected to be brought and available from common project.
I've included the common into settings.gradle inside etl project with using includeFlat and Couldn't trace any issue with regard gradle multi module inclusion mechanisms.
Whats wrong with this flat multi module ecosystem?
the build script is ./gradlew clean build in the etl folder.
The problem was because of spring boot plugin being present in both etl and common projects. so I found out that in the cammon/build.gradle I have to add this piece of code:
...
bootJar {
enabled = false
}
jar {
enabled = true
}
...

AnnotationProcessor and dependencies

I'm using gradle / querydsl and JPA 2.1.
I would like to generate the querydsl metadata using APT (QEntities).
To do that I'm using the gradle-apt-plugin and gradle 4.7
In my project I configured the compileJava option using :
compileJava {
options.annotationProcessorGeneratedSourcesDirectory = file("$projectDir/src/generated2/java")
}
In my dependencies I added
compile 'org.springframework.boot:spring-boot-starter-data-jpa'"
annotationProcessor "com.querydsl:querydsl-apt:$querydslVersion:jpa"
The spring starter adds the org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final jar that contains javax.persistence.Entity class to the compileClasspath.
When launching the compileJava task I got the error :
caused by: java.lang.NoClassDefFoundError: javax/persistence/Entity at com.querydsl.apt.jpa.JPAAnnotationProcessor.createConfiguration(JPAAnnotationProcessor.java:37)
Don't understand why the annotation processor cannot load this class.
In case somebody else is searching. This is my full solution (based on your response). The important parts are the net.ltgt.apt* plugins to activate the code generation also in eclipse, and the last three querydsl dependencies.
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins { // activates the automatic code generation
id 'net.ltgt.apt' version '0.18'
id 'net.ltgt.apt-eclipse' version '0.18'
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
runtimeOnly 'com.h2database:h2'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.boot:spring-boot-starter-webflux'
// querydsl
annotationProcessor 'com.querydsl:querydsl-apt:4.1.3:jpa'
annotationProcessor 'org.springframework.boot:spring-boot-starter-data-jpa' // needed because the query dsl annotation processor doesn't recognize javax.persistence.Entity
compile 'com.querydsl:querydsl-jpa:4.1.3'
}

Resources