【问题标题】:Why can Maven plugin dependencies only be specified within <build> and not <reporting>?为什么 Maven 插件依赖项只能在 <build> 而不是 <reporting> 中指定?
【发布时间】:2019-05-11 15:11:29
【问题描述】:

为什么&lt;plugin&gt;&lt;dependencies&gt; 只能在&lt;build&gt; 部分中定义,而不能在pom&lt;reporting&gt; 部分中定义?

  • 为什么 maven pom.xml 语法在 &lt;reporting&gt; 中不允许 &lt;dependencies&gt;
    • 如果用户只想为&lt;reporting&gt; 配置插件并设置依赖版本怎么办?
  • &lt;build&gt; 依赖信息如何/为什么被 &lt;reporting&gt; 部分中的插件使用?

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

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

脚本&lt;build&gt; 部分中的插件可以覆盖默认依赖信息,这将影响&lt;reporting&gt; 部分中插件的依赖关系。因此,插件依赖信息不需要在&lt;reporting&gt;部分,只需在&lt;build&gt;部分。

这是正确的吗?文档中是否有说明这一点的地方?为了正确理解 &lt;dependencies&gt;&lt;build&gt;&lt;reporting&gt; 插件配置之间的关系,我遗漏了哪些细节?

来自 Maven 文档

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

使用&lt;reporting&gt; 标签 VS &lt;build&gt; 标签
在 pom 中的 &lt;reporting&gt;&lt;build&gt; 元素中配置报告插件没有相同的行为!

mvn site
它仅使用在&lt;reporting&gt; 元素中指定的每个报告插件的&lt;configuration&gt; 元素中定义的参数,即站点始终忽略在&lt;build&gt; 中指定的每个插件的&lt;configuration&gt; 元素中定义的参数。

文档明确指出&lt;configuration&gt; 不在&lt;build&gt;&lt;reporting&gt; 之间共享,但是 我的问题是关于 &lt;dependencies&gt; 以及它们如何/为什么只在 &lt;build&gt; 中声明,而不是在 &lt;reporting&gt; 中声明。

似乎&lt;build&gt; do 中指定的依赖项会延续到&lt;reporting&gt; 插件。但这是我想要确认/解释的一点。

小例子

我遇到了这个问题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>

【问题讨论】:

    标签: maven checkstyle maven-checkstyle-plugin


    【解决方案1】:

    我想说这里的情况不是那么简单 - 因为&lt;dependencies&gt; 可以在&lt;reporting&gt; 部分!

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

    但首先&lt;build&gt;&lt;reporting&gt;有什么区别:

    • &lt;build&gt; 用于编译、测试期间 - 即在生成/分析/运行代码期间 - 与 mvn clean install 类似

    • &lt;reporting&gt; 在生成文档时使用mvn site

    所以问题是 checkstyle 插件在文档生成期间是否需要您的依赖项 - 我猜不是 - 所以 checkstyle 拒绝 &lt;reporting&gt; 中的所有依赖项。

    所以要证明报告中的依赖关系是可能的,请查看 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>
    

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

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

    【讨论】:

      猜你喜欢
      • 2015-10-26
      • 2012-07-07
      • 2011-07-03
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多