英文:
Spring Boot project build failed - cannot find package
问题
以下是翻译好的内容:
我在我的Spring Boot项目中遇到了一个严重的问题。出于某种原因,当我启动应用程序时,我会得到一个项目构建失败的错误,如下面的截图所示。
这真的让人很惊讶,因为我已经开发了一段时间的应用程序,而且显然已经多次启动而没有任何问题。今天,我想再次启动它,却得到了这个错误,而且昨天一切都正常工作!!!而且,我没有对代码或设置进行任何修改。
尽管它们确实存在,但它找不到一些包。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pl.maciejdluzen</groupId>
<artifactId>hotelreservation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hotelreservation</name>
<description>Hotel reservation system in Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 其他依赖... -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编辑 1 - 一些额外的信息:
我知道我在使用的Java版本上有些混乱,因为mvn clean install
会抛出以下错误(但当我使用Intellij的绿色箭头按钮启动应用程序时,一切都正常):
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project hotelreservation: Fatal error compiling: invalid target release: 11 -> [Help 1]
所以在我的Maven依赖中,我有Java 11:<java.version>11</java.version>
java -version
和 javac -version
分别显示 1.8
和 9
版本。
在Intellij IDE设置中,项目的SDK设置为Java 14.0.2
,只有以下SDK可用:
java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
javac - version
javac 9.0.1
mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: C:\apache-maven-3.6.3\bin\..
Java version: 1.8.0_261, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_261\jre
英文:
I have got a massive problem with my Spring Boot project. For some reason, when starting my application, I get a project build failure error as per screenshot.
It is a huge surprise as I have been developing this app for a while now and obviously launching it number of times without any problems. Today, I wanted to start it again and got that error and yesterday everything worked fine!!! Moreover, I didn't make any modifications to the code or settings.
It cannot find some packages, even though they do exist.
Any help will be very much appreciated!
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pl.maciejdluzen</groupId>
<artifactId>hotelreservation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hotelreservation</name>
<description>Hotel reservation system in Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Edit 1 - some additional info:
I know that I have got some mess with java versions I've been using as mvn clean install
throws the following error (but when I was launching the app using Intellij green arrow button, it was all fine):
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project hotelreservation: Fatal error compiling: invalid target release
: 11 -> [Help 1]
So in my Maven dependencies I have got Java 11 <java.version>11</java.version>
java -version
and javac -version
show respectively 1.8
and 9
version.
In the Intellij IDE settings Project SDK is set to Java 14.0.2
and only the following SDKs are available:
java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
javac - version
javac 9.0.1
mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: C:\apache-maven-3.6.3\bin\..
Java version: 1.8.0_261, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_261\jre
答案1
得分: 1
要么安装Java 11 SDK并将其添加到Intellij,要么在pom.xml中将Java版本更改为已安装版本之一,例如:
<properties>
<java.version>1.8</java.version>
</properties>
英文:
Either install java 11 SDK and add it to Intellij, or change the java verion in the pom.xml to one of the installed version, like:
<properties>
<java.version>1.8</java.version>
</properties>
答案2
得分: 0
尝试在主类上使用以下注解,与 @SpringBootApplication
一起:
@ComponentScan(basePackages = "org.my.pkg")
英文:
Try with fallowing annotation on main class with @SpringBootApplication
@ComponentScan(basePackages = "org.my.pkg")
答案3
得分: -1
你尝试过重新构建Maven项目吗?
英文:
Have you tried rebuilding the Maven project?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论