Spring Boot:重命名Main类

时间:2018-03-16 21:31:07

标签: java spring maven spring-boot

我是Spring和Spring Boot的新手,但我正在使用它作为模板。

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

package com.example;

@Controller
@SpringBootApplication
public class Main {

}

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

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

[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是某种默认值吗?我可以改变吗?

1 个答案:

答案 0 :(得分:3)

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

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>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>