Spring Boot未添加spring-boot-starter-data-jpa

3
尝试通过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()
}

在有人提到之前,是的,我已经尝试多次清理和重建这个项目了。


请将您的答案作为“已解决”的答案添加,而不是编辑问题,然后从问题中删除它,并标记此评论为不再需要。 - Petter Friberg
3个回答

2
如果您正在使用Gradle 6.x,则compile配置已被弃用。自Gradle 3.4以来已经不建议使用它。您应该改用implementation。此更改还将使此依赖关系与构建脚本中的其他依赖关系更加一致。您可以在Gradle文档中了解更多信息。
您还在spring-boot-starter-data-jpa依赖项上指定了一个版本。这并不是必要的,因为版本可以通过应用的Spring Boot插件的版本确定。这就是在脚本中未声明版本的其他依赖项中发生的情况。这使得所有版本保持同步更加容易。
简而言之,请尝试将依赖声明更新为以下内容:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

这是我在查看 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa/2.3.4.RELEASE 之前的原始代码。不幸的是,它仍然无法正常工作。 - David Kostandin

1
已解决:问题出在Spring工具套件中,使用Project->Clean没有更新gradle依赖项。必须右键单击build.gradle->gradle->刷新gradle项目才能使所有内容更新。

这也是我的问题。Build -> Rebuild project 没有识别出我在 build.gradle 中添加的新的 JPA 依赖项。相反,我不得不导航到 Gradle 标签并选择 Reload all Gradle Projects 按钮。它识别出了新的依赖项,下载了所有内容,现在一切正常。 - undefined

0
根据之前的建议,在VS Code中清理Java项目帮助我解决了问题: 打开“Java项目”视图 -> “…” -> “清理工作区”
根据VS Code的配置,清理后gradle会自动重建依赖项。如果没有,请右键单击build.gradle并选择“更新项目”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接