maven - 创建 Maven 报告插件

标签 maven maven-3 maven-plugin

我正在尝试为 Maven 构建一个自定义报告插件,用于 mvn site .

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

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

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

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

至少没有报告插件的原型(prototype)吗?有人有这方面的经验吗?

谢谢!
——伯特兰

最佳答案

多一点挖掘,我找到了答案:
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>

然后,你的主类必须扩展 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 报告插件的 future 开发人员! ;-)

关于maven - 创建 Maven 报告插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45796023/

相关文章:

java - 从Main迁移到Gradle时未发现主类错误

java - Maven 或不 Maven

windows - 需要将 Maven 的 ${project.basedir} 从单反斜杠转换为双斜杠

maven - 发布到 Maven Central

maven-2 - Sonar maven插件无法下载JDBC驱动程序

java - GWT 类类似于 AWT 桌面?

json - org.json.simple.JSONObject VS org.json.JSONObject , JSONException 无法解析为类型

maven - 哪个 grails-maven-plugin 版本与 Maven 3.0.4 兼容?

maven - 如何使用依赖库生成 JAR?

java - 如何在公共(public)pom中保留版本号并在其他pom文件中使用它(spring maven项目)