# first-maven-plugin
**Repository Path**: devzl/first-maven-plugin
## Basic Information
- **Project Name**: first-maven-plugin
- **Description**: Custom Maven Plug-in
- **Primary Language**: Unknown
- **License**: MulanPSL-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-08-25
- **Last Updated**: 2022-08-25
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
### 2.6 自定义插件
[自定义插件项目]: F:\Java\unit-case-2022\test-maven\05-maven-project
#### 1. 设定打包方式
```xml
maven-plugin
```
#### 2.引入依赖
> 方式一:将来在文档注释中使用注解
```xml
org.apache.maven
maven-plugin-api
3.5.2
```
> 方式二:将来直接使用注解
```xml
org.apache.maven.plugin-tools
maven-plugin-annotations
3.5.2
```
#### 3.创建 Mojo 类
Mojo 类是一个 Maven 插件的核心类。
Mojo 这个单词的意思是:Maven Old Java Object,其实 mojo 这个单词本身包含魔力;符咒(袋);护身符;(人的)魅力的含义,Maven 用 Mojo 是因为它是对 POJO 开的一个小玩笑。
> 方式一:Mojo 接口
每一个 Mojo 都需要实现 org.apache.maven.plugin.Mojo 接口。

> 方式二:AbstractMojo 抽象类
我们实现 Mojo 接口比较困难,幸好可以继承 AbstractMojo,此时我们只要实现 execute() 这一个方法即可。
```java
public class MyHelloPlugin extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("---> This is my first maven plugin. <---");
}
}
```
#### 4.在setting中配置

#### 5.使用自定义插件
> 1️⃣ 识别插件前缀
Maven 根据插件的 artifactId 来识别插件前缀。例如下面两种情况:
- 前置匹配
- 匹配规则:${prefix}-maven-plugin
- artifactId:first-maven-plugin
- 前缀:first
- 中间匹配
- 匹配规则:maven-${prefix}-plugin
- artifactId:maven-first-plugin
- 前缀:first
> 2️⃣ 在命令行直接用
```sh
mvn first:sayHello
```
