How to import local external jar having spring cloud vault client dependency - spring-boot

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

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
}

Related

How to fix Could not find or load main class in Spring boot gradle.build?

Im new in gradle and i receive and error about my main class is Could not find or load main class i tried to fix it but doest work.
plugins {
id 'org.springframework.boot' version '2.6.9'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.notification'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2021.0.3")
}
dependencies {
implementation 'io.awspring.cloud:spring-cloud-aws-context:2.3.1'
implementation 'io.awspring.cloud:spring-cloud-aws-autoconfigure:2.3.1'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-stream'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
tasks.named('test') {
useJUnitPlatform()
}
springBoot {
mainClass = 'com.notification.NotificationServiceApplication'
}
Error
Error: Could not find or load main class com.notifcation.NotificationServiceApplication
Caused by: java.lang.ClassNotFoundException: com.notifcation.NotificationServiceApplication
Disregard this post question. I just restart my IDE

Use main Gradle project to define all dependencies versions

I want to create one main Gradle project which hosts all project dependencies:
Main Gradle project:
plugins {
id 'org.springframework.boot' version '2.6.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'maven-publish'
}
group = 'com.parent.dependencies'
version = '0.0.1'
sourceCompatibility = '11'
ext {
set('springCloudVersion', "2021.0.1")
artifactory_contextUrl = "http://192.168.1.112/repository"
}
repositories {
mavenCentral()
maven {
url "${artifactory_contextUrl}/plugins-release"
allowInsecureProtocol = true
}
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.boot:spring-boot-starter-validation'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.threeten:threetenbp:1.5.1'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// Lombok
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
testCompileOnly 'org.projectlombok:lombok:1.18.22'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'
// Swagger
implementation group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
implementation 'org.springframework.boot:spring-boot-starter-hateoas:2.6.3'
implementation group: 'com.netflix.hystrix', name: 'hystrix-core', version: '1.5.18'
implementation group: 'org.springframework.security', name: 'spring-security-core', version: '5.6.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
resolutionStrategy {
cacheDynamicVersionsFor 0, "seconds"
cacheChangingModulesFor 0, "seconds"
}
}
bootJar {
enabled = false
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
mavenLocal()
}
}
test {
useJUnitPlatform()
}
Sub project which will read all dependencies versions from parent Gradle project:
plugins {
id 'org.springframework.boot' version '2.6.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'maven-publish'
}
group = 'com.child'
version = '0.0.1'
sourceCompatibility = '11'
ext {
artifactory_contextUrl = "http://192.168.1.112/repository"
}
repositories {
mavenCentral()
maven {
url "${artifactory_contextUrl}/plugins-release"
allowInsecureProtocol = true
}
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.boot:spring-boot-starter-validation'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.threeten:threetenbp'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// Lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
// Swagger
implementation 'io.springfox:springfox-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'com.netflix.hystrix:hystrix-core'
implementation 'org.springframework.security:spring-security-core'
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
dependencyManagement {
imports {
mavenBom "com.parent:com-parent-dependencies:0.0.1"
}
}
bootJar {
enabled = false
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
mavenLocal()
}
}
test {
useJUnitPlatform()
}
When I try to compile the child project I get error:
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
> Could not find org.threeten:threetenbp:.
Required by:
project :
> Could not find io.springfox:springfox-boot-starter:.
Required by:
project :
> Could not find com.netflix.hystrix:hystrix-core:.
Required by:
project :
Do you know how I can define properly the parent Gradle project with all dependencies versions and use the child project without defining dependencies versions?
There’s a relatively new Gradle feature called “version catalogs”. With those you can centrally declare dependencies that you’d like to share between multiple projects of your build (or even between different builds).
In your concrete example, you’d add something like the following to your settings.gradle file:
dependencyResolutionManagement {
versionCatalogs {
libs {
library('spring-cloud-bom', 'org.springframework.cloud:spring-cloud-dependencies:2021.0.1')
library('spring-cloud-starter', 'org.springframework.cloud', 'spring-cloud-starter-netflix-eureka-client').withoutVersion()
library('spring-boot-starter-validation', 'org.springframework.boot', 'spring-boot-starter-validation').withoutVersion()
library('spring-boot-starter-test', 'org.springframework.boot', 'spring-boot-starter-test').withoutVersion()
library('threetenbp', 'org.threeten:threetenbp:1.5.1')
library('spring-boot-starter-actuator', 'org.springframework.boot', 'spring-boot-starter-actuator').withoutVersion()
library('lombok', 'org.projectlombok:lombok:1.18.22')
library('springfox-boot-starter', 'io.springfox:springfox-boot-starter:3.0.0')
library('spring-boot-starter-hateoas', 'org.springframework.boot:spring-boot-starter-hateoas:2.6.3')
library('hysterix-core', 'com.netflix.hystrix:hystrix-core:1.5.18')
library('spring-security-core', 'org.springframework.security:spring-security-core:5.6.1')
library('junit-jupiter-api', 'org.junit.jupiter:junit-jupiter-api:5.8.2')
library('junit-jupiter-engine', 'org.junit.jupiter:junit-jupiter-engine:5.8.2')
}
}
}
Then, in both build.gradle files, you’d replace your dependencies blocks with the following:
dependencies {
implementation libs.spring.cloud.starter
implementation libs.spring.boot.starter.validation
testImplementation libs.spring.boot.starter.test
implementation libs.threetenbp
implementation libs.spring.boot.starter.actuator
// Lombok
compileOnly libs.lombok
annotationProcessor libs.lombok
testCompileOnly libs.lombok
testAnnotationProcessor libs.lombok
// Swagger
implementation libs.springfox.boot.starter
implementation libs.spring.boot.starter.hateoas
implementation libs.hysterix.core
implementation libs.spring.security.core
testImplementation libs.junit.jupiter.api
testRuntimeOnly libs.junit.jupiter.engine
}
As you’re using a Maven BOM for Spring, you’ll also have to use the following updated dependencyManagement block in all projects:
dependencyManagement {
imports {
mavenBom libs.spring.cloud.bom.get().toString()
}
resolutionStrategy {
cacheDynamicVersionsFor 0, "seconds"
cacheChangingModulesFor 0, "seconds"
}
}
That should be it. I could successfully test your sample build configurations with my updates at least on Gradle 7.4.

Why subproject dependency jar includes all dependencies in it?

My project consists of multiple modules like core, my-backend, university, learning, etc.
Core is module which is required in all rest modules.
When I create build of my-backend module, I see core is being added with all dependencies in it which is making that jar too big. Seems some issue with my gradle file which is causing this problem.
Also somehow application.yml from core module is not being loaded from my-backend module. Any Idea why?
My gradle file is as follows:
buildscript {
ext {
springBootVersion = '2.2.1.RELEASE'
internalUrl = 'http://internal/repository'
}
repositories {
mavenLocal()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "http://repo.spring.io/release" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.3'
classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.15'
}
}
plugins {
id "io.freefair.lombok" version "3.8.4" apply false
id 'org.springframework.boot' version '2.2.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
}
subprojects {
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'application'
apply from: "$rootDir/dependencies.gradle"
mainClassName = "com.myk.MyApp"
group = 'com'
version = app.version
description = """"""
sourceCompatibility = 11
targetCompatibility = 11
ext {
set('springCloudVersion', "Hoxton.RC1")
}
repositories {
mavenLocal()
maven { url "http://repo.spring.io/libs-snapshot" }
jcenter()
mavenCentral()
maven { url "http://repo.maven.apache.org/maven2" }
maven { url "http://maven.aksw.org/repository/internal" }
maven { url "https://dl.bintray.com/michaelklishin/maven/" }
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
// additional plugins
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "io.freefair.lombok"
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-integration'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'org.springframework.cloud:spring-cloud-stream-test-support'
testImplementation (group: 'com.github.javafaker', name: 'javafaker', version: ver.'javafaker') {
exclude group: 'org.yaml', module: 'snakeyaml'
}
}
test {
useJUnitPlatform()
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
}
project(':core') {
dependencies {
implementation group: 'com.github.fommil.netlib', name: 'all', version: '1.1.2' // weka dependency
implementation 'org.springframework.boot:spring-boot-configuration-processor:2.2.1.RELEASE'
implementation group: 'com.google.firebase', name: 'firebase-admin', version: ver.'firebase-admin'
implementation group: 'com.google.apis', name: 'google-api-services-gmail', version: ver.'google-api-services-gmail'
compile "org.quartz-scheduler:quartz:${ver.quartz}"
}
}
[
':university',
':my-backend',
':learning'
].each {
project(it) {
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'io.spring.dependency-management'
mainClassName = "com.myk.MyApp"
springBoot {
buildInfo()
}
dependencies {
compile project(':core')
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testCompile group: 'io.github.swagger2markup', name: 'swagger2markup', version: ver.'swagger2markup'
}
}
}

Trying to modularize Gradle SpringBoot Project but getting "Error: cannot find symbol" when trying to build?

I had a working single-module Java project I am trying to break up into two modules: a library, and an application module. I pulled out the Java libraries from the top level build.gradle and stuffed them in the library build.gradle, and kept the SpringBoot/Docker/MySQL related dependencies in the application build file. Running ./gradlew build causes errors due to "cannot find symbol" errors on the lombok stuff in the App.
I added a top level settings.gradle (code below), pulled out all the Java libraries and put into the library/build.gradle (code below), and added a reference to library in the application/build.gradle (code below).
settings.gradle:
rootProject.name = 'order-routing'
include 'library'
include 'application'
library/build.gradle:
buildscript {
repositories { mavenCentral() }
}
plugins {
id "io.spring.dependency-management" version "1.0.5.RELEASE"
}
apply plugin: 'project-report'
apply plugin: 'java'
ext { springBootVersion = '2.1.6.RELEASE' }
jar {
baseName = 'order-routing-library'
version = '0.0.1-SNAPSHOT'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
compileOnly 'com.google.code.gson:gson:2.8.5'
runtimeOnly 'com.h2database:h2:1.4.197'
compile 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'com.google.cloud.sql:mysql-socket-factory-connector-j-8:1.0.14'
}
and application/build.gradle (notice I added "compile project(':library')") :
buildscript {
ext { springBootVersion = '2.1.6.RELEASE' }
repositories { mavenCentral() }
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath('se.transmode.gradle:gradle-docker:1.2')
}
}
plugins {
id "io.spring.dependency-management" version "1.0.5.RELEASE"
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
id "org.flywaydb.flyway" version "5.2.4"
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'project-report'
apply plugin: "maven"
apply plugin: 'docker'
repositories {
mavenCentral()
}
dependencies {
compile 'mysql:mysql-connector-java'
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile project(':library')
}
sourceCompatibility = 8
targetCompatibility = 8
compileJava.options.compilerArgs.add '-parameters'
compileTestJava.options.compilerArgs.add '-parameters'
configurations {
all*.exclude group: "org.hibernate", module: "hibernate-entitymanager"
all*.exclude group: "org.apache.tomcat", module: "tomcat-jdbc"
}
flyway {
url = 'jdbc:mysql://localhost/ordb'
user = 'flyway'
table = 'schema_version'
}
bootJar {
baseName = 'order-routing-application'
version = '0.0.1-SNAPSHOT'
mainClassName = 'com.pokemonmerch.orderrouting.OrderRoutingApplication'
}
Expected result: run './gradlew build' and see a successful build.
Actual: Getting "error: cannot find symbol" on a bunch of lombok generated methods
As I understood you're trying to use lombok in the application module, but you added it as a compileOnly dependency to the library module. So, because it's compileOnly it's not passed as a transitive dependency to the application module.
Please add lombok dependecies to the application module directly or change compileOnly to compile(what I don't recommend to do).
P.S. Don't forget to turn on Annotation Processing in your IDE.
Actually, what I suggest - it's to create build.gradle in the root folder (order-routing/build.gradle) and move common dependencies there, under the subprojects sections (https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:subproject_configuration)

AnnotationProcessor and dependencies

I'm using gradle / querydsl and JPA 2.1.
I would like to generate the querydsl metadata using APT (QEntities).
To do that I'm using the gradle-apt-plugin and gradle 4.7
In my project I configured the compileJava option using :
compileJava {
options.annotationProcessorGeneratedSourcesDirectory = file("$projectDir/src/generated2/java")
}
In my dependencies I added
compile 'org.springframework.boot:spring-boot-starter-data-jpa'"
annotationProcessor "com.querydsl:querydsl-apt:$querydslVersion:jpa"
The spring starter adds the org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final jar that contains javax.persistence.Entity class to the compileClasspath.
When launching the compileJava task I got the error :
caused by: java.lang.NoClassDefFoundError: javax/persistence/Entity at com.querydsl.apt.jpa.JPAAnnotationProcessor.createConfiguration(JPAAnnotationProcessor.java:37)
Don't understand why the annotation processor cannot load this class.
In case somebody else is searching. This is my full solution (based on your response). The important parts are the net.ltgt.apt* plugins to activate the code generation also in eclipse, and the last three querydsl dependencies.
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins { // activates the automatic code generation
id 'net.ltgt.apt' version '0.18'
id 'net.ltgt.apt-eclipse' version '0.18'
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
runtimeOnly 'com.h2database:h2'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.boot:spring-boot-starter-webflux'
// querydsl
annotationProcessor 'com.querydsl:querydsl-apt:4.1.3:jpa'
annotationProcessor 'org.springframework.boot:spring-boot-starter-data-jpa' // needed because the query dsl annotation processor doesn't recognize javax.persistence.Entity
compile 'com.querydsl:querydsl-jpa:4.1.3'
}

Resources