Search code examples
spring-bootgradleintellij-ideaspring-cloud-configspring-vault

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()
}


Solution

  • 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
    }