在使用maven多模块整合springboot项目时候,意外发现mvn install 报错提示main方法找不到
错误日志如下:
Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find main class
google之,发现很多都是提示类似问题可能是因为项目中多个main导致的,需要在pom.xml中配置
properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<start-class>org.roshan.Application</start-class>
</properties>
又或者
profiles>
<profile>
<id>profile1</id>
<properties>
<spring.boot.mainclass>com.detaysoft.Application</spring.boot.mainclass>
</properties>
</profile>
</profiles>
又或者
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>${spring.boot.mainclass}</mainClass>
</configuration>
</execution>
</executions>
</plugin>
然而,病没什么乱用.
所以经过一番排除之后,发现其实是plugin配置的问题,罪魁祸首就是
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
因为父项目中的pom.xml如上面配置,意思是直接试用plugin的意思,这里父类项目中直接应用maven-plugin.所以会去每个module中寻找main方法进行编译,一旦不需要run的module被检查到没有main方法 所以 哼哼…
解决方式2种:
1 直接在需要run的module里面配置maven-plugin
2. 想直接在parent-module中配置,可以用pluginManagement标签,表示声明插件变量,所有child-module都可以继承,也可以覆盖.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
没了.