Springboot mavne项目多模块打包,报错 找不到 base包,找不到common类等等

报错示例:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.4.RELEASE:repackage (default) on project jcdemo-common: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:2.0.4.RELEASE:repackage failed: Unable to find main class -> [Help 1]

ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

认真看maven打包报错里面提到的关键信息:

repackage (default) on project jcdemo-common

 unable to find main class

这是因为什么? 是因为我的jcdemo-common 这个模块是个没有main函数入口的模块,只是用于提供jar依赖给其他的模块使用。

目前我的项目结构为:

jcdemo-parent   这个是父模块

jcdemo-common 这个是基础的公共类模块

jcdemo-myWebA 这是一个web A项目,打包设置是war ,里面引用了common的jar

jcdemo-myWebB 这是一个web B项目,打包设置是war ,里面也引用了common的jar

之所以出现上面找不到 jcdemo-common模块的类,是因为我在  jcdemo-parent 和   jcdemo-common 的pom.xml里面都加入了(不要加!):

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

那么仅仅是因为这个原因吗? 直接看正确流程:

maven多模块打包 正规流程

第一步:

首先,在父模块的pom.xml  ,排查是否加了 :

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

如果有,删掉。

第二步:

检测所谓的 common 模块的pom.xml,排查是否也加了:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

如果有,也删掉,因为我们的common 模块是没有main函数入口的。

然后在common 模块的pom.xml里加上:

    <build>
        <plugins>
            <!-- 资源文件拷贝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

第三步,在你的web模块的pom.xml里面,加上:


        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.xxx.Application</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>

注意指明打包的main函数入口,也就是 springboot的启动类:

ok,可以打包了(直接对着你的父模块点package就行,也可以先clean一下):

可以看到,该打的都成功打包:

猜你喜欢

转载自blog.csdn.net/qq_35387940/article/details/109067973
今日推荐