Spring Boot项目构建失败 – 无法找到包。

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

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>

Spring Boot项目构建失败 – 无法找到包。

Spring Boot项目构建失败 – 无法找到包。

编辑 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 -versionjavac -version 分别显示 1.89 版本。

在Intellij IDE设置中,项目的SDK设置为Java 14.0.2,只有以下SDK可用:

Spring Boot项目构建失败 – 无法找到包。

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!

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
	&lt;parent&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
		&lt;version&gt;2.3.2.RELEASE&lt;/version&gt;
		&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
	&lt;/parent&gt;
	&lt;groupId&gt;pl.maciejdluzen&lt;/groupId&gt;
	&lt;artifactId&gt;hotelreservation&lt;/artifactId&gt;
	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
	&lt;name&gt;hotelreservation&lt;/name&gt;
	&lt;description&gt;Hotel reservation system in Spring Boot&lt;/description&gt;

	&lt;properties&gt;
		&lt;java.version&gt;11&lt;/java.version&gt;
	&lt;/properties&gt;

	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
			&lt;scope&gt;runtime&lt;/scope&gt;
			&lt;optional&gt;true&lt;/optional&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.h2database&lt;/groupId&gt;
			&lt;artifactId&gt;h2&lt;/artifactId&gt;
			&lt;scope&gt;runtime&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;mysql&lt;/groupId&gt;
			&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
			&lt;scope&gt;runtime&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
			&lt;artifactId&gt;lombok&lt;/artifactId&gt;
			&lt;optional&gt;true&lt;/optional&gt;
		&lt;/dependency&gt;
		&lt;!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.modelmapper&lt;/groupId&gt;
			&lt;artifactId&gt;modelmapper&lt;/artifactId&gt;
			&lt;version&gt;2.3.8&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.thymeleaf.extras&lt;/groupId&gt;
			&lt;artifactId&gt;thymeleaf-extras-springsecurity5&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
					&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
			&lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;junit&lt;/groupId&gt;
			&lt;artifactId&gt;junit&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;

	&lt;build&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;

&lt;/project&gt;

Spring Boot项目构建失败 – 无法找到包。

Spring Boot项目构建失败 – 无法找到包。

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 -&gt; [Help 1]

So in my Maven dependencies I have got Java 11 &lt;java.version&gt;11&lt;/java.version&gt;

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:
Spring Boot项目构建失败 – 无法找到包。

java -version

java version &quot;1.8.0_261&quot;
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:

&lt;properties&gt;
    &lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;

答案2

得分: 0

尝试在主类上使用以下注解,与 @SpringBootApplication 一起:

@ComponentScan(basePackages = "org.my.pkg")
英文:

Try with fallowing annotation on main class with @SpringBootApplication

@ComponentScan(basePackages = &quot;org.my.pkg&quot;)

答案3

得分: -1

你尝试过重新构建Maven项目吗?

英文:

Have you tried rebuilding the Maven project?

huangapple
  • 本文由 发表于 2020年9月27日 17:35:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64086973.html
匿名

发表评论

匿名网友

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

确定