Is there a simple way to use a different Spring version? - spring

I want to use latest Spring 4.1.x snapshot in my Spring Boot project.
Is there a simple way to override the version of all Spring dependencies, or should I include all required Spring dependencies with it's desired version manually?
Reason is I want experiment with Spring 4.1 #JsonView annotation in REST services.

If you're using Maven with spring-boot-starter-parent as the parent, you can override the spring.version property in your pom to change the version of Spring that you're using:
<properties>
<spring.version>4.1.0.BUILD-SNAPSHOT</spring.version>
</properties>
If you're using Gradle, you can achieve the same effect by using a resolution strategy to override the version of everything with the org.springframework group id:
configurations.all {
resolutionStrategy {
eachDependency {
if (it.requested.group == 'org.springframework') {
it.useVersion '4.1.0.BUILD-SNAPSHOT'
}
}
}
}

I once again needed this and previous block doesn't work anymore, causing already dependencies to be failed.
Anyway this works:
configurations.all {
resolutionStrategy {
eachDependency { DependencyResolveDetails details ->
if (details.requested.group == "org.springframework") {
details.useVersion "4.1.0.RC1"
}
}
}
}

Given that you're using Spring Boot, I'm guessing that you must be using either Maven or Gradle, but you don't say which. For Maven, you have a couple of things you might be able to do.
First, you could have a go at just forcing the Spring version in your pom.xml:
<properties>
<spring.version>4.1.0.BUILD-SNAPSHOT</spring.version>
</properties>
That ought to override the property which is defined in the spring-boot-dependencies project.
For more fine-grained control, use dependency management:
<dependencyManagement>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencyManagement>

Related

Dependency table for spring boot 2.4.3

I have a difficulty defining what dependencies versions are compatible with each other.
In my case i have this
I don't know for the spring boot 2.4.3 what versions of the dependencies below go with it or the cloud version to 2020.0.1.
How can i fix this for example i want to migrate the spring boot and spring cloud to its latest versions.
It is a good rule of thumb to not define the versions yourself but use BOMs and let them define the versions for you:
spring-boot-dependencies
spring-cloud-dependencies
In order to find out which BOMs to use, you can use this compatibility matrix or the endpoint that #spencergibb mentioned: https://start.spring.io/actuator/info.
If you want to use a Spring Project that is not in the BOM, most probably that Spring Project is not supported (e.g.: Netflix libraries by latest Spring Cloud as #spencergibb mentioned).
Update: here's a Gradle example but you can generate a whole project using Spring Initializer:
plugins {
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2020.0.1'
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j'
}
It might be because there are collisions between dependencies included from the start and your explicitly defined dependencies. For example, take this dependency: spring-cloud-contract
Your version: 2.1.5
2020.0.1 Spring cloud's supported version: 3.0.1 (check this link mentioned by #Jonatan)
Try to exclude built-in dependencies that you have explicitly defined. For Maven it would be:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.1</version>
<type>pom</type>
<scope>import</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-verifier</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
But that might not work because not all libraries are backward compatible. I would suggest using default libraries provided by spring cloud and remove explicit dependencies.

Can I pull the version for the Gradle org.springframework.boot plugin from my Gradle platform definition

I really like the Gradle java-platform feature. I've created my own platform that bundles spring-boot-dependencies along with other things. Now I have (shortened for clarity):
plugins {
id 'org.springframework.boot' version '2.4.1'
}
dependencies {
implementation platform("my-group:my-base-bom:1.0.0")
}
And I'd like the spring boot plugin version to automatically adjust to match the version of spring-boot-dependencies that is bundled in my platform (so if the platform went to SB 2.5.0 then plugin would do the same without my needing to change the build.gradle.
I can't figure out how to do it though without resorting to external variables. Is it possible?
Not possible. Currently, there are (3) ways to define versions for plugins:
In the Gradle file directly:
// build.gradle.kts
plugins {
id("org.springframework.boot") version "2.4.1"
}
In the plugins dependencies spec:
// settings.gradle.kts
pluginManagement {
plugins {
id("org.springframework.boot") version "2.4.1"
}
}
or with a resolution rule:
// settings.gradle.kts
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "org.springframework.boot") {
useVersion("2.4.1")
}
}
}
}
All of which do not accept a platform, only a single version variable.
Another way I tested, but ultimately did not work was utilizing the buildscript:
// build.gradle.kts
buildscript {
dependencies {
classpath(platform("io.mateo.sample:platform-bom:1.0.0-SNAPSHOT"))
classpath("org.springframework.boot:spring-boot-gradle-plugin")
}
}
As mentioned at the start, it's not possible.
Your custom platform can provide an opinion about which version of the Spring Boot Gradle plugin you want clients to use (especially since it's not included in the spring-boot-dependencies BOM).
Here are the relevant parts of an example platform's build.gradle.kts file, for example:
plugins {
`java-platform`
}
javaPlatform {
allowDependencies()
}
dependencies {
// This platform extends the Spring Boot platform.
api(platform("org.springframework.boot:spring-boot-dependencies:2.7.6"))
constraints {
// Provide an opinion about which version of the Spring Boot Gradle plugin clients
// should use since it's not included in the standard spring-boot-dependencies BOM.
api("org.springframework.boot:spring-boot-gradle-plugin:2.7.6")
}
}
That will generate a BOM that aligns both spring-boot-gradle-plugin and spring-boot-dependencies:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-gradle-plugin</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Your client projects can then just depend upon your platform and inherit its opinion about the Spring Boot version using something like this:
buildSrc/build.gradle.kts:
// Pull in the version of the Spring Boot Gradle plugin specified by your
// platform, making it available to your regular build script.
dependencies {
implementation(enforcedPlatform("my-group:my-base-bom:1.0.0"))
implementation("org.springframework.boot:spring-boot-gradle-plugin")
}
build.gradle.kts:
plugins {
id("org.springframework.boot") // version inherited from your platform
}
dependencies {
// It's necessary to specify it for each configuration.
implementation(enforcedPlatform("my-group:my-base-bom:1.0.0"))
// Pull in any normal Spring Boot-managed dependencies you need (versions come from platform).
implementation("org.springframework.boot:spring-boot-starter-web")
}
Of course, you could also use Gradle version catalogs to centralize versions, which are inlined for clarity in the examples.

Publish BOM (as pom.xml) using gradle plugin java-platform

I am setting up a project specific BOM that will "inherit" definitions from other BOMs (available as pom.xml) and also define own managed dependendies.
I tried the following (as stated in the java-platform docs) in my build.gradle.kts:
plugins {
`java-platform`
`maven-publish`
}
dependencies {
constraints {
api(platform("org.camunda.bpm:camunda-bom:${Versions.camunda}"))
}
}
publishing {
publications {
create<MavenPublication>("camunda-bom") {
from(components["javaPlatform"])
}
}
}
But when I do gradle publishToMavenLocal and check the resulting pom.xml in .m2/repositories it looks like:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.camunda.bpm</groupId>
<artifactId>camunda-bom</artifactId>
<version>7.10.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Which will not work because the syntax for importing poms should be
...
<type>pom</type>
<scope>import</scope>
...
How can I publish a valid BOM as pom.xml with gradle (using version 5.3.1)?
You are defining the BOM as a constraint, but that is most likely not what you want to do.
A constraint on a platform will just say that if that dependency enters the graph elsewhere it should use the platform part of it and the version recommendation from the constraint.
If you expect that constraints of that BOM to be visible to the consumers of your platform, then you need to add the BOM as a platform dependency by doing something like:
javaPlatform {
allowDependencies()
}
dependencies {
api(platform("org.camunda.bpm:camunda-bom:${Versions.camunda}"))
}
Then this will be properly published as an inlined BOM in Maven.

How to use a maven BOM for Spring in Gradle?

I am converting POM to Gradle and one of the things I am stuck at is having dependency management in Gradle like the following that I have in POM:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Is there a way to have Edgware.SR4 in Gradle as well?
I checked https://docs.gradle.org/4.6/release-notes.html#bom-import but that doesn't really tell me a way on how to utilize Edgware.SR4 BOM.
UPDATE
I finally have my build.gradle as follows that seems to work:
plugins{
id 'org.springframework.boot' version '1.5.8.RELEASE'
}
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4'
}
}
This seems to be working fine but wondering if there is any flaw in this approach. Documentation available at https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/ suggests to use apply false to begin with in
id 'org.springframework.boot' version '1.5.8.RELEASE'
I didn't do that and it worked fine. Wondering why it was suggested like that.
Assuming that you are using Spring Boot and, therefore, already have the Dependency Management Plugin applied, you can import Spring Cloud's bom by adding the following to your build.gradle file:
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4'
}
}
As of today, the latest versions of gradle have a built-in solution.
dependencies {
implementation(platform("org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4"))
}

How to set a version on a variable out of dependencies tag with Gradle?

I want to set the version of my dependencies in just one place in my Gradle (app/build.gradle) configuration file , to became easier to change in case of a update.
the problem is:
dependencies {
implementation 'com.google.android.gms:play-services-maps:12.0.0'
implementation 'com.google.android.gms:play-services-auth:12.0.0'
implementation 'com.google.firebase:firebase-core:12.0.0'
implementation 'com.google.firebase:firebase-auth:12.0.0'
implementation 'com.google.firebase:firebase-database:12.0.0'
implementation 'com.google.firebase:firebase-storage:12.0.0'
implementation 'com.google.firebase:firebase-messaging:12.0.0'
implementation 'com.google.android.gms:play-services-ads:12.0.0'
implementation 'com.github.bumptech.glide:glide:4.5.0'
}
Can you see that, i'm repiting the same version many times and this make slow and unprodutive to change all version to the next version.
Like in Maven i could just do like this:
<properties>
<org.springframework.version>5.0.8.RELEASE</org.springframework.version>
</properties>
After set the version, I just add like this:
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies>
The last piece of Maven configuration did set the version in this parte:
${org.springframework.version}
How can I do the same in my gradle configuration?
the assumption, that they'd all would have the same version is categorically wrong ...
def glideVersion = "4.5.0"
dependencies {
implementation "com.google.android.gms:play-services-base:16.0.1"
implementation "com.google.android.gms:play-services-auth:16.0.1"
implementation "com.google.android.gms:play-services-maps:16.0.0"
implementation "com.google.android.gms:play-services-ads:16.0.0"
implementation "com.google.firebase:firebase-core:16.0.4"
implementation "com.google.firebase:firebase-auth:16.0.4"
implementation "com.google.firebase:firebase-database:16.0.3"
implementation "com.google.firebase:firebase-storage:16.0.3"
implementation "com.google.firebase:firebase-messaging:17.3.4"
implementation "com.github.bumptech.glide:glide:${glideVersion}"
}
one can also set project.ext properties with version numbers - or load them from external files.
ext {
glideVersion = "4.5.0"
...
}
and then use it with ${rootProject.ext.glideVersion} or ${project.ext.glideVersion}.
in general, it's not easier to change... just another way of organizing it.

Resources