How to exclude dependency from Plugin in gradle? - spring-boot

I am using springboot version 2.4.6 and gradle version 6.3. This springboot downloads commons-compress 1.20 and I want to exclude this particular version of commons-compress. I want to have commons-compress 1.21 for security reasons. I have enforced this particular version by using :
implementation('org.apache.commons:commons-compress') {
version {
strictly '1.21'
}
}
However when I do gradle build, it shows me both 1.20 as well as 1.21. So how do I exclude commons-compress 1.20 in the Plugin?
This is how my code snippet:
buildscript {
ext {
springBootVersion = "2.4.6"
junitVersion = "5.7.2"
awsVersion = "1.11.728"
lombokVersion = "1.18.12"
resilience4jVersion = "1.7.0"
logbackJsonVersion = "0.1.5"
}
}
plugins {
id "checkstyle"
id "com.github.spotbugs" version "4.4.4"
id "idea"
id "io.spring.dependency-management" version "1.0.11.RELEASE"
id "jacoco"
id "java"
id "org.springframework.boot" version "2.4.6" --> this is the culprit
id "pmd"
}
dependencies {
implementation('org.apache.commons:commons-compress') {
version {
strictly '1.21'
}
}
.....
}
springBoot {
buildInfo {
properties {
additional = ['deployTag': System.getenv('DEPLOY_TAG') ?: '']
}
}
}

One cannot exclude any dependency from a Gradle plugin, but only upgrade the plugin.
Version 2.5.4 might not have it and Gradle is currently also rather version 7.1.1.

Related

Project unable to find gradle shadow plugin

I have a project that builds a lib. I want to make a fat jar for packaging.
I followed instructions in the gradle-shadow-plugin docs
Here is my build.gradle
buildscript {
repositories {
jcenter()
mavenCentral()
gradlePluginPortal()
maven {
url = "https://packages.confluent.io/maven"
}
}
dependencies {
classpath 'gradle.plugin.com.github.jengelman.gradle.plugins:shadow:7.1.2'
}
}
plugins {
id 'java' // so that we can use 'implementation', 'testImplementation' for dependencies
}
apply plugin: 'com.github.johnrengelman.shadow'
repositories {
jcenter()
mavenCentral()
maven {
url = "https://packages.confluent.io/maven"
}
maven {
url = "https://jitpack.io"
}
}
dependencies {
// Kafka
implementation group: 'org.apache.kafka', name: 'connect-api', version: '3.3.1'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.1'
implementation 'com.github.jcustenborder.kafka.connect:connect-utils:0.7.173'
implementation 'com.github.jcustenborder.kafka.connect:kafka-connect-transform-common:0.1.0.14'
//test
testImplementation(platform('org.junit:junit-bom:5.9.0'))
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation("io.mockk:mockk:1.9.2")
}
sourceSets {
main {
java {
srcDirs = ["src/main/java"]
}
resources {
srcDirs = ["src/main/avro", "src/main/resources"]
}
}
test {
java {
srcDirs = ["src/test/java"]
}
}
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
plugins.withId("com.github.johnrengelman.shadow"){
//this block requires the java plugin to be applied first.
plugins.withId("java"){
shadowJar {
//We are overriding the default jar to be the shadow jar
classifier = null
exclude 'META-INF'
exclude 'META-INF/*.INF'
exclude 'META-INF/license/*'
}
jar {
manifest {
attributes(
'Built-By' : System.properties['user.name'],
'Build-Timestamp': new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()),
'Created-By' : "Gradle ${gradle.gradleVersion}",
'Build-Jdk' : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
'Build-OS' : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
)
}
}
tasks.build.dependsOn tasks.shadowJar
tasks.shadowJar.mustRunAfter tasks.jar
}
}
When I do a .gradlew clean build I am getting the following error.
* What went wrong:
A problem occurred configuring root project 'remove-json-value'.
> Could not resolve all files for configuration ':classpath'.
> Could not find gradle.plugin.com.github.jengelman.gradle.plugins:shadow:7.1.2.
Searched in the following locations:
- https://jcenter.bintray.com/gradle/plugin/com/github/jengelman/gradle/plugins/shadow/7.1.2/shadow-7.1.2.pom
- https://repo.maven.apache.org/maven2/gradle/plugin/com/github/jengelman/gradle/plugins/shadow/7.1.2/shadow-7.1.2.pom
- https://plugins.gradle.org/m2/gradle/plugin/com/github/jengelman/gradle/plugins/shadow/7.1.2/shadow-7.1.2.pom
- https://packages.confluent.io/maven/gradle/plugin/com/github/jengelman/gradle/plugins/shadow/7.1.2/shadow-7.1.2.pom
Required by:
project :
I search SO and found this answer. However, adding jcenter to the buildscript -> repositories section does not seem to solve it.
This is my environment:
$ ./gradlew -version
------------------------------------------------------------
Gradle 7.6
------------------------------------------------------------
Build time: 2022-11-25 13:35:10 UTC
Revision: daece9dbc5b79370cc8e4fd6fe4b2cd400e150a8
Kotlin: 1.7.10
Groovy: 3.0.13
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 1.8.0_181 (Oracle Corporation 25.181-b13)
OS: Mac OS X 10.16 x86_64
Any ideas what I am doing wrong?
At the time of writing this answer, the shadow plugin's latest version is 2.0.3, not 7.1.2.
Try the following:
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.3'
}
The 7.0.x version refers to a different "shadow" dependency, the 2.0.x belongs to the one you specified.
This is a bug in the current quickstart docs.
Details here
The correct classpath that works is:
classpath 'gradle.plugin.com.github.johnrengelman:shadow:7.1.2'

Gradle dependency resolution issue

I am converting a project from Maven to Gradle.
Here is my gradle.build file
plugins {
id 'java'
id 'maven-publish'
id "org.jetbrains.kotlin.jvm" version "1.5.30"
id "org.jetbrains.kotlin.plugin.spring" version "1.5.30"
id 'org.springframework.boot' version "2.5.4"
id 'io.spring.dependency-management' version "1.0.11.RELEASE"
}
group = 'com.aeonai.lib'
version = '1.0.0'
description = 'Aeon AI Library'
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
maven {
url = uri 'https://repo.osgeo.org/repository/release/'
}
maven {
url = uri 'https://repo.maven.apache.org/maven2/'
}
}
dependencies {
...
implementation 'org.geotools:gt-cql:25.2:sources#jar'
implementation 'org.geotools:gt-epsg-hsql:25.2:sources#jar'
implementation 'org.geotools:gt-geojson:25.2:sources#jar'
implementation 'org.geotools:gt-main:25.2:sources#jar'
implementation 'org.geotools:gt-opengis:25.2:sources#jar'
implementation 'org.geotools:gt-shapefile:25.2:sources#jar'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
...
}
configurations {
all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
}
bootRun.enabled = false
bootJar {
mainClass.set('NONE')
}
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
test {
useJUnitPlatform()
}
I am importing many of the geotools from https://repo.osgeo.org/repository/release/.
When I run gradle build all of my dependency show up as they should, but the build still fails. The compiler cannot find some of the dependencies (which I have added in a screenshot below), although I can see the files in the External Libraries tree (also in a screenshot below). I can also see the libraries in the ~/.gradle/caches.
Here is a screenshot of the files not being found.
Here is another screenshot of the files in the tree.
Why is gradle/compiler not recognizing the file is there? What else am I missing?

Page Not Found when get /info from gradle-git-properties/micronaut-management

Context: I downloaded an initialized project from https://micronaut.io/launch/ and I added both gradle-git-properties plugin and micronaut-management dependency in order to expose git.properties as explained in adding commit info guide
I checked my project buil/resources/main and I see this git.properties
git.branch=
git.build.host=SPANOT149
git.build.user.email=jimis.drpc#gmail.com
git.build.user.name=Jimis.drpc
git.build.version=0.1
git.closest.tag.commit.count=
git.closest.tag.name=
git.commit.id=
git.commit.id.abbrev=
git.commit.id.describe=
git.commit.message.full=
git.commit.message.short=
git.commit.time=
git.commit.user.email=
git.commit.user.name=
git.dirty=true
git.remote.origin.url=
git.tags=
git.total.commit.count=0
So I assume the plugin is working properly.
Nevertheless, when I get http://localhost:8080/info the result is
{"message":"Page Not Found","_links":{"self":{"href":"/info","templated":false}}}
The closest tutorial beyond the above official guidance I found is this quick guide using Micronaut version 1.0.3 and with few extra steps in Maven. Note I am using Micronaut 2.1.3 and Gradle and, the oficial guidance uses Gradle also and hasn't such few extra steps.
Here are my:
build.gradle
plugins {
id "org.jetbrains.kotlin.jvm" version "1.4.10"
id "org.jetbrains.kotlin.kapt" version "1.4.10"
id "org.jetbrains.kotlin.plugin.allopen" version "1.4.10"
id "com.github.johnrengelman.shadow" version "6.1.0"
id "io.micronaut.application" version '1.0.5'
id "com.gorylenko.gradle-git-properties" version "2.2.2"
}
version "0.1"
group "com.mybank"
repositories {
mavenCentral()
jcenter()
}
micronaut {
runtime "netty"
testRuntime "junit5"
processing {
incremental true
annotations "com.mybank.*"
}
}
dependencies {
implementation("io.micronaut:micronaut-validation")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
implementation("io.micronaut:micronaut-runtime")
implementation("javax.annotation:javax.annotation-api")
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut:micronaut-management")
runtimeOnly("ch.qos.logback:logback-classic")
runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
}
mainClassName = "com.mybank.ApplicationKt"
java {
sourceCompatibility = JavaVersion.toVersion('11')
}
compileKotlin {
kotlinOptions {
jvmTarget = '11'
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = '11'
}
}
application.yml
micronaut:
application:
name: demo
endpoints:
info:
enabled: true
sensitive: false
gradle.properties
micronautVersion=2.1.3
kotlinVersion=1.4.10
As a final goal I want to use micronaut-management to expose some built-in management and monitoring endpoints.
See the project at https://github.com/jeffbrown/jimcinfoendpoint.
I copied your build config into that project and the /info endpoint appears to work.

Spring-boot project, using Gradle, coded in Kotlin, auto-generate OpenAPI kotlin classes

Problem: Generated sources are not picked up by gradle as part of the sources, thus importing the generated interfaces fails.
Question: how to add the generated sources so that gradle(with goovy dsl and kotlin plugin) picks them up?
setup:
spring-boot -> kotlin programming language
gradle -> groovy
I can generate the kotlin classes with OpenAPI Generator Gradle Plugin with no problems.
I am using the option "interfaceOnly", so it only creates interfaces. That means I do need to implement those.
I am using the typical gradle project setup. Now I have no idea where to place the generated files so that gradle(and intellj) pick them up.
openAPI.yaml:
openapi: 3.0.1
info:
version: 0.1.0
title: Villagechatter Calendar
description: Villagechatter calendar microservice
tags:
- name: external
description: external API
- name: internal
description: Internal API for other microservices
paths:
/hello:
get:
responses:
200:
description: Ok
content:
text/plain:
schema:
type: string
description: demo
example: Hello World!
build.gradle:
id("org.springframework.boot") version "2.3.1.RELEASE"
id("io.spring.dependency-management") version "1.0.9.RELEASE"
id "org.jetbrains.kotlin.jvm" version "1.3.72"
id "org.openapi.generator" version "4.3.1"
}
apply plugin: 'kotlin'
group = "com.example"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
compileKotlin {
kotlinOptions {
freeCompilerArgs = ["-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
openApiGenerate {
inputSpec = "$rootDir/specs/openApi.yaml"
generatorName = "kotlin-spring"
//setting package names and interface only with a configFile, should not matter for the problem
outputDir = "$buildDir/generated-src"
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude group : "org.junit.vintage"
exclude module : "junit-vintage-engine"
}
}
what I have tried:
sourceSets {
main {
kotlin.srcDirs += "PATH-TO-GENERATED-FILES"
}
}
Also tried creating a new sourceSet and adding it to the compileKotlin task
The google results I found were all about gradle and the kotlin dsl, sorry if this is a duplicate, I have not found the solution.
Are you trying to auto generate them and put use them as sources in the project? Why not generate them into the project directory directly?
outputDir = "$projectDir"
I managed to solve like this:
sourceSets {
main {
java {
srcDir("$buildDir/generated-src")
}
}
}

Two versions of module javafx.base in gradle/JavaFX project

My team is working on a java project. (git repo # https://github.com/RaiderRobotix/Scouting-Client-FX). We are trying to package it using jlink. The following is displayed when running the jlink task (gradlew jlink). I'm using gradle 6.1.1, with jdk 11 on macOS. If you'd like to see our module-info.java, please check the repo. I didn't want to make this question too lengthy.
BTW, I have multiple JDK's on my machine (8,11). Building the project works, but running it with gradlew run does not (I think its an unrelated issue with lombok).
Full error: Error: Two versions of module javafx.base found in ./build/jlinkbase/jlinkjars (Infinite Recharge Client-v4.0.0-alpha.jar and javafx-base-11-mac.jar)
build.gradle
plugins {
id 'java'
id 'idea'
id "org.openjfx.javafxplugin" version "0.0.8"
id 'org.beryx.jlink' version '2.17.2'
id "com.github.johnrengelman.shadow" version "5.2.0"
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'com.github.gmazzo.buildconfig' version '1.6.2'
}
group = "com.github.RaiderRobotix"
version = "v4.0.0-alpha"
repositories {
maven { url 'https://jitpack.io' }
jcenter()
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
jar {
manifest {
attributes (
'Implementation-Title': 'Raider Robotix Scouting Client',
'Implementation-Version': project.version,
'Main-Class': 'org.usfirst.frc.team25.scouting.client.ui.Main'
)
}
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}
dependencies {
implementation 'com.github.RaiderRobotix:Scouting-Models:29617b7dcc'
implementation 'com.github.RaiderRobotix:blue-alliance-api-java-library:3.0.0'
implementation 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'
implementation 'com.google.code.gson:gson:2.+'
implementation 'commons-io:commons-io:2.+'
implementation 'org.apache.commons:commons-math3:3.+'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.61"
}
buildConfig {
packageName "com.raiderrobotix"
buildConfigField 'String', 'TBA_API_KEY', project['TBA_API_KEY']
// The escaped quotes here are NEEDED. The plugin copies the TEXT LITERAL given to it. The quotes are part of this.
buildConfigField 'String', 'VERSION', "\"$version\""
}
javafx {
version = "11"
modules = [
'javafx.base',
'javafx.graphics',
'javafx.controls',
'javafx.fxml',
]
}
jlink {
launcher {
name = 'Scouting Client'
}
}
application {
mainClassName = 'org.raiderrobotix.scouting.client/org.raiderrobotix.scouting.client.ui.Main'
}
wrapper {
gradleVersion = '6.1.1'
}
compileKotlin {
kotlinOptions {
jvmTarget = "11"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "11"
}
}
I had a similar issue, my specific error during jlink was:
Error: Two versions of module javafx.base found in C:\Users\tareh\code\cleopetra\build\jlinkbase\jlinkjars (javafx-base-11.0.2-win.jar and javafx-base-11.0.2-linux.jar)
Execution failed for task ':jlink'.
I got some inspiration from https://github.com/openjfx/javafx-gradle-plugin/issues/65 and changed one of my dependencies in build.gradle:
dependencies {
// Get rid of this
// compile group: 'org.controlsfx', name: 'controlsfx', version: '11.0.3'
// Use this instead
implementation('org.controlsfx:controlsfx:11.0.3') {
exclude group: 'org.openjfx'
}
}
After I did that, the jlink error went away, and also I noticed the linux jars which had been listed in my IntelliJ module dependencies had disappeared (I'm on Windows).
This doesn't address your specific problem, but hopefully this will be helpful to others.
In my case I had to comment jar block in build.gradle to perform jlink command. I guess that problem is related to how plugin 'org.beryx.jlink' resolves modules when jar block is appear.

Resources