Maven自定义插件简要流程

1. 新建一个Maven工程;
2. 修改POM打包方式为packing:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
me.richard
maven-plugin-suffix
1.0-SNAPSHOT
maven-plugin


3. 编写代码:

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

@Mojo(name = "suffix",defaultPhase = LifecyclePhase.PACKAGE)
public class MavenPlginSuffix extends AbstractMojo {
    public void execute() throws MojoExecutionException, MojoFailureException {
        System.out.println("This is zhaoy's first test plugin.");
    }
} 

4. 在需要使用的工程中引入,并指定对应的phase和goal:

<build>
    <finalName>learnjavafinalName>
    <plugins>
      <plugin>
        <groupId>me.richardgroupId>
        <artifactId>maven-plugin-suffixartifactId>
        <version>1.0-SNAPSHOTversion>
        <executions>
          <execution>
            <phase>packagephase>
            <goals>
              <goal>suffixgoal>
            goals>
          execution>
        executions>
      plugin>
    plugins>
  build>

5. 验证测试:

D:\ideaspace\learnjava>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building learnjava Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ learnjava ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ learnjava ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ learnjava ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\ideaspace\learnjava\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ learnjava ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ learnjava ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ learnjava ---
[INFO]
[INFO] --- maven-plugin-suffix:1.0-SNAPSHOT:suffix (default) @ learnjava ---
This is zhaoy's first test plugin.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.690 s
[INFO] Finished at: 2018-01-25T22:25:43+08:00
[INFO] Final Memory: 11M/167M
[INFO] ------------------------------------------------------------------------

是不是很简单呢。

你可能感兴趣的:(Maven)