Develop one of your Maven plug-ins: Hello World

Source: Internet
Author: User

Maven has been used for a long time and various plug-ins are used. However, if you do not have any plug-ins, you still need to write them by yourself. Writing a plug-in is not difficult. Another advantage of writing a plug-in is to learn more about the maven working mechanism. It is helpful for better use of Maven.

Create a Maven project named plugin-example1 first.

The term mojo is Maven plain old Java object, which is a common Java class.
We need the mojo api library, so add a dependency in POM. xml:

  <dependencies>    <dependency>      <groupId>org.apache.maven</groupId>      <artifactId>maven-plugin-api</artifactId>      <version>2.0</version>    </dependency>  </dependencies>

Create an example class that inherits from abstractmojo and implements the execute method.
The code is simple:

public class Example extends AbstractMojo{    public void execute() throws MojoExecutionException, MojoFailureException {        getLog().info("Hello world");    }    }

Getlog () obtains the internal log of abstractmojo. The type is org. Apache. Maven. plugin. Logging. log;
At least in Mojo development, do not use other log infrastructure.

Modify the description of the project:

  <groupId>org.freebird</groupId>  <artifactId>plugin-example1</artifactId>  <version>1.0-SNAPSHOT</version>  <packaging>maven-plugin</packaging>  <name>plugin-example1</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>

Note that the value of packaging is Maven-plugin.

Compile it now and you will soon encounter an error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:2.9:descriptor (default-descriptor) on project plugin-example1: Error extracting plugin descriptor: 'No mojo definitions were found for plugin: org.freebird:plugin-example1.' -> [Help 1]org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:2.9:descriptor (default-descriptor) on project plugin-example1: Error extracting plugin descriptor: 'No mojo definitions were found for plugin: org.freebird:plugin-example1.'

You need to add a Maven-plugin to generate a descriptor. I don't know why it is not mentioned in Maven official documentation.

  <build>    <plugins>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-plugin-plugin</artifactId>        <version>3.0</version>        <executions>        </executions>        <configuration>          <!-- Needed for Java 5 annotation based configuration, for some reason. -->          <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>        </configuration>      </plugin>    </plugins>  </build>

In this way, the compilation is successful. Or add a descriptor to the class watching:

/** * * @goal sayhi */public class Example extends AbstractMojo{

To deploy maven on a private server, add the following Configuration:

  <distributionManagement>    <snapshotRepository>      <id>snapshots</id>      <url>http://your_server:8080/nexus/content/repositories/snapshots</url>    </snapshotRepository>  </distributionManagement>

Then run MVN clean package deploy

Deployed successfully.

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.