Maven Plugin Development

Source: Internet
Author: User
Tags log log svn xmlns

Most computer language learning begins with Hello World, and we learn how to develop a custom Maven plugin by creating a Maven plugin that prints the Hello World string in the console.

Rapid development of the first plugin

1, create a maven-plugin directory, and enter this directory.

2, run the command:

MVN archetype:create-dgroupid=com.ailbaba.maven-dartifactid=maven-hello-plugin-darchetypeartifactid= Maven-archetype-mojo

You can see the new project, which pom.xml:

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" >

<modelVersion>4.0.0</modelVersion>

<groupId>com.alibaba.maven</groupId>

<artifactId>maven-hello-plugin</artifactId>

<packaging>maven-plugin</packaging>

<version>1.0-SNAPSHOT</version>

<name>maven-hello-plugin maven Mojo</name>

<url>http://maven.apache.org</url>

<dependencies>

<dependency>

<groupId>org.apache.maven</groupId>

<artifactId>maven-plugin-api</artifactId>

<version>2.0</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

</dependencies>

</project>

3, go to maven-hello-plugin directory, Run command: MVN eclipse:eclipse build Eclipse project.

4, through the Eclipse Import project, delete the automatically generated Java files under the package, the new Helloworldmojo.java,helloworldmojo.java content is as follows:

Package Com.alibaba.maven;

Import Org.apache.maven.plugin.AbstractMojo;

Import org.apache.maven.plugin.MojoExecutionException;

/**

* @goal HelloWorld

*/

public class Helloworldmojo extends Abstractmojo {

/**

* @parameter expression= "${helloworld.words}" default-value= "Hello world!"

*/

private String words;

public void execute () throws Mojoexecutionexception {

GetLog (). info (words);

}

}

5, after writing the file back to the cmd command line, run the install command at the project's Pom file directory to install the plugin to the local repository:mvn clean install

6, after the successful installation, continue to run the following command to see the plug-in operation:

Run:

MVN Com.alibaba.maven:maven-hello-plugin:1.0-snapshot:helloworld

, you can see the output from the console: "Hello world!" This output is the default parameter for plug-ins:

Default-value= "Hello world!"

Run

MVN com.alibaba.maven:maven-hello-plugin:1.0-snapshot:helloworld-dhelloworld.words= "welcome!"

Output can be seen in the console: "welcome!" This explicitly specifies the plug-in's parameters in the command, so the specified parameter "welcome!" is output.

Key points

Parameter injection of 1.maven

MAVEN's self-developed Plexus,plexus, which uses Javadoc to manage dependency injection, can see the above @goal, @parameter injected in this way, and now looks very primitive, but Maven3 will be migrated to Guice.

2.MOJO

MAVEN does the task by life cycle and goal, such as Maven install actually shifting a variety of goal. Each task in the plug-in is goal called a Mojo (Maven plain old Java Object). Every mojo in the project implements the Org.apache.maven.plugin.Mojo interface, the mojo of the plug-in example above implements the interface by extending the Org.apache.maven.plugin.AbstractMojo class. Mojo provides the following methods:

void Setlog (Org.apache.maven.monitor.logging.Log Log)

When Maven loads and runs Mojo, it calls the Setlog () method to provide the correct log target for the Mojo instance.

The Mojo interface only cares about two things: logging the results of the target run, and running a target. When writing a custom plugin, you need to extend the Abstractmojo. Abstractmojo handles the implementation of Setlog () and Getlog () and contains an abstract execute () method. When extending Abstractmojo, all you need to do is implement the Execute () method.

Company Maven-autoconfig-plugin Inquiry

In your local repository there will be the Maven AutoConfig plugin code, SVN address: http://svn.alibaba-inc.com/repos/ali_platform/maven/plugins/ Maven-autoconf-plugin/trunk, you can look at the core class Autoconfmojo, in fact, it is very simple:

/**

* Autoconf Mojo, EG:MVN autoconf:autoconf

*

* @goal autoconf

* @requiresProject False

*

* @author Shawn.qianx

* @author Hongyuan.zhouhy

* @author WILLIAM.LIANGF

*/

public class Autoconfmojo extends Abstractmojo {

/**

* Directory or Jar/war/zip archive path, separate by comma, default was current directory.

*

* @parameter expression= "${path}"

*/

Private String path;

public void execute () throws Mojoexecutionexception, Mojofailureexception {

GetLog (). info ("Execute autoconf:autoconf.");

Warconfigruntimeimpl runtime = new Warconfigruntimeimpl (system.in, System.out, System.err, CharSet);

if (path! = null && Path.trim (). Length () > 0) {

Runtime.setdests (Path.split (","));

} else {

Runtime.setdests (new string[] {"."});

}

if (charset = = null) {

charset = Configconstant.default_charset;

}

if (encoding = = null) {

encoding = Configconstant.default_charset;

}

if (interactive) {

GetLog (). info ("Turn on interactive mode.");

Runtime.setinteractivemode (configconstant.interactive_on);

}

if (GUI) {

Runtime.setguimode ();

}

if (verbose) {

Runtime.setverbose ();

}

if (Properties! = null && Properties.trim (). Length () > 0) {

GetLog (). info ("Customized User properties:" + Properties + "using encoding:" + charset);

Runtime.setuserpropertiesfile (properties, CharSet);

}

if (Userprop! = null && Userprop.trim (). Length () > 0) {

GetLog (). info ("Customized User properties:" + Userprop + "using Encoding:" + encoding);

Runtime.setuserpropertiesfile (Userprop, encoding);

}

if (sharedproperties! = null && sharedproperties.length () > 0) {

GetLog (). info ("Customized shared properties:" + descriptors);

Runtime.setsharedpropertiesfiles (Sharedproperties.split (","), Sharedpropertiesname, CharSet);

}

if ((descriptors! = null && descriptors.length () > 0)

|| (excludedescriptors! = null && excludedescriptors.length () > 0)) {

String[] includes;

String[] excludes;

Patternset patterns = Runtime.getdescriptorpatterns ();

if (descriptors = = NULL | | descriptors.length () = = 0) {

try {

Includes = Patterns.getincludes ();

} catch (NullPointerException NPE) {

Includes = new String[0];

}

} else {

GetLog (). info ("Customized include descriptors:" + descriptors);

Includes = Descriptors.split (",");

}

if (excludedescriptors = = NULL | | excludedescriptors.length () = = 0) {

try {

excludes = Patterns.getexcludes ();

} catch (NullPointerException NPE) {

excludes = new String[0];

}

} else {

GetLog (). info ("Customized exclude descriptors:" + excludedescriptors);

excludes = Excludedescriptors.split (",");

}

Runtime.setdescriptorpatterns (includes, excludes);

}

if ((packages! = null && packages.length () > 0)

|| (excludepackages! = null && excludepackages.length () > 0)) {

String[] includes;

String[] excludes;

Patternset patterns = Runtime.getpackagepatterns ();

if (packages = = NULL | | packages.length () = = 0) {

try {

Includes = Patterns.getincludes ();

} catch (NullPointerException NPE) {

Includes = new String[0];

}

} else {

GetLog (). info ("Customized include packages:" + descriptors);

Includes = Packages.split (",");

}

if (excludepackages = = NULL | | excludepackages.length () = = 0) {

try {

excludes = Patterns.getexcludes ();

} catch (NullPointerException NPE) {

excludes = new String[0];

}

} else {

GetLog (). info ("Customized Exclude packages:" + excludedescriptors);

excludes = Excludepackages.split (",");

}

Runtime.setpackagepatterns (includes, excludes);

}

try {

Runtime.start ();

} catch (Exception e) {

Runtime.error (e);

throw new Mojoexecutionexception ("Autoconf execution Error", E);

}

}

}

Summary

Developing a Maven plugin is to develop a mojo and then use them: Maven xx:xx.

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.