Spring #ComponentScan in gradle subprojects - java

When it is one gradle package with #Service definition and another package with dependency on this service, dependency is not satisfied.
I use root gradle project with two submodules, every with dependency on spring boot.
Root settings.gradle
...
include ':shared'
include ':app'
Shared library shared/build.gradle
plugins {
id 'java-library'
id 'org.springframework.boot' version '2.1.5.RELEASE'
}
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
}
Application app/build.gradle
plugins {
id 'java'
id 'application'
id 'org.springframework.boot' version '2.1.5.RELEASE'
}
apply plugin: 'io.spring.dependency-management'
dependencies {
compile project(':shared')
implementation 'org.springframework.boot:spring-boot-starter'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
}
mainClassName = 'com.example.app.App'
Application main app/src/main/java/com/example/app/App.java
package com.example.app;
import com.example.shared.SharedService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan("com.example.shared")
public class App {
public static void main(String[] args) {
var context = SpringApplication.run(App.class);
var service = context.getBean(SharedService.class);
}
}
Produces error
Exception in thread "main" java.lang.NoClassDefFoundError: com/example/shared/SharedService
at com.example.app.App.main(App.java:14)
Caused by: java.lang.ClassNotFoundException: com.example.shared.SharedService
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)

Related

Jsp page getting download instead of rendering in gradle

I am using spring boot with Gradle I have added all the dependencies in the build. Gradle file but I don't know why the JSP page is getting downloaded instead of rendering.
This is my code.
build.gradle
plugins {
id 'org.springframework.boot' version '2.5.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'war'
}
group = 'com.example.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
implementation group: 'org.apache.tomcat', name: 'tomcat-jasper', version: '9.0.31'
implementation group: 'javax.servlet', name: 'jstl', version: '1.2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
application.properties
server.port=8000
spring.mvc.view.prefix=/view/
spring.mvc.view.suffix=.jsp
PageController.java
package com.example.demo.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#Controller
public class PageController {
#RequestMapping("/")
public String home() {
return "Home";
}
}
Folder structure image
https://i.stack.imgur.com/J9VWK.png
I finally found the answer for my question by changing the version of tomcat-jasper according to tomcat-embed-core.jar file in build.gradle.
For example if tomcat-embed-core.jar was 9.0.54 then build.gradle file should contain tomcat-jasper version 9.0.54 and it is applicable for maven too.
Here is the image that I have attached for reference
https://i.stack.imgur.com/KZ9BQ.png

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()
}

Spring: Error creating bean with name 'entityManagerFactory'

I am following the tutorial at https://spring.io/guides/tutorials/bookmarks/ . I used the initializer and decided to use gradle. When attemting to build the application or directly run it, I'm getting the error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
I guess that's caused by the spring-boot-starter-data-jpa or com.h2database:h2, and probably related to some version mismatch. However, I don't know what to do about it.
My gradle.build:
buildscript {
ext {
springBootVersion = '2.0.0.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 = 'sfs.examples'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 9.0
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

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:

Categories

Resources