创建 Maven 报告插件

Creating a Maven report plugin

我正在尝试为 Maven 构建自定义报告插件,以与 mvn site 一起使用。

但我找不到关于如何继续的任何更新文档。

关于创建插件的官方文档提到扩展 org.apache.maven.plugin.AbstractMojo。但这是关于通常构建生命周期的 "regular" 插件。它不适用于 site 构建生命周期。

SO (Writing a maven custom report plugin; how do I generate the html body or "middle" of my report?) 上有一个类似的问题,它引用了 2015 年的一份文件,其中提到 AbstractMavenReport class 而不是 AbstractMojo class,但我无法在任何地方找到它以导入到我的项目中。

我也看了一些最近的报告插件的代码(这里的changes插件:http://svn.apache.org/viewvc/maven/plugins/tags/maven-changes-plugin-2.12.1/),但是我找不到我要找的东西。

至少有一个报告插件的原型吗?有人有这方面的经验吗?

谢谢! -- 伯特兰

再深入一点,我找到了答案: http://maven.apache.org/shared/maven-reporting-impl/index.html

还有一个工作示例: http://svn.apache.org/viewvc/maven/shared/tags/maven-reporting-impl-3.0.0/src/it/setup-reporting-plugin/

所以,基本上,您需要在 pom.xml:

  <dependencies>
    <dependency>
      <groupId>org.apache.maven.reporting</groupId>
      <artifactId>maven-reporting-impl</artifactId>
      <version>@project.version@</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.reporting</groupId>
      <artifactId>maven-reporting-api</artifactId>
      <version>3.0</version>
    </dependency>

    <!-- plugin API and plugin-tools -->
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>3.0.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>3.3</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.maven.shared</groupId>
      <artifactId>maven-shared-utils</artifactId>
      <version>3.2.0</version>
    </dependency>
  </dependencies>

然后,您的主要 class 必须扩展 AbstractMavenReport:

import java.util.Locale;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;

/**
 * Typical code to copy as a reporting plugin start: choose the goal name, then implement getOutputName(),
 * getName( Locale ), getDescription( Locale ) and of course executeReport( Locale ).
 */
@Mojo( name = "custom" )
public class CustomReport
    extends AbstractMavenReport
{
    public String getOutputName()
    {
        return "custom-report";
    }

    public String getName( Locale locale )
    {
        return "Custom Maven Report";
    }

    public String getDescription( Locale locale )
    {
        return "Custom Maven Report Description";
    }

    @Override
    protected void executeReport( Locale locale )
        throws MavenReportException
    {
        // direct report generation using Doxia: compare with CustomReportRenderer to see the benefits of using
        // ReportRenderer
        getSink().head();
        getSink().title();
        getSink().text( "Custom Report Title" );
        getSink().title_();
        getSink().head_();

        getSink().body();

        getSink().section1();
        getSink().sectionTitle1();
        getSink().text( "section" );
        getSink().sectionTitle1_();

        getSink().text( "Custom Maven Report content." );
        getSink().section1_();

        getSink().body_();
    }
}

希望这对未来的 Maven 报告插件开发人员有所帮助! ;-)