Spring Web projects multi module - spring

Regarding modern multi-module constructions.
Can many Spring Web projects be imported into one another?
- Project 1 ==> have some endpoints
- Project 2 ==> have some other
endpoints
I tried, but only Project 1's endpoints are exposed.
and the other project 2 endpoints provide the following error
The origin server did not find a current representation for the target resource or is not
willing to disclose that one exists.
project 1 dependencies
api 'org.springframework.boot:spring-boot-starter-security'
api 'org.springframework.boot:spring-boot-starter-actuator'
api("io.springfox:springfox-swagger2:3.0.0") {
exclude module: "mapstruct"
}
compileOnly 'javax.servlet:servlet-api:2.5'
api 'javax.persistence:javax.persistence-api:2.2'
api 'org.springframework.boot:spring-boot-starter-data-jpa'
api 'com.cmeza:spring-data-generator:1.1.9'
api 'org.springframework.boot:spring-boot-starter-web'
api 'com.slack.api:slack-api-client:1.24.0'
api 'com.google.guava:guava:31.1-jre'
api 'io.jsonwebtoken:jjwt:0.9.1'
api 'org.apache.commons:commons-lang3:3.12.0'
api 'javax.validation:validation-api:2.0.1.Final'
api 'io.jsonwebtoken:jjwt:0.9.1'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
project 2 dependencies
implementation project(":project1")
implementation 'javax.persistence:javax.persistence-api:2.2'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.cmeza:spring-data-generator:1.1.9'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('javax.xml.bind:jaxb-api:2.3.1')
implementation('org.javassist:javassist:3.23.1-GA')
implementation 'org.mapstruct:mapstruct:1.5.2.Final'
implementation 'com.opencsv:opencsv:5.6'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.2.Final'

I was expecting this will include project 2 packages as it's the main runner
#ComponentScan(basePackages="project1.*")
but, regrettably, it doesn't.
so it should be
#ComponentScan(basePackages="project1.*","project2.*")

Related

Spock tests failing with org.junit.runners.model.InvalidTestClassError:

After upgrading to spock-core 2.0 , my tests are failing with below error:
org.junit.runners.model.InvalidTestClassError: Invalid test class 'com.spock.test.TestSpockSpecification':
1. No runnable methods
at org.junit.runners.ParentRunner.validate(ParentRunner.java:525)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:102)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:84)
at org.junit.runners.JUnit4.<init>(JUnit4.java:23)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:125)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:111)
I am using below configuration in my build.gradle file :
testImplementation platform("org.spockframework:spock-bom:2.0-M5-groovy-3.0") //copied from spock example
testImplementation "org.spockframework:spock-core:2.0-M5-groovy-3.0"
implementation localGroovy() //3.0.7 as I am using gradle 7
I am running these test with junit 4 ,Can someone please help
Spock 2.0 builds on top of JUnit 5 and does not work with legacy JUnit 4. Also, it has been out for some time now, and there are no reasons to use milestones.
See this for a full example. Pay attention to the useJUnitPlatform part. E.g.:
dependencies {
testImplementation platform("org.codehaus.groovy:groovy-all:3.0.9")
testImplementation platform("org.spockframework:spock-bom:2.0-groovy-3.0")
testImplementation "org.spockframework:spock-core"
testRuntimeOnly "net.bytebuddy:byte-buddy:1.11.18" // allows mocking of classes (in addition to interfaces)
testRuntimeOnly "org.objenesis:objenesis:3.2" // allows mocking of classes without default constructor (together with ByteBuddy or CGLIB)
}
test {
useJUnitPlatform()
}

Unable to fetch gradle dependency net.devh:grpc-server-spring-boot-starter

I am using grpc springboot. I can download all dependencies apart from 'net.devh:grpc-server-spring-boot-starter:2.12.0.RELEASE'.
Due to the I can't use annotation #GrpcService
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation 'io.grpc:grpc-netty-shaded:1.37.0'
implementation 'io.grpc:grpc-protobuf:1.37.0'
implementation 'io.grpc:grpc-stub:1.37.0'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
implementation group: 'commons-io', name: 'commons-io', version: '2.6'
implementation 'org.springframework.boot:spring-boot-starter:2.4.5'
implementation 'net.devh:grpc-server-spring-boot-starter:2.12.0.RELEASE'
compileOnly 'jakarta.annotation:jakarta.annotation-api:1.3.5'
}
Any help much appreciated
Just try the install dependencies and reimport them.
If it doesn't work, close the IDE and open it again.

TestEngine with ID 'junit-vintage' failed to discover tests - JUnitException: Failed to parse version of junit:junit: 4.13.2

I'm having a very weird issue with my Gradle config and JUnit, I have tried updating to 5.7+ but I'm getting the following exceptions:
org/junit/jupiter/api/extension/ScriptEvaluationException
java.lang.NoClassDefFoundError: org/junit/jupiter/api/extension/ScriptEvaluationException
My Gradle version is 6.8.3 by the way. On the other hand, Here is the exception that I got with the current config below, I have tried a lot of combinations of versions and libraries, hut seems that none of them work:
Caused by: org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-vintage' failed to discover tests
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:111)
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:85)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:92)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:99)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
... 25 more
Caused by: org.junit.platform.commons.JUnitException: Failed to parse version of junit:junit: 4.13.2
at org.junit.vintage.engine.JUnit4VersionCheck.parseVersion(JUnit4VersionCheck.java:54)
at org.junit.vintage.engine.JUnit4VersionCheck.checkSupported(JUnit4VersionCheck.java:37)
at org.junit.vintage.engine.JUnit4VersionCheck.checkSupported(JUnit4VersionCheck.java:32)
at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:62)
plugins {
id 'org.springframework.boot' version '2.4.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'com.palantir.docker' version '0.26.0'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
docker {
name "{project.name}"
dockerfile file('src/docker/Dockerfile')
copySpec.from( jar ).rename(".*", "crm_lead_validator.jar")
buildArgs( ['JAR_FILE':"crm_lead_validator.jar"] )
}
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-webflux'
implementation 'org.jeasy:easy-random-core:5.0.0'
implementation 'io.github.openfeign:feign-jackson:9.3.1'
implementation 'com.github.tomakehurst:wiremock:2.25.1'
implementation 'org.mock-server:mockserver-netty:3.10.8'
implementation 'org.mock-server:mockserver-client-java:3.10.8'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.12.3'
implementation 'com.fasterxml.jackson.core:jackson-core:2.12.3'
implementation 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
implementation 'io.github.openfeign:feign-okhttp:10.11'
implementation 'io.github.openfeign:feign-slf4j:10.11'
implementation 'io.github.openfeign:feign-gson:10.11'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
// https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher
testImplementation group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.7.2'
testImplementation "org.junit.jupiter:junit-jupiter-engine:5.6.2"
testImplementation "org.junit.vintage:junit-vintage-engine:5.6.2"
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.2'
}
test {
useJUnitPlatform()
}
Does anyone know how to make it work?
Thank you for your help.
Due to a bug in JUnit, junit-vintage-engine:5.6.2 did not support JUnit 4.13.2. It only supported 4.12 or 4.13. So that's why you see that exception.
That bug was fixed in JUnit 5.6.3. See https://github.com/junit-team/junit5/issues/2198 for details.
You are also mixing versions of JUnit 5 artifacts, which you should try to avoid. For example, if you want to use JUnit Platform 1.7.2, I would advise that you use JUnit Jupiter 5.7.2 and JUnit Vintage 5.7.2.
For details on how to correctly configure the JUnit versions with Spring Boot, see
Gradle 5 JUnit BOM and Spring Boot Incorrect Versions.
I was having this error too.
In my idea project file I had 2 versions of junit dependencies declared, 5.4.2 and 4.13.2.
getting rid of 4.13.2 fixes the issue for me.
I solved this problem by adding the dependency as suggested by the spring documentation:
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
Reference doc: Spring-Boot 7.8 - Test

How can I add spring-security-dependency with gradle in IntelliJ?

I would like to use the dependency:
implementation 'org.springframework.boot:spring-boot-starter-security'
in my IntelliJ-project, but for any reason, it cannot resolve this dependency.
These are my dependencies. The relevant one is spring-boot-starter-security.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
runtime('org.springframework.boot:spring-boot-devtools')
}
For any reason I get this error message, when starting boot-run:
Error-message when starting boot-run
Here the whole build.gradle-file:
plugins {
id 'org.springframework.boot' version '2.2.7.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'de.hohenheim'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
runtime('org.springframework.boot:spring-boot-devtools')
}
test {
useJUnitPlatform()
}
bootRun {
sourceResources(sourceSets.main)
}
You need to re-enable your Gradle online mode
For the latest version of IntelliJ IDE, please find an option here :
For older version :
Preferences
Go inside Build, Execution, Deployment.
Finally select Gradle.
uncheck offline work. Click apply then ok.
Hope this will work.
It seems that you're running Gradle in offline mode. In that mode, Gradle will only use its local cache. When you add a new dependency to the project, Gradle will fail to find it. To fix this error, disable the offline mode in Intellij.
Go to the Gradle tool window and disable this button:
It should appear in the toolbar (top of the window). If it isn't visible, like in the picture below, it should appear when you make the window larger, or when you click the two arrows on the far right of the toolbar (marked in blue):

Spring Boot/Gradle/Querydsl project has the same dependency dependent on different versions of another dependency

I am having problems adding Querydsl to my Spring Boot project. I have the following dependencies in my build.gradle file:
dependencies {
annotationProcessor 'com.querydsl:querydsl-apt:4.1.3:jpa'
annotationProcessor 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.querydsl:querydsl-jpa:4.1.3'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.3.RELEASE'
implementation 'org.springframework.cloud:spring-cloud-config-client:2.1.0.RELEASE'
implementation 'mysql:mysql-connector-java'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'io.springfox:springfox-bean-validators:2.9.2'
implementation group: 'org.latencyutils', name: 'LatencyUtils', version: '2.0.3'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-inline:2.23.4'
}
The project builds fine, but when I run it, I get the following error:
An attempt was made to call the method com.google.common.collect.FluentIterable.concat(Ljava/lang/Iterable;Ljava/lang/Iterable;)Lcom/google/common/collect/FluentIterable; but it does not exist. Its class, com.google.common.collect.FluentIterable, is available from the following locations:
jar:file:/C:/Users/me/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/18.0/cce0823396aa693798f8882e64213b1772032b09/guava-18.0.jar!/com/google/common/collect/FluentIterable.class
jar:file:/C:/Users/me/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/20.0/89507701249388e1ed5ddcf8c41f4ce1be7831ef/guava-20.0.jar!/com/google/common/collect/FluentIterable.class
It was loaded from the following location:
file:/C:/Users/me/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/18.0/cce0823396aa693798f8882e64213b1772032b09/guava-18.0.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.google.common.collect.FluentIterable
The Gradle window in IntelliJ shows the following:
Why does each instance of com.querydsl:querydsl-core:4.2.1 depend on a different version of guava?
The version 18.0 of Guava comes from querydsl and the 20.0 comes from spring-fox swagger.
spring-fox library expected that method of guava version 20.0 to be called. but called from 18.0. (because there are two guava library that has difference version in classpath)
you can use the following code to avoid conflict version of guava.
// QueryDsl
implementation("com.querydsl:querydsl-jpa:${querydslVersion}")
annotationProcessor("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final")
annotationProcessor("com.querydsl:querydsl-apt:${querydslVersion}:jpa") {
exclude group: "come.google.guava"
}
annotationProcessor("com.google.guava:guava:20.0")

Resources