1.新建maven插件开发工程
2.修改pom文件内容为如下:
<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>org.example</groupId> <artifactId>demo-maven-plugin</artifactId> <packaging>maven-plugin</packaging> <version>1.0-SNAPSHOT</version> <name>demo-maven-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> <dependency> <groupId>org.apache.maven.plugin-tools</groupId> <artifactId>maven-plugin-annotations</artifactId> <version>3.5.2</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-plugin-plugin</artifactId> <version>3.5.2</version> </plugin> </plugins> </build> </project>
3.新建demo类
package org.example; /* * 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 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; /** * @author buhao * @version DemoMojo.java, v 0.1 2020-03-30 22:51 buhao */ @Mojo(name = "hello") public class DemoMojo extends AbstractMojo { @Parameter(name = "name", defaultValue = "kiwi") private String name; public void execute() throws MojoExecutionException, MojoFailureException { getLog().info("hello " + name); } }
4.将插件开发后的jar包添加到工程pom文件中
<plugin> <groupId>org.example</groupId> <artifactId>demo-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> <configuration> <name>你好</name> </configuration> </plugin>
4.idea右边点击maven进行测试
5.测试结果如下:
6.总结
必须继承 AbstractMojo 该类重写execute类
版权声明:本文为ruanjianchenming原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。