可以在父项目中使用 <pluginManagement> 标签来管理插件的版本和配置,而在子项目中不使用该标签,这样就可以保证插件只在父项目中执行而不在子项目中执行。

例如,以下是一个父项目的 pom.xml 文件的示例:

<project>
  <groupId>com.example</groupId>
  <artifactId>parent-project</artifactId>
  <version>1.0.0</version>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.1</version>
          <configuration>
            <!-- 配置编译器插件 -->
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <modules>
    <module>child-project</module>
  </modules>
</project>

在子项目的 pom.xml 文件中,可以直接引用父项目中的插件,例如:

<project>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
  </parent>

  <groupId>com.example</groupId>
  <artifactId>child-project</artifactId>
  <version>1.0.0</version>

  <build>
    <plugins>
      <!-- 不需要配置编译器插件,直接使用父项目中的配置 -->
    </plugins>
  </build>
</project>

这样,编译器插件就只会在父项目中执行,而不在子项目中执行


原文地址: https://www.cveoy.top/t/topic/fdP8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录