春季启动项目无法在Gradle本地存储库上运行。

huangapple go评论68阅读模式
英文:

Spring boot project doesn't run on Gradle local Repository

问题

以下是我的 build.gradle 文件:

plugins {
	id 'org.springframework.boot' version '2.3.4.RELEASE'
	id 'java'
}

group = 'jwt.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
	flatDir {
		dirs 'C://Users//David//.gradle//caches//modules-2//files-2.1'
	}
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter:2.3.4.RELEASE'
	implementation 'org.springframework.boot:spring-boot-starter-web:2.3.4.RELEASE'
}

test {
	useJUnitPlatform()
}

当我在 IntelliJ 的 Gradle 面板上点击“构建”和“组装”图标时,它会显示“构建成功”。但是当我添加 Spring Boot 注解 @SpringBootApplication 时,它会显示错误:“无法解析符号 SpringBootApplication”。看起来它没有成功编译。有任何想法如何修复吗?

英文:

Below is my build.gradle file

plugins {
	id 'org.springframework.boot' version '2.3.4.RELEASE'

	id 'java'
}

group = 'jwt.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
	flatDir {
		dirs 'C://Users//David//.gradle//caches//modules-2//files-2.1'
	}
}
dependencies {
	implementation 'org.springframework.boot:spring-boot-starter:2.3.4.RELEASE'
	implementation 'org.springframework.boot:spring-boot-starter-web:2.3.4.RELEASE'


}
test {
	useJUnitPlatform()
}

When i click on build and assemble icon of the Gradle panel on the intelliji, it shows "BUILD SUCCESFUL" But when i add spring boot annotation @SpringBoot, it shows error " cannot resolve symbol SpringbootApplication". It seems it was not compiled succesfully. Any ideas how to fix it ?

答案1

得分: 1

TL;DR 使用Spring Initializr来进行正确的项目设置。以下是原因。

flatDir 仓库,尤其是将Gradle缓存目录定义为源,不适用于开发Spring Boot应用,原因如下:

  • Gradle共享缓存目录是Gradle的内部结构,用于存储来自远程仓库的已解析构件。它默认情况下最初是空的。解析但未使用的构件会在一定时间后被删除。缓存的结构对于flatDir并不适用,因为它期望的是目录中的JAR文件,而不是目录结构。
  • flatDir 不支持像Ivy XML或Maven POM文件这样的元数据格式,因此依赖信息不可用。spring-boot-starterspring-boot-starter-web 在它们各自的POM中定义了许多依赖关系。独立使用时,它们相当无用。

最好使用一个合适的(公共)Maven仓库。Gradle内置了Maven Central(mavenCentral())、JCenter(jcenter())和Google(google())的支持。Gradle会缓存已解析的构件,所以它们通常只会被解析一次。更多信息请阅读官方文档:声明公共可用仓库

您还可以使用Spring Initializr,它将为您生成一个可用的项目和构建文件。

英文:

TL;DR use the Spring Initializr for a proper project setup. Here's why.

flatDir repositories and especially defining the Gradle cache directory as source are not suitable to develop a Spring Boot application for the following reasons:

  • The Gradle shared cache directory is an internal construct of Gradle where it stores resolved artifacts from remote repositories. It is initially empty by default. Resolved but unused artifacts get removed after a certain period of time. The structure of the cache makes is also not suitable for flatDir which expects jars rather than directory structures in the directory defined.
  • flatDir does not support any meta-data formats like Ivy XML or Maven POM files, hence dependency information is unavailable. spring-boot-starter and spring-boot-starter-web define lots of dependencies in their respective POMs. Standalone they're quite useless.

You're better off to use a proper (public) Maven repository. Gradle has built-ins for Maven Central (mavenCentral()), JCenter (jcenter()) and Google (google()). Gradle caches the resolved artifacts so that they're usually resolved only once. Read more about this in the official documentation: Declaring a publicly-available repository.

You may also want to use the Spring Initializr that will generate a working project and build file for you.

huangapple
  • 本文由 发表于 2020年10月9日 15:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64275866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定