Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot get property of a package in 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!

like image 504
rakpan Avatar asked Jul 04 '15 15:07

rakpan


1 Answers

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.

like image 140
Andy Wilkinson Avatar answered Sep 28 '22 06:09

Andy Wilkinson