Spring Boot get property of a package in Gradle - java

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!

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.

Related

org.springframework.boot Configuration with name 'runtime' not found

When trying to run my program using gradle bootRun, the error shows that
Failed to apply plugin 'org.springframework.boot'
Configuration with name 'runtime' not found
The following is my build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.3.RELEASE")
}
}
apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
jar {
baseName = 'blockchain-demo'
version = '0.0.1'
}
war {
baseName = 'blockchain-demo'
version = '0.0.1'
}
application {
mainClass = 'web.Application'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("junit:junit")
}
I assume you're using Gradle 7. In Gradle 7, the configurations compile, testCompile, runtime and testRuntime have been removed in favor of implementation, testImplementation, runtimeOnly and testRuntimeOnly. That's why Gradle issues
Failed to apply plugin 'org.springframework.boot' Configuration with name 'runtime' not found
To fix the issue, you should use the Gradle version that is supported by the Spring Boot Gradle Plugin version you're using (1.5.3, according to the snippet provided). The system requirements lists Gradle version 2 and 3 as requirement for Spring Boot 1.5.3. Everything thereafter is not supported.
Spring Boot 2.5.0 supports Gradle 6.8, 6.9, or 7.x
See here for a good reference on Gradle 7 configurations: https://docs.spring.io/spring-boot/docs/2.5.0/gradle-plugin/reference/htmlsingle/
This will get you off the ground:
plugins {
id 'org.springframework.boot' version '2.5.0'
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
}
repositories {
mavenCentral()
}

Gradle cannot resolve Spring Boot HATEOAS dependency

I created my skeleton project from http://start.spring.io . But when I build the application, Gradle cannot resolve the HATEOAS dependency. Here is the error I get:
Error:java: Illegal char <:> at index 78:
C:\Users\TempUser\Downloads\hateoas\Could not resolve
org.springframework.boot:spring-boot-starter-hateoas:2.0.4.RELEASE.
This is my build.gradle file:
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-hateoas:2.0.4.RELEASE')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
This declaration
compile('org.springframework.boot:spring-boot-starter-hateoas:2.0.4.RELEASE')
cause error. Change to
compile('org.springframework.boot:spring-boot-starter-hateoas')
This is what under the hood
You've already specified the spring boot components' version right here:
ext {
springBootVersion = '2.0.4.RELEASE'
}
Hence, all starter dependencies must be specified without the version value. Use:
compile('org.springframework.boot:spring-boot-starter-hateoas')
instead of
compile('org.springframework.boot:spring-boot-starter-hateoas:2.0.4.RELEASE')
Hope this helps

Testing with Gradle

Testing fails.
How fix exception?
Here is stacktrace and code:
Could not determine the dependencies of task ':test'.
Configuration with name 'default' not found.
My parent settings.gradle
rootProject.name = 'opti'
include 'web'
build.gradle
allprojects {
repositories {
mavenCentral()
}
group 'com.opti'
version '1.0'
apply plugin: 'java'
apply plugin: 'groovy'
dependencies {
testCompile 'org.codehaus.groovy:groovy-all:2.3.11',
'org.spockframework:spock-maven:0.7-groovy-2.0'
}
}
Tested module settings.gradle
rootProject.name = 'web'
include 'web'
build.gradle
group 'com.opti'
version '1.0'
apply plugin: 'groovy'
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile(project(':web'))
}
#Artur check that first:
https://docs.gradle.org/current/userguide/java_plugin.html
It seems that gradle could not find default configuration for web project.
You could also check if running this command helps
gradle :opti:test
or
gradle :web:test

springframework.security package not found with starter-security dependency?

I'm using spring boot and spring starter dependencies for my project.
I tried with spring starter security dependency in Gradle, but only security packages are not found in project. IDE is IntelliJ IDEA‎.
My build.gradle file :
buildscript {
ext {
springBootVersion = '1.2.7.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath('io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE')
classpath("org.springframework:springloaded:1.2.4.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'hashfon-spring'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-hateoas')
compile('org.springframework.boot:spring-boot-starter-jersey')
compile('org.springframework.boot:spring-boot-starter-mustache')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-security')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
All libraries except security can be found in External Libraries...
One example of class in project:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.*;
import org.springframework.hateoas.*;
import org.springframework.mock.*;
import org.springframework.data.*;
import org.springframework.security.*; //cannot resolve symbol!
/**
* I can import all packages from external libraries except security
*/
PS. I tried with a lot of different release versions of spring-security-core and nothing happens.
After dealing with this issue multiple times I came across following solution which works for me 100%:
Go to File | Invalidate Caches
Delete folder where all dependencies are stored (~/.gradle/ for UNIX systems) - if it exists after invalidation
Restart Intellij IDEA
Reimport gradle dependencies by refreshing gradle:

How to fix this gradle dependency resolution error?

I am new to gradle and I am trying to use it for an existing java project.
I have created settings.gradle file in my root directory:
include ':datamodel'
include ':active'
include ':client'
include ':zero'
include ':toall'
and build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
}
}
The build.gradle in datamodel looks like this:
apply plugin: 'java'
sourceSets {
main.java.srcDirs = ['src/main/java']
main.resources.srcDirs = ['src/main/java']
test.java.srcDirs = ['tests/java']
test.resources.srcDirs = ['tests/resources']
}
dependencies {
compile 'com.google.protobuf:protobuf-java:2.5.+'
}
When I run gradle build (version: 2.0) I get this failure
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all dependencies for configuration ':datamodel:compile'.
> Could not find any version that matches com.google.protobuf:protobuf-java:2.5.+.
Required by:
MyRoot:datamodel:unspecified
How can I fix this?
Add
repositories {
mavenCentral()
}
to your build script. Not as a part of buildscript closure where you can define where to look for dependencies required to execute your build script. You can do it in your top-level build.gradle using
allprojects {
repositories {
mavenCentral()
}
}
Try this
compile 'com.google.protobuf:protobuf-java:2.5.0'
More info here
The maven repo looks like
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
You can also try and add jcenter() to dependencies like this
dependencies {
mavenCentral()
jcenter()
}
which will allow to search not only in repo1.maven.org

Categories

Resources