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')
}
Related
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()
}
I need to keep versions of dependencies in gradle.properties.
gradle.properties:
springBootVersion = '2.1.9.RELEASE'
build.gradle:
plugins {
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'org.springframework.boot' version '2.1.9.RELEASE'
}
subprojects {
repositories {
mavenCentral()
}
configurations.all {
resolutionStrategy {
failOnVersionConflict()
}
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
dependencies {
dependency "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
dependency "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
}
}
sourceCompatibility = 11
targetCompatibility = 11
tasks.withType(JavaCompile){
options.encoding = 'UTF-8'
}
}
settings.gradle:
include 'api'
api/build.gradle:
apply plugin: 'org.springframework.boot'
archivesBaseName = 'phone-gift-processing-api'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Output of ./gradlew clean build:
> Task :api:compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':api:compileJava'.
> Could not resolve all files for configuration ':api:compileClasspath'.
> Could not find org.springframework.boot:spring-boot-starter-web:'2.1.9.RELEASE'.
Required by:
project :api
I.e., Gradle uses springBootVersion, but fails to bind the dependency for some reason.
When I replace ${springBootVersion} by ${springBootVersion}, build is successful.
You have the format wrong in gradle.properties. Instead of this:
springBootVersion = '2.1.9.RELEASE'
Try this:
springBootVersion=2.1.9.RELEASE
(The spaces are optional, but the important part is that you should not have the quotation marks around the value.)
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
I have application.properties with that code:
spring.config.additional-location=file:///C:/Users/user/Desktop/project/cfg.properties
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=true
and in cfg.properties it looks like that:
spring.datasource.url=jdbc:oracle:thin:#correctDbUrl
spring.datasource.username=user
spring.datasource.password=pass
I think there is something wrong with the path - can't it be outside the project or what happened? This is an error (url is correct, it works when placed directly in application.properties):
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine suitable jdbc url
Gradle:
buildscript {
ext {
springBootVersion = '2.0.3.RELEASE'
}
repositories {
maven { url "someRepository" }
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.abc'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
maven { url "someRepository" }
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile(group: 'com.hynnet', name: 'oracle-driver-ojdbc6', version: '12.1.0.1')
compile("org.springframework.boot:spring-boot-starter-data-jpa")
}
Set the following environment variable.
SET SPRING_CONFIG_LOCATION=classpath:/application.properties,file:C:/Users/user/Desktop/project/cfg.properties
you can remove "spring.config.additional-location" from the property file.
Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx] Offending resource: ServletContext resource [/WEB-INF/spring-servlet.xml]
In gradle you can use shadowJar plugin for fix that. My build.gradle file for compile fat jar:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'application'
mainClassName = 'ru.antowka.Initializer'
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
}
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
shadowJar {
mergeServiceFiles('META-INF/spring.*')
}
// JDK 8
sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding = 'UTF-8'
repositories {
maven {
url 'http://repo.spring.io/snapshot'
}
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
compile 'org.jsoup:jsoup:1.8.3'
compile 'org.springframework:spring-context:4.2.2.BUILD-SNAPSHOT'
compile 'log4j:log4j:1.2.17'
compile 'org.quartz-scheduler:quartz:1.8.6'
compile 'org.springframework:spring-support:2.0.8'
compile 'org.springframework:spring-tx:2.5.4'
compile 'org.springframework:spring-orm:4.1.7.RELEASE'
compile 'org.hibernate:hibernate-core:4.3.10.Final'
compile 'org.postgresql:postgresql:9.4-1201-jdbc41'
compile 'org.apache.commons:commons-dbcp2:2.1'
testCompile 'org.springframework:spring-test:4.2.0.RELEASE'
testCompile 'org.mockito:mockito-core:1.+'
testCompile 'junit:junit:4.12'
}