簡體   English   中英

Spring Boot:重命名Main類

[英]Spring Boot: Renaming the Main class

我是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是某種默認值嗎? 我可以改變嗎?

問題不在於類的名稱,而在於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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM