Gradle Spring Boot - spring jar versions are not automatically picked up - gradle

I am trying out a simple gradle spring boot application as per the below URL
https://spring.io/guides/gs/spring-boot/
This is my build.gradle file
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'my-jar'
version = '1.0.0'
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-web"
testCompile "junit:junit"
}
I am using a local artifactory and my init.gradle has the buildscript configuration which is below
buildscript {
repositories {
maven {
url 'http://mylocalartifactory:8081/'
}
}
dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE" }
}
I get the below error when i try to run gradlew build
What went wrong:
Could not resolve all dependencies for configuration ':compileClasspath'.
> Could not find org.springframework.boot:spring-boot-starter-web:.
Looks like the version is not getting applied for the dependency.
What I understand is that the version will be defaulted by the spring-boot plugin .
Am I missing something ?
It works fine if I mention the version number in the dependency
dependencies {
compile "org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE"
testCompile "junit:junit"
}
Though I can make it work , it beats the purpose of using spring boot if I need to manually specify the spring version jar dependency .
Kindly revert back if you see any issue in my build.gradle or init.gradle

Related

gradle does not fetch dependencies after upgrade from 5.1.1 to 6.4.1

I have several services that uses gradle 5.1.1 with java 8.
As we want to upgrade to Java 13, we first need to upgrade to gradle 6after doing so, some dependencies are not fetched.
Those dependencies are listed with compile() under a dependency which is our jar library and still built with gradle 5.1.1
our libraries are stored in a S3 bucket and we use shadowjar to generate the end jar.
so, for example:
I have project A which I want to upgrdae.
Project A has project B as a dependency (compile)
Project B has google guava as a dependency (also with compile)
Now, project A, that under gradle 5.1.1 had fetched guava with no problems, alerting me that it is missing guava after upgrading to gradle 6.
I use local computer installed gradle (not wrapper).
Here are the important build.gradle parts:
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
ext.ver = [
'springboot': '2.2.0.RELEASE',
'slf4j' : '1.7.12'
]
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${ver.springboot}"
classpath 'io.spring.gradle:dependency-management-plugin:1.0.7.BUILD-SNAPSHOT'
classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
classpath 'com.amazonaws:aws-java-sdk-core:1.11.5'
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'maven-publish'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
compileJava {
sourceCompatibility = JavaVersion.VERSION_1_8
}
configurations {
compile.exclude module: 'spring-boot-starter-logging'
testCompile.exclude module: 'spring-boot-starter-logging'
runtime.exclude module: 'spring-boot-starter-logging'
compile.exclude group: 'ch.qos.logback'
}
configurations.all {
resolutionStrategy.cacheDynamicVersionsFor 10, 'seconds'
resolutionStrategy.cacheChangingModulesFor 10, 'seconds'
}
dependencyManagement {
applyMavenExclusions = false
}
repositories {
mavenLocal()
maven {
url "s3://bucket"
credentials(AwsCredentials) {
accessKey = awsCredentials.AWSAccessKeyId
secretKey = awsCredentials.AWSSecretKey
}
metadataSources {
artifact()
}
}
mavenCentral()
}
dependencies {
compile("com.test:projectB:1.0.0")
...
}
import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer
shadowJar {
classifier = ''
baseName = 'project-A'
manifest {
attributes 'Main-Class': 'com.test.projectA.Starter'
}
mergeServiceFiles()
append 'META-INF/spring.handlers'
append 'META-INF/spring.schemas'
append 'META-INF/spring.tooling'
transform(PropertiesFileTransformer) {
paths = ['META-INF/spring.factories']
mergeStrategy = "append"
}
}
Could this be because project B was not built with new gradle?
unfortunately, I cannot create a real reproducer as those libraries are real code of the company I work at.
Thanks and Regards,
Ido
The metadataSources declaration of the s3 bucket Maven repository is most likely the root cause why transitive dependencies of projectB are not resolved. The documentation is quite a bit vague here, but I suspect artifact() looks for the actual jar file only and not for the POM file, hence transitive dependency resolution is not performed. You should be able to see this behavior when running the build with switches --info and --refresh-dependencies.
Thankfully, this is quite easy to fix. Add mavenPom() and Gradle will try to resolve the POM first and with that, dependency resolution should be back to normal.
And while you're at it, you might want to read the upgrading from Gradle 5 guide and get rid of the compile configuration in favor of implementation. You should be able to see a warning similar to this when running the build with --warning-mode all:
The compile configuration has been deprecated for dependency declaration. This will fail with an error in Gradle 7.0. Please use the implementation or api configuration instead. Consult the upgrading guide for further information: https://docs.gradle.org/6.4.1/userguide/upgrading_version_5.html#dependencies_should_no_longer_be_declared_using_the_compile_and_runtime_configurations

unable to create a executable spring boot jar by the bootJar task

basically I need to create a spring boot jar to run system spring-server service on my server
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
ext {
spring_boot_version = '1.5.7.RELEASE'
spring_version = '4.3.11.RELEASE'
}
repositories {
mavenLocal()
jcenter()
mavenCentral()
maven { url 'https://dl.bintray.com/kotlin/exposed' }
maven { url 'https://jitpack.io' }
maven { url 'https://ci-artifactory.corda.r3cev.com/artifactory/corda-releases' }
}
apply plugin: 'kotlin'
apply plugin: 'java'
apply plugin: 'net.corda.plugins.cordformation'
apply plugin: 'net.corda.plugins.quasar-utils'
bootJar {
basName = 'corda-webserver'
version = '1.0.0'
}
How can I achieve this is there something wrong with gradle dependencies or plugins?
I tried springboot task as well
So there's not enough information here to be sure of what the issue could be; so here are a few ideas.
Start by cleaning gradle cache. ./gradlew clean
Obviously the issue here is dependencies, you need to make sure gradle is able to build your project, and download dependencies.
./gradlew deployNodes (if you're using one of the corda sample projects) will build all of the files locally and should pull in the dependencies for you.
R3 now uses software.r3.com as the 'official' link to the artifact repository, you may want to take a look there and maybe look at whether the downloads are still good.
good luck!

spring boot jar missing project dependencies

I have a Spring Boot project that is built using Gradle. It has compile dependencies on other projects. All the projects are mentioned via "includeFlat" in a settings.gradle file in a master project.
My problem: the boot ("fat") jar that is generated by the build omits the project dependencies.
Here's the project structure:
master
(no source)
build.gradle - applies 'Eclipse' plugin (but not Java)
settings.gradle -- has 'includeFlat' for projectA, projectB
projectA
src/main/java/...
build.gradle
projectB
src/main/java/...
build.gradle -- see below
The build.gradle for projectB looks roughly like this:
buildscript
{
repositories
{
mavenCentral()
}
dependencies
{
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
jar
{
baseName = 'xxx'
version = '0.1.0'
}
repositories
{
mavenCentral()
}
jar {
enabled = true
}
dependencies
{
compile project(":projectA")
}
"gradle build" on projectB generates both a regular and a boot jar. I expect the boot jar to include the classes from ":projectA" -- but they are missing.
Other than that, both projects build and run properly, whether built individually or via the master project.

Building a Spring-boot project without eclipse gradle

I'm fairly new to gradle and writing a project that I have working in eclipse and was posed with the challenge to write it without eclipse using gradle. I'm finding that even once I add the spring framework configurations to my build file it still can not see what I am importing. I am also using maven so I think it my understanding of gradle changing from a maven project and with SQL. Any thoughts?
Here is my build.gradle:
plugins {
id "org.springframework.boot" version "1.5.9.RELEASE"
id "io.spring.dependency-management" version "1.0.4.RELEASE"
}
apply plugin: 'java'
repositories {
jcenter()
mavenCentral()
}
// spring dependency management plugin configuration
dependencyManagement {
imports {
// select versions based on this BOM
mavenBom 'io.spring.platform:platform-bom:1.1.1.RELEASE'
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework:spring-jdbc")
compile('mysql:mysql-connector-java:5.1.37')
}
You need to add spring boot dependencies.
like:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
}
See the Spring documentation:
https://spring.io/guides/gs/spring-boot/#scratch

Using jettyRunWar or jettyRun in Gradle with a Spring Boot app results in just a directory listing in the browser

I'm using Gradle and Spring Boot for the first time. I decided to create a project that actually builds a WAR archive and I'm using the Gradle Jetty plugin. If I run the jettyRun or jettyRunWar tasks, in my browser all I'm seeing is a directory listing, not my actual application.
For example, the jettyRunWar task results in a directory listing like this:
META-INF/
WEB-INF/
dist/
The dist/ directory contains my static files.
Maybe I'm missing something fundamental since I'm using Gradle and Spring Boot for the first time.
I'm trying to test my app while making changes to my static files without restarting the app. Here is my build.gradle file.
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M6"
}
}
apply plugin: "java"
apply plugin: "idea"
apply plugin: "spring-boot"
apply plugin: "war"
war {
baseName = "mis-support-client"
version = "1.0.0-SNAPSHOT"
includes = ["dist/**"]
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-snapshot" }
}
dependencies {
testCompile "junit:junit:4.11"
compile ("org.springframework.boot:spring-boot-starter-web:0.5.0.M7") {
exclude module: "spring-boot-starter-tomcat"
}
compile "org.springframework.boot:spring-boot-starter-jetty:0.5.0.M7"
compile "org.springframework.boot:spring-boot-starter-security:0.5.0.M7"
compile "org.springframework.boot:spring-boot-starter-websocket:0.5.0.M7"
compile "javax.inject:javax.inject:1"
compile "org.codehaus.jackson:jackson-mapper-asl:1.9.12"
compile "org.apache.httpcomponents:httpclient:4.3.1"
compile "commons-io:commons-io:2.4"
}
task wrapper (type: Wrapper) {
gradleVersion = "1.8"
}
Did you try ./gradlew bootRun instead? Normal Spring Boot project has server embedded for easier usage :)
This task requires gradle plugin:
apply plugin: 'spring-boot'
Features
Embed Tomcat or Jetty directly (no need to deploy WAR files)
Task
Execution tasks
---------------
bootRun - Run the executable JAR/WAR

Resources