maven插件开发和调试

maven插件开发

官方文档 http://maven.apache.org/plugin-developers/index.html

1.新建maven工程

pom.xml参考:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mxy.maven</groupId>
    <artifactId>sample-maven-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--打包方式:-->
    <packaging>maven-plugin</packaging>

    <name>Sample Parameter-less Maven Plugin</name>

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

        <!-- dependencies to annotations -->
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.5</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>


           <!--插件使用,一般是其他工程使用-->
            <plugin>
                <groupId>com.mxy.maven</groupId>
                <artifactId>sample-maven-plugin</artifactId>
                <version>1.0-SNAPSHOT</version>
                <configuration>
                    <msg>message input</msg>
                    <lists>
                        <list1>first</list1>
                        <list1>second</list1>
                    </lists>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>sayhi</goal>
                            <goal>hello</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <repositories>
        <repository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
        <repository>
            <id>mvnrepository</id>
            <name>mvnrepository</name>
            <url>https://mvnrepository.com/artifact/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>mvnrepository</id>
            <name>mvnrepository</name>
            <url>https://mvnrepository.com/artifact/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

2.创建java文件(goal)(两种不同方式)

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

import java.util.List;

// 在处理源码的时候,plugin-tools 会把使用了 @Mojo
// 注解或 Javadoc 里包含 @goal 注释的类来当作一个 Mojo 类。
///**
// * @goal hello
// */
@Mojo(name = "hello")
public class HelloMojo extends AbstractMojo {

    @Parameter private String msg;

    @Parameter private List<String> lists;

//    可以通过configuration配置,也可以通过-DinlineArgs=inlineInput传入参数值。
    @Parameter(property = "inlineArgs") private String inlineArgs;

    public void execute() throws MojoExecutionException, MojoFailureException {
        System.out.println(String.format("hello world msg->%s, lists->%s, inlineArgs->%s ", msg, lists, inlineArgs));

    }

}

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

/**
 * Says "Hi" to the user. 扫描@goal方式
 */
/**
 * @goal sayhi
 */
public class GreetingMojo extends AbstractMojo {
    public void execute() throws MojoExecutionException {
        getLog().info("Hello, world.");
    }
}

3.打包

mvn clean install

4.运行

直接运行指定插件:

mvn com.mxy.maven:sample-maven-plugin:1.0-SNAPSHOT:hello
  • 符合-maven-plugin命名规范,可以简化运行
mvn sample:hello -DinlineArgs=inlineInput

绑定在maven生命周期中运行,这个地方我们绑定在package中

mvn clean package

调试

调试时候分为两个工程,一个为插件源码工程A,一个为使用插件的工程B。

这个地方简化逻辑,A和B都是本工程。

工程B控制台执行mvnDebug clean package
工程A配置远程连接。这里以idea为例,配置如下

image

猜你喜欢

转载自blog.csdn.net/qq_30062125/article/details/82984193