问题描述
当尝试通过gradle将spring-boot-starter-data-jpa添加到我的项目中时,它只是不这样做。 @Entity标记无效,并且jar不会出现在项目和外部依赖项文件夹中。除非我输入@Entity标记,否则没有错误。这是我的gradle文件供参考。
plugins {
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.Hype'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.springframework.boot',name: 'spring-boot-starter-data-jpa',version:
'2.3.4.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation 'org.springframework.session:spring-session-jdbc'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'MysqL:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
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()
}
在任何人提到它之前,是的,我已经尝试过多次清理和重建项目。
解决方法
如果您正在使用Gradle 6.x,则compile
配置已被弃用。自Gradle 3.4起不鼓励使用它。您应该改为使用implementation
。此更改还将使此依赖项与构建脚本中的其他依赖项更加一致。您可以在Gradle documentation中进一步了解这一点。
您还已经在spring-boot-starter-data-jpa
依赖项上指定了版本。这不是必需的,因为该版本可以由您所应用的Spring Boot插件的版本确定。这就是脚本中没有声明任何版本的其他依赖项所发生的情况。这样可以更轻松地使所有版本保持同步。
简而言之,请尝试将依赖项声明更新为如下所示:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
,
已解决:问题出在Spring工具套件中,使用Project-> Clean不会更新gradle依赖项。必须右键单击build.gradle-> gradle-> refresh gradle项目以更新所有内容。