【问题标题】:Maven Build Failure - Missing Main ClassMaven 构建失败 - 缺少主类
【发布时间】:2017-07-21 10:43:19
【问题描述】:

我正在尝试使用 Maven 开始使用 Spring Boot 应用程序。我的教程来自:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started

我的 pom.xml 看起来像这样:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
<!-- Additional lines to be added here... -->

<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
        <snapshots><enabled>true</enabled></snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>

我的项目文件夹如下所示:Test\src\main\java

Pom.xml 在 Test\bin 中

只有一个 Java 文件:

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

如果我在 cmd 中使用 mvn spring-boot:run 运行它,我会遇到构建失败,指出它无法找到合适的主类

堆栈跟踪:

[INFO] BUILD FAILURE
[INFO]      ------------------------------------------------------------------------
[INFO] Total time: 2.206 s
[INFO] Finished at: 2017-07-21T12:33:18+02:00
[INFO] Final Memory: 21M/227M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-     maven-plugin:2.0.0.BUILD-SNAPSHOT:run (default-cli) on project myproject:   Unable to find a suitable main class, please add a 'mainClass' property ->   [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions,   please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

【问题讨论】:

  • 请添加堆栈跟踪
  • Java 文件在 Test\src\main\java

标签: java spring maven spring-boot


【解决方案1】:

您的 Example 类是有效的 Spring Boot Main 类。

问题就在这里:

Pom.xml 在 Test\bin 中

您的 pom.xml 未在正确的位置声明。

默认情况下,Maven 期望 src\main\java 文件夹位于定义 pom.xml 的同一级别。

因此,使用这种布局,Maven 会尝试在

中找到您的类
Test\bin\src\main\java

但是你的课在这里:

我的项目文件夹如下所示:Test\src\main\java

只需将 pom.xml 移动到 Test 文件夹的根目录即可。

【讨论】:

  • 感谢您的热心帮助
【解决方案2】:

Maven 期望文件位于特定位置: 如果您的 java 文件在 Test\src\main\java 中,那么您的 pom 文件应该在 Test 中,而不是在 Test\bin

只需使用相同的代码运行它,它就可以正常构建。

您可以在此处阅读有关 maven 中文件位置的更多信息: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

【讨论】:

  • 感谢您的热心帮助
猜你喜欢
  • 1970-01-01
  • 2021-05-30
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 2022-10-20
  • 1970-01-01
  • 2016-03-25
  • 2019-12-14
相关资源
最近更新 更多