开发自己的Maven插件之五:创建简单的report plugin

现在我来开发一个report plugin,我希望我的report plugin输出的hello world能够被集成到mvn site产生的站点中。


1.用向导创建一个report plugin工程:

mvn archetype:create -DgroupId=org.freebird -DartifactId=myreport -DarchetypeArtifactId=maven-archetype-mojo


2.在自动生成的类MyMojo类的javadoc annotation中,适当修改为如下:

/**
 * Goal which touches a timestamp file.
 *
 * @goal my-report
 * 
 * @phase site
 */
public class MyMojo
    extends AbstractMojo
{

3.替换父类为AbstractMavenReport

需要在pom.xml中添加以下依赖:

    <dependency>
      <groupId>org.apache.maven.reporting</groupId>
      <artifactId>maven-reporting-api</artifactId>
      <version>3.0</version>
    </dependency>
    
    <dependency>
      <groupId>org.apache.maven.reporting</groupId>
      <artifactId>maven-reporting-impl</artifactId>
      <version>2.2</version>
    </dependency>
    
    <dependency>
      <groupId>org.codehaus.plexus</groupId>
      <artifactId>plexus-utils</artifactId>
      <version>2.1</version>
    </dependency>
前面两个依赖的最新版本号以及详细信息可以通过下面这个站点获取:
http://maven.apache.org/shared/index.html

你可以在maven-reporting-impl库的javadoc中找到AbstractMavenReport的文档:

http://maven.apache.org/shared/maven-reporting-impl/apidocs/index.html

或者直接参考源代码:

http://maven.apache.org/shared/maven-reporting-impl/xref/index.html



4.实现excuteReport而不是execute方法,抛出的exception也变成了MavenReportException

execute方法已经被AbstractMavenReport类实现,内部会在合适的时候调用executeReport,我们需要实现的就是executeReport方法。

我的executeReport方法里面就是利用Sink输出一个hello world字符串。

    @Override
    protected void executeReport(Locale locale) throws MavenReportException {
        Sink sink = getSink();
        sink.paragraph();
        sink.text("hello world");
        sink.paragraph();
    }



5.还需要实现getSiteRender/getOutputDirectory/getProject/getOutputName/getName/getDescription方法

参考下面的完整代码:

package org.freebird;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
import java.io.File;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import java.util.Locale;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.project.MavenProject;

/**
 * Goal which touches a timestamp file.
 *
 * @goal my-report
 *
 * @phase site
 */
public class Report1 extends AbstractMavenReport {

    private Renderer siteRenderer;
    
    @Override
    protected Renderer getSiteRenderer() {
        return siteRenderer;
    }
    /**
     * The output directory.
     *
     * @parameter
     * expression="${project.build.directory}/generated-sources/myreport"
     * @required
     */
    private File outputDirectory;

    @Override
    protected String getOutputDirectory() {
        return outputDirectory.getAbsolutePath();
    }
    
    private MavenProject project;

    @Override
    protected MavenProject getProject() {
        return project;
    }

    @Override
    protected void executeReport(Locale locale) throws MavenReportException {
        Sink sink = getSink();
        sink.paragraph();
        sink.text("hello world");
        sink.paragraph();
    }

    public String getOutputName() {
        return "myreport-output";
    }

    public String getName(Locale locale) {
        return "myreport-name";
    }

    public String getDescription(Locale locale) {
        return "myreport-description";
    }
}







你可能感兴趣的:(开发自己的Maven插件之五:创建简单的report plugin)