我在 https://start.spring.io/ 创建了我的项目
网上有几个类似的错误,但没有解决我的问题。
执行时发现错误返回:
Failed to execute goal org.springframework.boot: spring-boot-maven-plugin: 2.4.1: run
这是我的 POM 和主文件 main
POM:
<?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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.javaguides</groupId>
<artifactId>spring-boot-restfull-webservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-restfull-webservice</name>
<description>Demo project for 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-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
类(class):
package net.javaguides.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootRestfullWebserviceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestfullWebserviceApplication.class, args);
}
}
我是 Springboot 新手,如何解决这个问题?
最佳答案
我已经让你的构建工作了,但我必须做出一些妥协。
我查看了 Spring 文档以了解您正在尝试的内容:
https://spring.io/guides/gs/rest-service/
然后我查看了 pom.xml 中的示例:
https://github.com/spring-guides/gs-rest-service/blob/master/complete/pom.xml
尽管文档说您可以使用 Java 1.8 或更高版本,但您需要使用 1.8。如果你走得更高,你必须在插件中指定属性编码,但你不能这样做,因为你使用的是 Spring Boot 插件,而不是你可以在项目的 pom.xml 中配置的插件。
我还添加了比 Spring Boot 使用的版本更低的 maven-surefire-plugin。在此版本中,您会收到错误[ERROR]存在测试失败
,但您没有运行任何测试。
我注释掉了 MySql 和 JPA 依赖项,除非您有数据库连接设置,否则它们将无法工作。
<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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.javaguides</groupId>
<artifactId>spring-boot-restful-webservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-restful-webservice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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-web</artifactId>
</dependency>
<!--
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skipTests>false</skipTests>
<testFailureIgnore>true</testFailureIgnore>
<forkMode>once</forkMode>
</configuration>
</plugin>
</plugins>
</build>
</project>
关于java - Maven - 无法执行目标 org.springframework.boot :spring-boot-maven-plugin:2. 4.1:run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65427175/