执行maven clean后Spring Boot项目编译失败求助
maven clean后编译失败的解决方法 看起来你在执行maven clean之后,遇到了项目无法编译和启动的双重问题,我来帮你一步步排查解决:
问题回顾
你遇到的两个核心错误:
- Eclipse启动时报错:
Impossible to find or load the main class com.in28minutes.springboot.rest.example.springboot2restservicebasic.SpringBoot2RestServiceBasicApplication
- 终端执行
mvn spring-boot:run或spring-boot:repackage时编译失败:
Compilation failure [ERROR] /D:/eclipse-workspace/spring-boot-2-rest-service-basic/spring-boot-2-rest-service-basic/src/main/java/com/in28minutes/springboot/rest/example/springboot2restservicebasic/api/StudentResource.java:[18,45] package net.bytebuddy.implementation.bytecode does not exist
你的项目pom.xml依赖配置如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
问题根源
这个问题的核心是**spring-boot-devtools的依赖范围设置错误**:你把它的scope设为了runtime,这意味着这个依赖只会在运行时被加载,但net.bytebuddy是DevTools的依赖之一,你的代码(StudentResource.java)在编译时需要用到它的API,所以maven clean清理掉之前的编译产物后,编译期找不到这个包,就报错了。而主类找不到的问题,是编译失败导致没有生成对应的class文件引发的连锁反应。
解决步骤
1. 调整spring-boot-devtools的依赖范围
修改pom.xml中spring-boot-devtools的配置,移除scope标签(默认就是compile范围),或者把scope从runtime改为compile:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <!-- 移除这一行 <scope>runtime</scope>,或者改为<scope>compile</scope> --> </dependency>
2. 重新编译并恢复项目
- 先在终端执行
mvn clean install,这个命令会重新下载依赖、清理旧编译产物并重新编译整个项目:mvn clean install - 完成后,再尝试用
mvn spring-boot:run启动项目;如果是用Eclipse,右键项目 → Maven → Update Project,勾选「Force Update of Snapshots/Releases」,更新完成后再通过「RunAs --> Spring Boot App」启动。
3. 额外排查(如果上述步骤无效)
- 检查
StudentResource.java中是否误导入了net.bytebuddy的包,如果是不小心加的,直接删除该导入语句即可。 - 确认Eclipse的构建路径是否正常:右键项目 → Build Path → Configure Build Path,查看Libraries下的Maven Dependencies是否有红色叉号,若有则重复上述的Maven Update操作。
内容的提问来源于stack exchange,提问作者pinale




