Spring boot Data JPA task :test Failed - java

I'm starting to use Data JPA from Spring to interact with Postgres Database, but when I try to build the project, the building phase raise an exception in the applicationTest.
This is my build.gradle:
plugins {
id 'org.springframework.boot' version '2.5.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'eu.universome.radio'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
//implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
If I comment the dependencies:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' the build will be successful.
This is my empty applicationTest:
package eu.universome.radio.RadioUVMServer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
#SpringBootTest
class RadioUvmServerApplicationTests {
#Test
void contextLoads() {
}
}
Here there is the Console Error:
Gradle Distribution: Gradle wrapper from target build
Gradle Version: 7.1.1
Java Home: /usr/lib/jvm/java-11-openjdk-amd64
JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: build
> Task :compileJava
> Task :processResources UP-TO-DATE
> Task :classes
> Task :bootJarMainClassName
> Task :bootJar
> Task :jar UP-TO-DATE
> Task :assemble
> Task :compileTestJava
> Task :processTestResources UP-TO-DATE
> Task :testClasses
> Task :test FAILED
RadioUvmServerApplicationTests > contextLoads() FAILED
java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at ConstructorResolver.java:800
Caused by: org.springframework.beans.factory.BeanCreationException at ConstructorResolver.java:658
Caused by: org.springframework.beans.BeanInstantiationException at SimpleInstantiationStrategy.java:185
Caused by: java.lang.IllegalStateException at Assert.java:97
1 test completed, 1 failed
Here you can find the report:
[1]: https://www.universome.eu/wp-content/uploads/RadioUVM/progetto-radio-unime/reports/tests/test/classes/eu.universome.radio.RadioUVMServer.RadioUvmServerApplicationTests.html

Not sure if you have the dependency added.
implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.23'
You can check if the driver is on classpath:
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
// recheck dependencies
}

Solved adding the line
runtimeOnly 'org.postgresql:postgresql'
on build.gradle file in section dependencies
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
//implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Related

Setting up Spring Native Experimental (id 'org.springframework.experimental.aot' version '0.10.3' was not found in any of the following sources)

Hello ladies and gentlemen,
so i was just trying to get a executable for my Spring application by using Spring Native.
My build.gradle:
plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
id 'org.springframework.experimental.aot' version '0.10.3'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.5.4'
implementation 'org.springframework.boot:spring-boot-starter-web:2.5.4'
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.4'
implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.14'
implementation group: 'org.apache.poi', name: 'poi', version: '4.1.2'
implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.14.6'
implementation group: 'org.json', name: 'json', version: '20180813'
}
test {
useJUnitPlatform()
}
and my settings.gradle:
rootProject.name = 'demo'
after adding the 'org.springframework.experimental.aot' version '0.10.3' plugin to the build.gradle as seen above (doing whats told in the documentary under 2.1.2 https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/#getting-started), i get following error:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'org.springframework.experimental.aot', version: '0.10.3'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.springframework.experimental.aot:org.springframework.experimental.aot.gradle.plugin:0.10.3')
Searched in the following repositories:
Gradle Central Plugin Repository
The person who commented is right, though I think unnecessarily rude given that the tutorial is out of order, and they tell you to apply the plugin prior to giving you instructions how to install.
More specifically, you need to add the spring release maven repo to your settings.gradle.kts in order to source the plugin
pluginManagement {
repositories {
gradlePluginPortal()
maven { url = uri("https://repo.spring.io/release") }
mavenLocal()
}
}
and then also add the repository to your build.gradle.kts in order to source the dependencies
repositories {
maven { url = uri("https://repo.spring.io/release") }
}

spring clound eureka server dependencies error gradle intellij

Am not able to add the spring-cloud-netflix-eureka-server as dependencies in gradle project (IDE Intellij)
please find my build.gradle file below.
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'war'
}
group = 'com.lti.mod.services'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation('io.jsonwebtoken:jjwt:0.9.1')
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.liquibase:liquibase-core'
/*compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'*/
compile('org.springframework.cloud:spring-cloud-netflix-eureka-server:2.1.5.RELEASE')
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.security:spring-security-test'
}
test {
useJUnitPlatform()
}
Please find the gradle build messages below.
> Configure project : The compile configuration has been deprecated for dependency declaration. This will fail with an error in Gradle 7.0. Please use the implementation 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
at build_5fewyf5k4gqf0wzmewce4q3ow$_run_closure3.doCall(D:\AngularCourse\core-services\build.gradle:30)
(Run with --stacktrace to get the full stack trace of this deprecation warning.)
compile configuration is deprecated in the Gradle 7.0 as your error message says. Please use implementation instead.
Check the docs - https://docs.gradle.org/current/userguide/java_plugin.html#tab:configurations

How to run cucumber + springboot application using Gradle in Eclipse

I have a demo springboot application and trying to run the cucumber test cases using gradle .. In maven Its working fine but unable to run using gradle.
Can you please help me with the way to execute the gradle file either from eclipse or from command line
Below is mt build.gradle file
testImplementation 'io.cucumber:cucumber-junit:5.3.0'
testImplementation 'io.cucumber:cucumber-core:5.3.0'
testImplementation 'io.cucumber:cucumber-spring:5.3.0'
testImplementation 'io.cucumber:cucumber-java:5.3.0'
testImplementation 'javax.xml.bind:jaxb-api:2.2.8'
testImplementation 'org.mockito:mockito-core:1.9.5'
testImplementation 'junit:junit:4.12'
testImplementation('org.springframework.boot:spring-boot-starter-test'){
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation ('org.springframework.boot:spring-boot-starter-actuator'){
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-log4j2'
}
test{
testLogging.showStandardStreams = true
}
sourceSets {
main.java.srcDir "src/main/java"
main.resources.srcDir "src/main/resources"
test.java.srcDir "src/test/java/com/b**/f**/w**/tests"
test.resources.srcDir "src/test/resources"
}
configurations {
cucumberRuntime {
extendsFrom testImplementation
}
}
task cucumber() {
dependsOn assemble, compileTestJava , testClasses
doLast {
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--plugin', 'pretty', '--glue', 'com.b**.f**.w**.tests', 'src/test/resources']
}
}
}
}
And below is the error when i execute in command line using "Gradle cucumber"
> Task :cucumber FAILED
Feb 27, 2020 9:53:29 AM cucumber.api.cli.Main run
WARNING: You are using deprecated Main class. Please use io.cucumber.core.cli.Main
Feb 27, 2020 9:53:29 AM io.cucumber.core.cli.Main run
WARNING: By default Cucumber is running in --non-strict mode.
This default will change to --strict and --non-strict will be removed.
You can use --strict to suppress this warning
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/alekhya.machiraju/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.12.1/14973e22497adaf0196d481fb99c5dc2a0b58d41/log4j-slf4j-impl-2.12.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/alekhya.machiraju/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-log4j12/1.7.30/c21f55139d8141d2231214fb1feaf50a1edca95e/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/alekhya.machiraju/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.3/7c4f3c474fb2c041d8028740440937705ebb473a/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Feb 27, 2020 9:53:30 AM io.cucumber.core.runtime.Runtime run
SEVERE: Exception while executing pickle
java.util.concurrent.ExecutionException: java.lang.NullPointerException
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at io.cucumber.core.runtime.Runtime.run(Runtime.java:108)
at io.cucumber.core.cli.Main.run(Main.java:75)
at cucumber.api.cli.Main.run(Main.java:28)
at cucumber.api.cli.Main.main(Main.java:15)
Caused by: java.lang.NullPointerException
at io.cucumber.spring.SpringFactory.stop(SpringFactory.java:171)
at io.cucumber.core.runner.Runner.disposeBackendWorlds(Runner.java:173)
at io.cucumber.core.runner.Runner.runPickle(Runner.java:69)
at io.cucumber.core.runtime.Runtime.lambda$run$2(Runtime.java:100)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at io.cucumber.core.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:243)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at io.cucumber.core.runtime.Runtime.lambda$run$3(Runtime.java:100)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.SliceOps$1$1.accept(SliceOps.java:204)
at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1359)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:499)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:486)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at io.cucumber.core.runtime.Runtime.run(Runtime.java:101)
... 3 more
Feb 27, 2020 9:53:30 AM io.cucumber.core.runtime.Runtime run
SEVERE: Exception while executing pickle
java.util.concurrent.ExecutionException: java.lang.NullPointerException
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at io.cucumber.core.runtime.Runtime.run(Runtime.java:108)
at io.cucumber.core.cli.Main.run(Main.java:75)
at cucumber.api.cli.Main.run(Main.java:28)
at cucumber.api.cli.Main.main(Main.java:15)
Caused by: java.lang.NullPointerException
at io.cucumber.spring.SpringFactory.stop(SpringFactory.java:171)
at io.cucumber.core.runner.Runner.disposeBackendWorlds(Runner.java:173)
at io.cucumber.core.runner.Runner.runPickle(Runner.java:69)
at io.cucumber.core.runtime.Runtime.lambda$run$2(Runtime.java:100)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at io.cucumber.core.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:243)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at io.cucumber.core.runtime.Runtime.lambda$run$3(Runtime.java:100)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.SliceOps$1$1.accept(SliceOps.java:204)
at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1359)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:499)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:486)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at io.cucumber.core.runtime.Runtime.run(Runtime.java:101)
... 3 more
Exception in thread "main" io.cucumber.core.exception.CompositeCucumberException: There were 2 exceptions:
java.lang.NullPointerException(null)
java.lang.NullPointerException(null)
at io.cucumber.core.runtime.Runtime.run(Runtime.java:120)
at io.cucumber.core.cli.Main.run(Main.java:75)
at cucumber.api.cli.Main.run(Main.java:28)
at cucumber.api.cli.Main.main(Main.java:15)
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\alekhya.machiraju\eclipse-workspace\bProjet name\build.gradle' line: 133
* What went wrong:
Execution failed for task ':cucumber'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_241\bin\java.exe'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.2.1/userguide/command_line_interface.html#sec:command_line_warnings
My Folder stricuture is as below
src/test/java
---- com.b**.f**.w**.tests
----------------Cucumberrunnertest.java
----------------Stepdefinition.java
src/test/reaources
----------------cucumber.feature
I had a similar problem as you, but Gradle wouldn't run my Junit 5 + Cucumber tests. It seams like Cucumber doesn't have any Junit 5 support, so I had to revert back to Junit 4 (also known as Junit 5 vintage).
My trick was to enable legacy Junit-support in my build.gradle.
test {
// Use old (Junit 4) runner since Cucumber doesn't support Junit 5 *sigh*
useJUnit()
// useJUnitPlatform()
}
The reset of my code/setup:
build.gradle
plugins {
id 'org.springframework.boot' version '2.2.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'se.snorbu.cucumber'
version = '0.0.1-SNAPSHOT'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// Spring
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
// Cucumber
testCompile 'io.cucumber:cucumber-java:6.6.0'
testCompile 'io.cucumber:cucumber-junit:6.6.0'
testCompile 'io.cucumber:cucumber-spring:6.6.0'
testCompile 'io.cucumber:cucumber-junit-platform-engine:6.6.0'
// Misc
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
// Junit5
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
// Use old (Junit 4) runner since Cucumber doesn't support Junit 5 *sigh*
useJUnit()
}
The Cucumber-runner/-context
package com.example.webapp;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import io.cucumber.spring.CucumberContextConfiguration;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
#CucumberContextConfiguration
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {"pretty", "junit:build/cucumber-junit.xml", "html:build/cucumber.html"},
features = "classpath:/"
)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
#AutoConfigureMockMvc
public class ContextConfiguration {
}
My stepdef
package com.example.webapp.steps;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.cucumber.junit.platform.engine.Cucumber;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import se.snorbu.cucumber.webapp.PizzaClient;
import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
#Slf4j
#Cucumber
public class OrderPizzaSteps {
#Autowired
private MockMvc mockMvc;
private ResultActions mvcResult;
#Given("A pizza")
public void givenAPizza() {
// Do setup
}
#When("I order a pizza")
public void whenOrderAPizza() throws Exception {
// Do something
mvcResult = mockMvc.perform(
post("/order")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"pizza\": \"pepperoni\"}")
);
}
#Then("I should receive an order-id")
public void thenShouldReceiveOrderid() throws Exception {
// Do assertion
mvcResult
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("id", is("A12")));
}
}

springboot 1.5.2 + hibernate-search-orm

I want to use springboot and hibernate-search-orm but can't get the project to compile.
I think it is because of compatability issues between hibernate versions?
I am using Gradle and Intellij Idea.
build.gradle
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
ext["thymeleaf.version"] = "3.0.2.RELEASE"
ext["thymeleaf-layout-dialect.version"] = "2.0.1"
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
version = '0.0.5-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
compile 'org.springframework.boot:spring-boot-starter-security'
compile ("org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.0.RELEASE")
compile group: 'org.springframework.boot', name: 'spring-boot-starter-logging', version: '1.5.2.RELEASE'
compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity4', version: '2.1.2.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: '1.5.2.RELEASE'
compile group: 'org.hibernate', name: 'hibernate-search-orm',
version: '5.1.0.Final'
compile("org.springframework.boot:spring-boot-devtools")
compile('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
bootRun {
addResources = true
}
And the output when I run build
5:40:30 PM: Executing external task 'build'...
C:\Users\Owner\IdeaProjects\test\src\main\java\com\example\controllers\planController.java:27: warning: sym is internal proprietary API and may be removed in a future release
import static com.sun.org.apache.xalan.internal.xsltc.compiler.sym.error;
^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 warning
:compileJava
:processResources UP-TO-DATE
:classes
:findMainClass
:jar
:bootRepackage
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
com.example.Demo3ApplicationTests > contextLoads FAILED
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: java.lang.AbstractMethodError
1 test completed, 1 failed
:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///C:/Users/Owner/IdeaProjects/test/build/reports/tests/test/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 23.392 secs
There were failing tests. See the report at: file:///C:/Users/Owner/IdeaProjects/test/build/reports/tests/test/index.html
5:40:54 PM: External task execution finished 'build'.
I don't think hibernate-search-orm is part of springboot so I have added it to my build.gradle file explicitly.
If I try to run my project the root error appears to be:
Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
You need to Specify the versions of dependencies that you are using. And they should be compatible to each other.Go through https://mvnrepository.com and find your spring boot version and check Compile Dependencies. Do it with all other dependencies that you are using .

how to properly configure gradle build to avoid including log4j and slf4j from the resulting jar?

I'm having an issue with my spring-boot application: I'm able to run it within Eclipse, but unable to run the jar file (built with gradle).
I run the following command to build my project:
gradle buid
The build is successful:
gradle build
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:findMainClass
:jar
:bootRepackage
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 1.214 secs
This is a fragment of the resulting jar file:
jar -tvf build/libs/springboot-receiver-api-0.1.0.jar | grep log
9988 Mon Apr 04 20:37:48 CDT 2016 BOOT-INF/lib/slf4j-log4j12-1.7.21.jar
2308 Wed Sep 21 07:11:50 CDT 2016 BOOT-INF/lib/spring-boot-starter-logging-1.4.1.RELEASE.jar
66802 Thu May 28 09:49:34 CDT 2015 BOOT-INF/lib/jboss-logging-3.3.0.Final.jar
304075 Tue Mar 29 22:24:50 CDT 2016 BOOT-INF/lib/logback-classic-1.1.7.jar
23646 Mon Apr 04 20:39:02 CDT 2016 BOOT-INF/lib/log4j-over-slf4j-1.7.21.jar
470782 Tue Mar 29 22:23:42 CDT 2016 BOOT-INF/lib/logback-core-1.1.7.jar
489884 Sun May 06 13:24:48 CDT 2012 BOOT-INF/lib/log4j-1.2.17.jar
when I attempt to run this jar file, I get this error:
java -jar build/libs/springboot-receiver-api-0.1.0.jar
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/eugene/.Trash/springboot-receiverapi/build/libs/springboot-receiver-api-0.1.0.jar!/BOOT-INF/lib/slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/eugene/.Trash/springboot-receiverapi/build/libs/springboot-receiver-api-0.1.0.jar!/BOOT-INF/lib/logback-classic-1.1.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError.
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58)
Caused by: java.lang.ExceptionInInitializerError
at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72)
Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
at org.slf4j.impl.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:54)
... 19 more
Here is my build.gradle file:
apply plugin: 'java'
apply plugin: 'maven'
group = 'uptake'
version = '0.0.1-snapshot'
description = """Spring-Boot-ReceiverAPI"""
sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
compile(group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.2.3.RELEASE') {
exclude(module: 'log4j-over-slf4j')
}
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version:'1.2.3.RELEASE'
compile group: 'org.postgresql', name: 'postgresql', version:'9.3-1102-jdbc41'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate4', version:'2.8.1'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version:'2.8.3'
compile group: 'org.apache.kafka', name: 'kafka_2.10', version:'0.9.0.0'
compile group: 'org.springframework.integration', name: 'spring-integration-kafka', version:'2.0.1.RELEASE'
compile group: 'org.apache.zookeeper', name: 'zookeeper', version:'3.4.5'
compile(group: 'commons-beanutils', name: 'commons-beanutils', version:'1.9.2') {
exclude(module: 'commons-logging')
}
compile group: 'org.json', name: 'json', version:'20090211'
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version:'1.5.0'
compile(group: 'org.springframework', name: 'spring-core', version:'4.3.3.RELEASE') {
exclude(module: 'commons-logging')
}
compile group: 'com.spotify', name: 'docker-maven-plugin', version:'0.4.13'
}
configurations.all {
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j"
}
What can I do, to avoid this clash of logging libraries, and to be able to run my jar file stand-alone?
Couple things, it seems to me that your build.gradle should declare :
spring boot gradle plugin
apply spring boot plugin
let spring boot manage most of your dependency versions
This script works for me:
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'spring-boot'
group = 'uptake'
version = '0.0.1-snapshot'
description = """Spring-Boot-ReceiverAPI"""
sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE'
}
}
repositories {
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter'
compile 'org.springframework.boot:spring-boot-starter-logging'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.postgresql:postgresql'
compile 'com.fasterxml.jackson.datatype:jackson-datatype-hibernate4'
compile 'com.fasterxml.jackson.core:jackson-core'
compile 'org.apache.kafka:kafka_2.10:0.9.0.0'
compile 'org.springframework.integration:spring-integration-kafka:2.0.1.RELEASE'
compile 'org.apache.zookeeper:zookeeper:3.4.5'
compile 'commons-beanutils:commons-beanutils'
compile 'org.json:json'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.5.0'
compile 'org.springframework:spring-core'
compile 'com.spotify:docker-maven-plugin:0.4.13'
}
configurations.all {
exclude module: 'slf4j-log4j12'
exclude module: 'jms'
exclude module: 'jmxtools'
exclude module: 'jmxri'
}
Now, if you check both modules with gradle dependencyInsight, only log4j-over-slf4j will be found:
$ gradle dependencyInsight --dependency slf4j-log4j12
$ No dependencies matching given input were found...
$ gradle dependencyInsight --dependency log4j-over-slf4j
:dependencyInsight
org.slf4j:log4j-over-slf4j:1.7.21 (selected by rule)
\--- org.springframework.boot:spring-boot-starter-logging:1.4.1.RELEASE
+--- compile
\--- org.springframework.boot:spring-boot-starter:1.4.1.RELEASE
+--- compile
+--- org.springframework.boot:spring-boot-starter-web:1.4.1.RELEASE
| \--- compile
+--- org.springframework.boot:spring-boot-starter-data-jpa:1.4.1.RELEASE
| \--- compile
+--- org.springframework.boot:spring-boot-starter-aop:1.4.1.RELEASE
| \--- org.springframework.boot:spring-boot-starter-data-jpa:1.4.1.RELEASE (*)
\--- org.springframework.boot:spring-boot-starter-jdbc:1.4.1.RELEASE
\--- org.springframework.boot:spring-boot-starter-data-jpa:1.4.1.RELEASE (*)
I solved this problem updating kafka to a new version.
Some old versions of Kafka had dependecies with jmxtools and jmxri (from log4j < 1.2.16). And this dependecies doesn't have available licences in maven and gradle.
You can resolve this doing one of this three options:
Setting kafka in a major version(2.12 works well!)
Setting log4j in a version upper to 1.2.16
Excluding jmxtools and jmxri
Like I previously said, I resolved this setting the kafka version in my build.gradle:
compile group: 'org.apache.kafka', name: 'kafka_2.12', version: '2.4.0'

Categories

Resources