You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Maven Profiles的Spring Boot多入口应用构建失败排查

问题根源与解决方案

你的核心问题并非Spring Boot Maven插件不支持Maven Profiles,而是插件默认会扫描整个项目的所有主类,若未在激活的配置中明确强制指定单一主类,就会触发多主类冲突。以下是具体排查和解决步骤:

1. 纠正Maven Profile激活的命令误区

你之前使用的-Drun.profiles=dcr是Spring Boot运行时的参数,不会激活Maven Profile,正确激活Maven Profile的命令是:

mvn clean package -P dcr

这是导致Profile配置未生效的常见原因之一。

2. 确保Profile内的插件配置完全覆盖主类参数

在每个Profile中,必须单独配置spring-boot-maven-plugin并明确指定<mainClass>,且要避免全局插件配置中存在未指定主类的repackage执行。示例配置如下:

<profiles>
    <profile>
        <id>dcr</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <!-- 明确指定当前Profile对应的主类 -->
                        <mainClass>com.yourpackage.dcr.DcrApplication</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>other</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.yourpackage.other.OtherApplication</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <goal>repackage</goal>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

注意:如果全局build/plugins中已经配置了spring-boot-maven-plugin,请移除其中的repackage执行,避免与Profile内的配置冲突。

3. 多Execution配置的正确姿势

若不用Profile,直接通过多Execution构建不同主类的Jar,必须满足两个条件:

  • 每个Execution设置唯一id
  • 每个Execution指定<classifier>避免Jar包覆盖,同时明确主类

示例配置:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>repackage-dcr</id>
            <configuration>
                <mainClass>com.yourpackage.dcr.DcrApplication</mainClass>
                <classifier>dcr</classifier>
            </configuration>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
        <execution>
            <id>repackage-other</id>
            <configuration>
                <mainClass>com.yourpackage.other.OtherApplication</mainClass>
                <classifier>other</classifier>
            </configuration>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

构建后会生成xxx-dcr.jar和xxx-other.jar两个独立的可执行Jar。

4. 验证配置是否生效

执行以下命令查看激活Profile后的有效POM,确认主类配置是否正确:

mvn help:effective-pom -P dcr

在输出中查找spring-boot-maven-plugin的<mainClass>节点,确认是否为你指定的类。

内容的提问来源于stack exchange,提问作者Simeon Leyzerzon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:56:25