我正在尝试为 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/