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

如何让Spring Boot项目继承独立仓库中Maven模块的插件配置

如何让Spring Boot项目继承独立仓库中Maven模块的插件配置

我来帮你梳理下问题所在,以及具体的解决步骤——你之前的尝试没成功,核心是没摸透Maven插件配置的继承规则,还有Spring Boot项目多父pom的特殊处理方式。

先明确核心逻辑

Maven的插件配置默认不会像依赖那样自动跨项目传递:

  • 只有放在<pluginManagement>里的插件配置,才会被继承到其他项目的配置体系中;
  • 直接放在<plugins>里的插件,只会在当前模块生效,非直接子项目拿不到对应的配置;
  • 另外Spring Boot项目已经自带了spring-boot-starter-parent作为父pom,Maven不支持多个直接父pom,得用import方式引入自定义的pom配置。

具体解决步骤

步骤1:完善my-check父pom的模块声明

首先确保my-check的根pom里已经声明了子模块,否则Maven无法识别my-archunit-plugin-config,也就没法把它安装到仓库供其他项目引用。修改my-check/pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.plugin</groupId>
    <artifactId>my-check</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>

    <!-- 必须添加这个,声明子模块 -->
    <modules>
        <module>my-archunit-plugin-config</module>
    </modules>
</project>

步骤2:调整my-archunit-plugin-config的插件配置位置

把插件从<build><plugins>移到<build><pluginManagement>里,这样其他项目继承时才能完整拿到配置规则。修改my-archunit-plugin-config/pom.xml:

<parent>
    <groupId>com.plugin</groupId>
    <artifactId>my-check</artifactId>
    <version>0.0.1</version>
</parent>

<groupId>com.plugin</groupId>
<artifactId>my-archunit-plugin-config</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.societegenerale.commons</groupId>
                <artifactId>arch-unit-maven-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <excludedPaths>
                        <!-- exclude cds gen path -->
                        <excludedPath>${basedir}/src/gen/java/cds/gen</excludedPath>
                        <excludedPath>${basedir}/src/test</excludedPath>
                    </excludedPaths>
                    <rules>
                        <!-- using a rule available out of the box... -->
                        <preConfiguredRules>
                            <rule>com.societegenerale.commons.plugin.rules.NoStandardStreamRuleTest</rule>
                        </preConfiguredRules>
                        <configurableRules>
                            <configurableRule>
                                <rule>com.rules.RuleTest1</rule>
                                <applyOn>
                                    <packageName>com.factory</packageName>
                                    <!-- scope can be "main" or "test" -->
                                    <scope>test</scope>
                                </applyOn>
                            </configurableRule>
                        </configurableRules>
                    </rules>
                </configuration>
                <executions>
                    <execution>
                        <phase>test</phase>
                        <goals>
                            <goal>arch-test</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.plugins.commons</groupId>
                        <artifactId>commons-archunit</artifactId>
                        <version>2.0.1</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

步骤3:在my-bookshop中引入并使用插件

因为my-bookshop是Spring Boot项目,已经有spring-boot-starter-parent作为父pom,所以我们用dependencyManagement的import方式引入my-check,然后只需声明插件即可继承所有配置。修改my-bookshop/pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>你的Spring Boot版本号</version>
    <relativePath/> <!-- 从仓库查找父pom -->
</parent>

<groupId>你的项目groupId</groupId>
<artifactId>my-bookshop</artifactId>
<version>你的项目版本号</version>
<name>my-bookshop</name>

<!-- 引入my-check的pom,继承其pluginManagement配置 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.plugin</groupId>
            <artifactId>my-check</artifactId>
            <version>0.0.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <!-- 只需声明插件,所有配置会从my-archunit-plugin-config继承 -->
        <plugin>
            <groupId>com.societegenerale.commons</groupId>
            <artifactId>arch-unit-maven-plugin</artifactId>
        </plugin>
        <!-- Spring Boot默认插件保留 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

步骤4:安装my-check到本地仓库

在my-check项目的根目录下执行命令,把项目安装到本地Maven仓库,这样my-bookshop就能正常引用到:

mvn clean install

验证效果

现在打开IntelliJ的my-bookshop项目,查看右侧Maven面板的Plugins,应该能看到arch-unit-maven-plugin了。执行mvn test时,这个插件会自动在test阶段运行,并且完全使用你定义的规则和配置。

备注:内容来源于stack exchange,提问作者M.R.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.21 07:58:04