1. 程式人生 > >解決Unable to find a single main class from the following candidates [xxx,xxx]

解決Unable to find a single main class from the following candidates [xxx,xxx]

一、問題描述

1.1 開發環境配置

  1. pom.xml
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<!--一定要對上springboot版本號,因為新版springboot不再設定這個外掛的依賴-->
	<version>${spring-boot.version}</version>
	<executions>
		<execution>
			<goals>
				<goal>repackage</goal>
			</goals>
		</execution>
	</executions>
</plugin>

1.2 錯誤異常

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.17.RELEASE:repackage (default) on project taco-cloud-eureka-server: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:2.1.17.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.xxx.xxx.Application, com.xxx.xxx.Application]

二、解決方法

2.1 去掉repackage,使用mainclass替代

  1. 在頂級父工程pom.xml中配置
<properties>
	<!--需要子類覆蓋該屬性,解決有多個main方法-->
	<mainClass>com.taco.springcloud.Application</mainClass>
</properties>

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<!--一定要對上springboot版本號,因為新版springboot不再設定這個外掛的依賴-->
	<version>${spring-boot.version}</version>
	<configuration>
		<mainClass>${mainClass}</mainClass>
	</configuration>
</plugin>
  1. 在需要部署為web工程的專案pom.xml中覆蓋mainClass屬性
<properties>
	<mainClass>com.taco.springcloud.TacoConfigServerApplication</mainClass>
</properties>

這樣即使依賴包中的有其他main