关于java:Spring Boot:重命名Main类

Spring Boot: Renaming the Main class

我是Spring和Spring Boot的新手,但是我将其用作模板。

Main.java文件部分包含以下内容:

1
2
3
4
5
6
7
package com.example;

@Controller
@SpringBootApplication
public class Main {

}

不幸的是,我需要导入另一个称为Main的类,并在该类中使用它。另一个类在Jar中,我真的不想重新编译它,所以我在想最好的方法是重命名这个Main。

但是,当我这样做时(将其重命名为JavaGettingStarted),Spring Boot的Maven插件将失败:

1
2
3
4
5
6
7
[ERROR] Failed to execute goal
org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE:repackage
(default) on project java-getting-started: Execution default of goal
org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE:repackage
failed: Unable to find a single main class from the following
candidates [com.example.JavaGettingStarted, com.example.Main] -> [Help
1]

Main是否有某种默认设置?我可以更改它吗?


问题不在于类的名称,而是Spring Boot Maven插件使用main方法检测到两个类。删除这些主要方法之一,或显式配置maven插件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<plugin>
  <groupId>org.springframework.boot</groupId>
  spring-boot-maven-plugin</artifactId>
  <version>2.0.0.RELEASE</version>
  <configuration>
    <mainClass>Your class name here</mainClass>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
</plugin>