maven plugin 简单介绍和实战 (2021-05-29)
Posted master-dragon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了maven plugin 简单介绍和实战 (2021-05-29)相关的知识,希望对你有一定的参考价值。
maven plugin 开发: https://maven.apache.org/plugin-developers/index.html
什么是plugin?
“Maven” is really just a core framework for a collection of Maven Plugins. In other words, plugins are where much of the real action is performed, plugins are used to: create jar files, create war files, compile code, unit test code, create project documentation, and on and on. Almost any action that you can think of performing on a project is implemented as a Maven plugin.
-
plugin的作用
- create jar files (创建jar)
- create war files(创建war)
- compile code (编译源代码)
- unit test code (单元测试)
- create project documentation (创建项目文档)
- on and on(等等…)
什么是Mojo?
A Mojo is really just a goal in Maven, and plug-ins consist of any number of goals (Mojos). Mojos can be defined as annotated Java classes or Beanshell script. A Mojo specifies metadata about a goal: a goal name, which phase of the lifecycle it fits into, and the parameters it is expecting.
MOJO is a play on POJO (Plain-old-Java-object), substituting “Maven” for “Plain”. Mojo is also an interesting word (see definition). From Wikipedia, a “mojo” is defined as: “…a small bag worn by a person under the clothes (also known as a mojo hand). Such bags were thought to have supernatural powers, such as protecting from evil, bringing good luck, etc.”
mojo理解为一个执行目标,maven本身就是很多mojo组成的。mojo实现plugin,让用户自已在compile
,process-classed
,test
,package
,install
, deploy
等阶段实现特定的执行逻辑
maven plugin 的几个标准阶段
阶段 | 含义 |
---|---|
compile | Compiles the Java code for the plugin |
process-classes | Extracts data to build the plugin descriptor |
test | Runs the plugin’s unit tests |
package | Builds the plugin jar |
install | Installs the plugin jar in the local repository |
deploy | Deploys the plugin jar to the remote repository |
@Mojo的使用参考:https://maven.apache.org/developers/mojo-api-specification.html#The_Descriptor_and_Annotations
简单的定义plugin和使用plugin
如下就能简单的定义一个plugin的逻辑
package sample.plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Says "Hi" to the user.
*
*/
@Mojo( name = "sayhi")
public class GreetingMojo extends AbstractMojo
{
public void execute() throws MojoExecutionException
{
getLog().info( "Hello, world." );
}
}
plugin的pom依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.maven/maven-plugin-api -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugin-tools/maven-plugin-annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
在另一个工程模块中的pom.xml引入,然后就能使用该plugin了
<build>
<plugins>
<plugin>
<groupId>org.example</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
</plugin>
</plugins>
</build>
参考:https://maven.apache.org/guides/plugin/guide-java-report-plugin-development.html
plugin重要功能之parameter
- 可以自定义一些丰富的parameter:比如做生成jar和后续解析jar的操作,来适应丰富的变化。
关于参数的使用介绍:https://maven.apache.org/guides/plugin/guide-java-plugin-development.html#parameters
It is unlikely that a mojo will be very useful without parameters. Parameters provide a few very important functions:
- It provides hooks to allow the user to adjust the operation of the plugin to suit their needs.
- It provides a means to easily extract the value of elements from the POM without the need to navigate the objects.
使用例子:
/**
* The greeting to display.
*/
@Parameter( property = "sayhi.greeting", defaultValue = "Hello World!" )
private String greeting;
<plugin>
<groupId>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<!--定义greeting参数的值-->
<greeting>Welcome</greeting>
</configuration>
</plugin>
需要说明的是,可以在plugin中使用getLog()
来打印 log 已方便调试或观察plugin执行阶段情况
parameter的类型是丰富的,eg:
boolean、Numbers、Dates、Files and Directories、URLs、Plain Text、Enums,Types With Multiple Values、
Other Object Classes
即我们可以自己定义参数类型来极大的方便了我们在构建jar的灵活性
指定plugin执行的阶段
例如指定默认deploy
阶段完成某个Mojo:
@Mojo(name = "deploy", defaultPhase = LifecyclePhase.DEPLOY,
requiresDependencyResolution = ResolutionScope.RUNTIME)
public class DeployMojo extends AbstractMojo {
//...
}
参考:https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
实际例子编写
plugin 编写
package org.example;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Says "Hi" to the user.
*
*/
@Mojo( name = "sayhi")
public class GreetingMojo extends AbstractMojo
{
public void execute() throws MojoExecutionException
{
getLog().info( "plugin greeting" );
}
}
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>org.example</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.maven/maven-plugin-api -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugin-tools/maven-plugin-annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
然后mvn clean install -Dmaven.test.skip=true
安装到本地
plugin使用
工程的pom.xml引入上述plugin
<?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>org.example</groupId>
<artifactId>mvn-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.maven/maven-plugin-api -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugin-tools/maven-plugin-annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.example</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<phase>clean</phase>
<goals>
<goal>sayhi</goal>
</goals>
<configuration>
<tasks>
<echo>clean phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
通过mvn clean -Dmaven.test.skip=true
就能在clean阶段执行
以上是关于maven plugin 简单介绍和实战 (2021-05-29)的主要内容,如果未能解决你的问题,请参考以下文章
Jboot使用appassembler-maven-plugin插件生成启动脚本
Maven实战技巧「插件使用专题」Maven-Archetype插件创建自定义maven项目骨架