为什么 Maven 插件依赖项只能在 <build> 内指定,而不能在 <reporting> 内指定?

Why can Maven plugin dependencies only be specified within <build> and not <reporting>?

为什么 <plugin><dependencies> 只能在 <build> 部分定义,而不是 pom<reporting> 部分?

找到的文档,我在下面解释了为什么它没有回答这个问题(文档的混乱实际上是我在这里问这个问题的原因!) .

根据我阅读、观察和尝试的内容,这是我目前的理解:

Plugins in the <build> section of the script can override default dependency information, and that will affect the dependencies of the plugin in the <reporting> section. Therefore, plugin dependency information does not need to be in the <reporting> section, only the <build> section.

这是正确的吗?文档中是否有一个地方可以澄清这一点?为了正确理解 <dependencies><build><reporting> 插件配置之间的关系,我缺少哪些细节?

来自 Maven 文档

它在 Maven 文档中说 Using the Reporting vs the Build Tag:

Using the <reporting> Tag VS <build> Tag
Configuring a reporting plugin in the <reporting> or <build> elements in the pom does NOT have the same behavior!

mvn site
It uses only the parameters defined in the <configuration> element of each reporting Plugin specified in the <reporting> element, i.e. site always ignores the parameters defined in the <configuration> element of each plugin specified in <build>.

文档明确指出 <configuration> 不在 <build><reporting> 之间共享,但是 我的问题是关于 <dependencies> 和 how/why 它们只会在 <build> 中声明,而不会在 <reporting>.

中声明

似乎 <build> do 中指定的依赖关系会转移到 <reporting> 插件。但这是我想要 confirmation/explanation 的一点。

最小示例

我遇到这个问题 upgrading the dependencies for the CheckStyle plugin at runtimemvn site 一起使用,所以这个最小的 POM 示例以 Checkstyle 插件为例演示了这个问题。

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>mylib</artifactId>
  <version>1.0</version>

  <build> 
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>3.0.0</version>
            <dependencies>
                <dependency>
                    <groupId>com.puppycrawl.tools</groupId>
                    <artifactId>checkstyle</artifactId>
                    <version>8.15</version> <!-- Update from default 6.18 to 8.15 -->
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
  </build>

  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>3.0.0</version>

        <!-- Uncommenting will cause syntax error, Dependencies can't be declared in reporting -->
        <!-- <dependencies>
          <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>8.15</version>
          </dependency>
        </dependencies> --> 

      </plugin>
    </plugins>
  </reporting>
</project>

我想说这里的情况并不那么简单 - 因为 <dependencies><reporting> 部分是可能的!

我认为关键在于插件本身(因此每个插件可能有所不同)。

但起初<build><reporting>有什么区别:

  • <build> 在编译、测试期间使用 - 即在 generating/analyzing/running 代码期间 - 就像 mvn clean install

  • <reporting> 在使用 mvn site

  • 生成文档期间使用

所以问题是 checkstyle 插件在文档生成期间是否需要您的依赖项 - 我想不需要 - 所以 checkstyle 拒绝 <reporting>.

中的所有依赖项

因此,为了证明报告中的依赖关系是可能的,请查看 spotbugs(另一个著名的分析器):

<build>
  <plugins>
    <plugin>
      <groupId>com.github.spotbugs</groupId>
      <artifactId>spotbugs-maven-plugin</artifactId>
      <version>3.1.12.1</version>
      <configuration>
        <xmlOutput>true</xmlOutput>
        <spotbugsXmlWithMessages>true</spotbugsXmlWithMessages>
        <spotbugsXmlOutputDirectory>target/site</spotbugsXmlOutputDirectory>
        <failOnError>false</failOnError>
        <includeTests>true</includeTests>
        <dependencies>
          <dependency>
            <groupId>com.mebigfatguy.fb-contrib</groupId>
            <artifactId>fb-contrib</artifactId>
            <version>7.4.3.sb</version>
          </dependency>

          <plugin>
            <groupId>com.h3xstream.findsecbugs</groupId>
            <artifactId>findsecbugs-plugin</artifactId>
            <version>LATEST</version>
          </plugin>
        </dependencies>
      </configuration>
    </plugin>
  </plugins>
</build>


<reporting>
  <plugins>
    <plugin>
      <groupId>com.github.spotbugs</groupId>
      <artifactId>spotbugs-maven-plugin</artifactId>
      <version>3.1.12.1</version>
      <configuration>
        <xmlOutput>true</xmlOutput>
        <spotbugsXmlWithMessages>true</spotbugsXmlWithMessages>
        <spotbugsXmlOutputDirectory>target/site</spotbugsXmlOutputDirectory>
        <failOnError>false</failOnError>
        <includeTests>true</includeTests>
        <dependencies>
          <dependency>
            <groupId>com.mebigfatguy.fb-contrib</groupId>
            <artifactId>fb-contrib</artifactId>
            <version>7.4.3.sb</version>
          </dependency>

          <plugin>
            <groupId>com.h3xstream.findsecbugs</groupId>
            <artifactId>findsecbugs-plugin</artifactId>
            <version>LATEST</version>
          </plugin>

        </dependencies>
      </configuration>
    </plugin>
  </plugins>
</reporting>

请注意这个例子 - 因为这里 <dependencies><configuration> 的一部分。所以仔细阅读每个插件的文档总是明智的。

要获得完整的 maven pom.xml,请查看我的小型 OpenSource TemplateEngine,其中包含许多不仅适用于 maven 的最佳实践。