Get dependency version introduced by Spring Boot starter [duplicate] - gradle

Im trying to convert my project from Maven build to Gradle. The project currently uses Spring Boot.
In my current maven config, I have
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
<version>${jackson.version}</version>
</dependency>
In the above snippet, the jackson.version property comes from Spring Boot pom. Now, in Gradle, i'm using the Spring Boot plugin and Im trying to use the below snippet of code..
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.4.RELEASE")
}}
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'java'
dependencies {
compile("com.fasterxml.jackson.datatype:jackson-datatype-hibernate4")
}
In above, I'm expecting the spring Boot plugin to insert the version of jackson-hibernate4 module. But, this doesnt happen
Any idea on how to achieve this? My intention is to use the same version of jackson builds across the project.
Thanks!

You can use the dependency management plugin to import Spring Boot's bom and get access to the properties that it specifies.
Here's you original build.gradle file with the necessary changes:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.2.4.RELEASE"
classpath "io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE"
}
}
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom 'org.springframework.boot:spring-boot-starter-parent:1.2.4.RELEASE'
}
}
ext {
jacksonVersion = dependencyManagement.importedProperties['jackson.version']
}
dependencies {
compile("com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:$jacksonVersion")
}
Spring Boot 1.3 will start using the dependency management plugin by default when it'll apply the plugin and import the bom for you.

Related

How to download all spring boot dependencies using gradle

I have searched extensively for the answer to this question. I have extensive knowledge of Maven, but am pretty new to Gradle. I am using Intellij 2019.2 Ultimate. I have my build.gradle set up like so:
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.6.RELEASE'
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation 'org.springframework.boot:spring-boot-dependencies:2.2.6.RELEASE'
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
options.compilerArgs << '-Xlint:unchecked'
options.deprecation = true
}
compileJava {
options.incremental = true
options.fork = true
options.failOnError = false
}
compileTestJava {
options.incremental = true
options.fork = true
options.failOnError = false
}
springBoot {
mainClassName = "com.app.StartMain"
}
bootWar {
manifest {
attributes 'Start-Class': 'com.app.StartMain'
}
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
I would have thought from this that Gradle would download ALL dependencies in the spring-boot-dependencies-2.2.6.pom file and place in my external dependencies tree in Intellij. But this is not happening. There are a TON of dependencies in that file and I just thought that is how it would behave. Am I missing something in my build file?
Thanks in advance!
The spring-boot-dependencies module is a BOM (bill-of-materials) that contains a curated list of module and library versions that are compatible with Spring Boot. But it only advises on which version to use and doesn't actually make your project depend on them. If you open the POM file for it (here is the one for Spring Boot 2.2.6), you will see that they are all in a <dependencyManagement> block. If they were declared as actual dependencies, they would be in a <dependencies> block directly under the project root. This behaviour is the same for both Maven and Gradle.
I don't think there is an "all" library where you get all those dependencies in your project. But it would also be bad practice as you are unlikely to need them all. Instead, you should use the Spring Boot Starters that makes sense for you.
And even though the current documentation in Spring Boot tells you differently, I would suggest you don't use the io.spring.dependency-management plugin as it configures dependencies in a way that is not standard in Gradle, which can lead to confusion. It was created at a time where Gradle didn't support importing BOM files natively, but it does now. Here is a way to declare dependencies without it:
dependencies {
implementation platform("org.springframework.boot:spring-boot-dependencies:2.2.6-RELEASE") // Import the BOM
implementation "org.springframework.boot"spring-boot-starter" // Use a Spring Boot starter (and not that there is no version defined)
}

Could not find org.springframework.boot:spring-boot-starter-velocity

I'm new to spring and trying to use velocity with spring boot.
Here is my build.gradle
repositories {
mavenCentral()
}
plugins {
id 'org.springframework.boot' version '2.0.4.RELEASE'
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-velocity')
runtime('org.springframework.boot:spring-boot-devtools')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
When I sync by ./gradlew bootRun, it returned error as below.
> Task :compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all files for configuration ':compileClasspath'.
> Could not find org.springframework.boot:spring-boot-starter-velocity:.
Most probably you forgot to include Spring's dependency management plugin.
apply plugin: 'io.spring.dependency-management'
Also make sure that you have specified the Spring Boot version to use:
plugins {
id 'org.springframework.boot' version '2.0.4.RELEASE'
}
See https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/gradle-plugin/reference/html/ for more information
Spring Boot 2.0 depends on Spring Framework 5.0. Which dropped support for Velocity. Hence in Spring Boot 2 there is no more support for Velocity.
If you really need Velocity you would have to stick with Spring Boot 1.5. If you can move to something like Freemarker or Mustache you are probably better of using that.

Building a Spring-boot project without eclipse gradle

I'm fairly new to gradle and writing a project that I have working in eclipse and was posed with the challenge to write it without eclipse using gradle. I'm finding that even once I add the spring framework configurations to my build file it still can not see what I am importing. I am also using maven so I think it my understanding of gradle changing from a maven project and with SQL. Any thoughts?
Here is my build.gradle:
plugins {
id "org.springframework.boot" version "1.5.9.RELEASE"
id "io.spring.dependency-management" version "1.0.4.RELEASE"
}
apply plugin: 'java'
repositories {
jcenter()
mavenCentral()
}
// spring dependency management plugin configuration
dependencyManagement {
imports {
// select versions based on this BOM
mavenBom 'io.spring.platform:platform-bom:1.1.1.RELEASE'
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework:spring-jdbc")
compile('mysql:mysql-connector-java:5.1.37')
}
You need to add spring boot dependencies.
like:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
}
See the Spring documentation:
https://spring.io/guides/gs/spring-boot/#scratch

Gradle Spring Boot - spring jar versions are not automatically picked up

I am trying out a simple gradle spring boot application as per the below URL
https://spring.io/guides/gs/spring-boot/
This is my build.gradle file
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'my-jar'
version = '1.0.0'
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-web"
testCompile "junit:junit"
}
I am using a local artifactory and my init.gradle has the buildscript configuration which is below
buildscript {
repositories {
maven {
url 'http://mylocalartifactory:8081/'
}
}
dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE" }
}
I get the below error when i try to run gradlew build
What went wrong:
Could not resolve all dependencies for configuration ':compileClasspath'.
> Could not find org.springframework.boot:spring-boot-starter-web:.
Looks like the version is not getting applied for the dependency.
What I understand is that the version will be defaulted by the spring-boot plugin .
Am I missing something ?
It works fine if I mention the version number in the dependency
dependencies {
compile "org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE"
testCompile "junit:junit"
}
Though I can make it work , it beats the purpose of using spring boot if I need to manually specify the spring version jar dependency .
Kindly revert back if you see any issue in my build.gradle or init.gradle

How are some gradle dependencies working with no version supplied

As far as I know gradle requires a version number when setting dependencies, but partial wildcards are allowed. For example if I want Guava, I cannot do this as it fails:
compile('com.google.guava:guava')
It has to be (as an example):
compile('com.google.guava:guava:21.0')
However, I'm learning Spring, which has the following:
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-web")
compile("com.fasterxml.jackson.core:jackson-databind")
How are these dependencies working with no version supplied?
Is it because of the following, but I thought these lines were required only for my plugin 'org.springframework.boot':
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE")
}
}
It is worth mentioning that the trick is called BOM (bill of materials) and the actual versions can be checked in the related POM file (in this example, it is for the version 2.7.0) inside spring-boot-dependencies package. This is mentioned in the Spring Boot official documentation here: Build Systems.
Another way that Spring provides this (for non Boot projects) is through Spring Platform BOM where it actually provides version for the following dependencies.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'io.spring.gradle:dependency-management-plugin:0.6.0.RELEASE'
}
}
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom 'io.spring.platform:platform-bom:Athens-SR2'
}
}
TL;DR - spring boot uses custom dependencies resolver.
A spring boot plugin that is applied with the following piece of code:
apply plugin: 'spring-boot'
handles the dependencies that are listed without version. This logic is implemented in this class which delegates it to here. DependencyManagementPluginFeatures are applied here.
The spring boot gradle plugin documentation states the following:
The version of the spring-boot gradle plugin that you declare
determines the version of the spring-boot-starter-parent bom that is
imported (this ensures that builds are always repeatable). You should
always set the version of the spring-boot gradle plugin to the actual
Spring Boot version that you wish to use.
Spring Boot Dependency Management Plugin is not necessary.
You may use build-in Gradle BOM support instead of Spring Boot Dependency Management Plugin
For example:
plugins {
id 'java'
id 'org.springframework.boot' version '2.1.0.RELEASE'
}
repositories {
jcenter()
}
dependencies {
implementation platform('org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE')
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
}
and for multi-module project:
in root build.gradle :
plugins {
id 'java-library'
id 'org.springframework.boot' version '2.1.0.RELEASE'
}
allprojects {
apply plugin: 'java-library'
repositories {
jcenter()
}
}
dependencies {
implementation project(':core')
implementation 'org.springframework.boot:spring-boot-starter-web'
}
and in core/build.gradle
dependencies {
api platform('org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE')
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Resources