Skip to content

关于多模块处理打包问题

💡 Tips!: 是一个简单的小问题,但是在测试之初,没注意到这点,又查看了资料和C**N之后,发现解决的很复杂,所以才有了以下的尝试,所幸成功了,新增二,如果决定尝试请一定看完二,否则不要尝试

在没有主类的项目文件(也就是依赖项目中),让依赖项目作为聚合项目,不生成JAR或者WAR文件,作为一个聚合和依赖管理的工具,帮助组织和构建多个模块。

以下仅供作为参考学习,假设结构如下

A<父>
├── A.xml
├── B<子>
│   └── pom.xml
├── C
│   └── pom.xml
├── D
│   └── pom.xml
├── E
│   └── pom.xml
├── F
│   └── pom.xml
├── G
│   └── pom.xml
└── H
    └── pom.xml
假设只有D有 `@SpringbootApplication` 主类启动,那么对于没有主启动类,如果报出下面分析中的问题,
加入以下即可

<packaging>pom</packaging> 

然后再在你的主类,也就是启动项目中放置打包插件,并指向主类

   <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.pms.RestApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

在父类中加入

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

在后来的打包中,我又对上述的结构树进行了分析。发现了更为便捷优雅的方式,取消没有主类的打包方式,也就是的那一堆打包,那么我们只需要在有主类的模块中加入打包构建即可;

随后确保其他模块为jar包类型即可(一般默认即是),此外对于主类包,比如主类包中用了A或者B...等的子模块,只需要引入他们的依赖即可,然后再root下选择clean-install-package即可,选择主类中产生的jar包,丢到服务器中即可

分析

在没有加入pom之前的错误参考如下

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.3.2:repackage (repackage) on project pms-common: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:3.3.2:repackage failed: Unable to find main class -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[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
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :pms-common