Develop your own Maven plug-in 5: create a simple report plug-in

Source: Internet
Author: User

Now I want to develop a report plugin. I hope that the Hello world output by my report plugin can be integrated into the site generated by the MVN site.

1. Create a report plugin project using the Wizard:

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

2. In the automatically generated javadoc annotation of the mymojo class, modify it as follows:

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

3. Replace the parent class with abstractmavenreport.

Add the following dependency to 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>

The latest version numbers and detailed information of the previous two dependencies can be obtained through the following site:
Http://maven.apache.org/shared/index.html

You can find the abstractmavenreport document in the javadoc of Maven-reporting-impl Library:

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

Or directly refer to the source code:

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

4. Implement the excutereport instead of the execute method. The thrown exception is also changed to mavenreportexception.

The execute method has been implemented by the abstractmavenreport class, And executereport will be called internally when appropriate. What we need to implement is the executereport method.

In my executereport method, the sink is used to output a hello World string.

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

5. You also need to implement the getsiterender/getoutputdirectory/getproject/getoutputname/getname/getdescription method.

Refer to the complete code below:

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";    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.