Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: Access version declaration in spring boot dependency-management plugin

I have spring boot project and I try to use spring boot dependency-management plugin to be able to use provided dependency versions.

The plugin 'simulates' mavens BOM behaviour, which means that it somehow retrieves versions of libs from maven parent project (I'm not sure how exatly this gets acheived, but generally versions are taken from pom.xml). It does have jackson.version property that is used to setup versions for artifacts within com.fasterxml.jackson.dataformat group.

My project uses artifact from the same group, however it is not included into the BOM (jackson-dataformat-yaml) but I want to use the same jackson version.

I tried adding compile dependency like this:

compile "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${jackson.version}"

but build fails with:

Could not get unknown property 'jackson' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Q: Is there a way to access the property? Or how else can I reuse the lib version?

UPDATE I'm not sure why didn't I try this from the very beginning, but it works:

compile "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml"

However I have no clue why this works (the artifact is not declared anywhere).

like image 458
Sasha Shpota Avatar asked Oct 25 '17 13:10

Sasha Shpota


People also ask

Can Gradle be used for dependency management in spring boot?

The Spring Boot Gradle Plugin provides Spring Boot support in Gradle. It allows you to package executable jar or war archives, run Spring Boot applications, and use the dependency management provided by spring-boot-dependencies .

How do I set the Gradle plugin version?

You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or update your Gradle version using the command line. The preferred way is to use the Gradle Wrapper command line tool, which updates the gradlew scripts.

How do I find my Gradle version?

In Android Studio, go to File > Project Structure. Then select the "project" tab on the left. Your Gradle version will be displayed here.


2 Answers

After question is updated with working example, I'll try to answer why it works without specifying the version instead:

The pom.xml you are referring to contains the dependency

<dependency>
    <groupId>com.fasterxml.jackson</groupId>
    <artifactId>jackson-bom</artifactId>
    <version>${jackson.version}</version>
    <scope>import</scope>
    <type>pom</type>
</dependency>

... which refers to the following pom.xml in the jackson-bom project, which in turn contains the following dependency:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-yaml</artifactId>
    <version>${jackson.version.dataformat}</version>
</dependency>

... which in turn specifies the version of the jackson-dataformat-yaml artifact...

like image 159
Per Huss Avatar answered Oct 12 '22 22:10

Per Huss


However I have no clue why this works (the artifact is not declared anywhere).

So it is a concern that you are not using any version, but how it works?

Actually gradle use some rules. Those are given below:

  1. Gradle will use transitive dependency management for specifying actual version.

  2. On the other hand it will use first level or second level hierarchy of repositories to take the latest version.

  3. From spring documentation,

Maven’s dependency management includes the concept of a bill-of-materials (bom). A bom is a special kind of pom that is used to control the versions of a project’s dependencies and provides a central place to define and update those versions.

For more you can go through this tutorial: Gradle dependencies with jars that have no version number

  1. The spring-boot-gradle-plugin is also available and provides tasks to create executable jars and run projects from source. It also provides dependency management that, among other capabilities, allows you to omit the version number for any dependencies that are managed by Spring Boot:
plugins {
    id 'org.springframework.boot' version '1.5.8.RELEASE'
    id 'java'
}


repositories {
    jcenter()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

Resource Link:

  1. https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html

  2. How are some gradle dependencies working with no version supplied

like image 24
SkyWalker Avatar answered Oct 12 '22 23:10

SkyWalker