sunny906 2025-02-21 17:52 采纳率: 80%
浏览 3
已结题

模块之间plugin如何共享

模块A的pom.xml:

    <build>
          <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

模块B的pom.xml:

   <dependencies>
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>A</artifactId>
        </dependency>
    </dependencies>

模块A与模块B不是父子关系。在模块B里添加A的依赖之后,A的plugin并没有像dependency一样被引入到模块B中,如何才能使A的plugin被自动引入到模块B里呢?

  • 写回答

3条回答 默认 最新

  • 檀越@新空间 2025-02-21 17:56
    关注

    下午好🌅🌅🌅
    本答案参考通义千问

    在Maven项目中,插件(plugins)配置不会像依赖(dependencies)那样自动继承或传递。因此,如果你希望模块B能够使用模块A中的插件配置,你需要显式地在模块B的pom.xml文件中声明该插件。

    下面是解决这个问题的步骤:

    1. 在模块A中定义插件配置: 确保模块A中的pom.xml已经正确配置了插件。这已经在你的问题描述中给出了。

    2. 将插件配置提取到一个单独的POM文件中: 你可以将插件配置提取到一个单独的POM文件中,然后让模块A和模块B都引用这个配置文件。

    3. 在模块B中引用插件配置: 在模块B的pom.xml文件中引用模块A中的插件配置。

    以下是具体的实现步骤:

    步骤1:创建一个包含插件配置的POM文件

    假设我们创建一个名为generator-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.test</groupId>
        <artifactId>generator-config</artifactId>
        <version>1.0.0</version>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    步骤2:在模块A的pom.xml中引用这个配置文件

    在模块A的pom.xml中,通过<pluginManagement>标签来引用这个配置文件:

    <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.test</groupId>
        <artifactId>A</artifactId>
        <version>1.0.0</version>
    
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>com.test</groupId>
                        <artifactId>generator-config</artifactId>
                        <version>1.0.0</version>
                        <type>pom</type>
                        <scope>import</scope>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>
    

    步骤3:在模块B的pom.xml中引用插件配置

    在模块B的pom.xml中,引用模块A的插件配置:

    <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.test</groupId>
        <artifactId>B</artifactId>
        <version>1.0.0</version>
    
        <dependencies>
            <dependency>
                <groupId>com.test</groupId>
                <artifactId>A</artifactId>
                <version>1.0.0</version>
            </dependency>
        </dependencies>
    
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>com.test</groupId>
                        <artifactId>generator-config</artifactId>
                        <version>1.0.0</version>
                        <type>pom</type>
                        <scope>import</scope>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>
    

    总结

    通过上述步骤,我们可以确保模块B能够访问到模块A中的插件配置。关键在于将插件配置提取到一个单独的POM文件中,并在模块A和模块B的pom.xml中引用这个配置文件。这样可以避免直接复制粘贴相同的插件配置,保持项目的整洁和可维护性。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月21日
  • 已采纳回答 2月21日
  • 修改了问题 2月21日
  • 创建了问题 2月21日