簡體   English   中英

創建Maven報告插件

[英]Creating a Maven report plugin

我正在嘗試為Maven構建一個自定義報告插件,以便與mvn site一起使用。

但我找不到任何有關如何繼續的更新文檔。

有關創建插件的官方文檔提及擴展org.apache.maven.plugin.AbstractMojo 但這是關於通常的構建生命周期的“常規”插件。 它不適用於site構建生命周期。

在SO上有一個類似的問題( 編寫maven自定義報告插件;如何生成我的報告的html主體或“中間”? ),它引用了2015年的文檔,其中提到了AbstractMavenReport類而不是AbstractMojo類,但我找不到它在我的項目中導入的任何地方。

我還查看了一些最近報告插件的代碼(這里的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>

然后,您的主類必須擴展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 Reporting插件的未來開發人員! ;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM