0%

maven自定义插件

maven自定义插件

步骤

  • 首先创建一个maven-plugin项目,打包方式为<packaging>maven-plugin</packaging>
  • 为插件编写目标Mojo(继承AbstractMojo来进行实现),每个插件必须包含一个或多个目标
  • 为目标提供配置点

实现

自定义插件的pom文件,注意打包方式为<packaging>maven-plugin</packaging>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<groupId>com.zhanghe.study</groupId>
<artifactId>test_maven_plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<dependencies>
<!-- 编写插件必备的依赖 -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.0</version>
</dependency>
</dependencies>

编写插件逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// name表示插件的goal名称  defaultPhase指的是触发的生命周期
@Mojo(name = "customMojo",defaultPhase = LifecyclePhase.COMPILE)
public class CustomMojo extends AbstractMojo {

// 会获取配置文件中的变量并赋值
@Parameter
private String version;
// 如果抛出MojoFailureException异常,maven会显示“BUILD FAILURE”的错误信息,表示发现了预期的错误
// 如果抛出MojoExecutionException异常,maven会显示“BUILD ERROR”的错误信息,表示发现了未预期的错误
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
System.out.println("使用的版本为"+version);
}
}

将该插件打包到本地仓库或者maven私服中,就可以在其他maven项目中进行使用了

注解解释

看着上边的代码可能有些注解不知道什么意思,这里来解释一下

@Mojo解释

先说一下@Mojo注解,是用来标注Mojo行为的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public @interface Mojo
{
/**
* goal name (required).
* @return the goal name
*/
// 目标名称,这个在pom.xml中使用插件时配置goal需要用到
String name();

/**
* default phase to bind your mojo.
* @return the default phase
*/
// 配置默认绑定的阶段,这样就不需要在pom.xml中使用该插件的时候进行配置<phase>了
LifecyclePhase defaultPhase() default LifecyclePhase.NONE;

/**
* the required dependency resolution scope.
* @return the required dependency resolution scope
*/
// 表示在运行该Mojo之前必须解析所有指定范围的依赖
ResolutionScope requiresDependencyResolution() default ResolutionScope.NONE;

/**
* the required dependency collection scope.
* @return the required dependency collection scope
*/
ResolutionScope requiresDependencyCollection() default ResolutionScope.NONE;

/**
* your Mojo instantiation strategy. (Only <code>per-lookup</code> and <code>singleton</code> are supported)
* @return the instantiation strategy
*/
InstantiationStrategy instantiationStrategy() default InstantiationStrategy.PER_LOOKUP;

/**
* execution strategy: <code>once-per-session</code> or <code>always</code>.
* @return <code>once-per-session</code> or <code>always</code>
*/
String executionStrategy() default "once-per-session";

/**
* does your mojo requires a project to be executed?
* @return requires a project
*/
// 表示该项目是否必须在一个maven项目中运行
boolean requiresProject() default true;

/**
* does your mojo requires a reporting context to be executed?
* @return requires a reporting context
*/
// 表示是否要求项目报告生成
boolean requiresReports() default false;

/**
* if the Mojo uses the Maven project and its child modules.
* @return uses the Maven project and its child modules
*/
// 当Mojo在多模块项目上运行时,配置了该目标只会在顶层模块运行
boolean aggregator() default false;

/**
* can this Mojo be invoked directly only?
* @return invoked directly only
*/
// 如果为true,表示只能通过命令行来进行调用,不能在pom中将其绑定到生命周期阶段
boolean requiresDirectInvocation() default false;

/**
* does this Mojo need to be online to be executed?
* @return need to be online
*/
// 是否要求maven必须是online状态
boolean requiresOnline() default false;

boolean inheritByDefault() default true;

/**
* own configurator class.
* @return own configurator class
*/
String configurator() default "";

/**
* is your mojo thread safe (since Maven 3.x)?
* @return is thread safe
*/
boolean threadSafe() default false;
}
@Parameter解释

用来获取Mojo参数的,其可支持boolean、int、float、String、Date、File、array、Collection、Map、Properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public @interface Parameter
{
/**
* name of the bean property used to get/set the field: by default, field name is used.
* @return the name of the bean property
*/
// 参数名
String name() default "";

/**
* alias supported to get parameter value.
* @return the alias
*/
// 参数别名
String alias() default "";

/**
* Property to use to retrieve a value. Can come from <code>-D</code> execution, setting properties or pom
* properties.
* @return property name
*/
// 表达式,可以在命令行使用-D进行配置,也可以在settings.xml中配置或者在pom.xml中配置
// 如使用 ${maven.test.skip}
String property() default "";

/**
* parameter default value, eventually containing <code>${...}</code> expressions which will be interpreted at
* inject time: see
* <a href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">
* PluginParameterExpressionEvaluator</a>.
* @return the default value
*/
// 默认值,没有配置时使用的默认值
String defaultValue() default "";

/**
* is the parameter required?
* @return <code>true</code> if the Mojo should fail when the parameter cannot be injected
*/
// 是否必须设置
boolean required() default false;

/**
* Specifies that this parameter cannot be configured directly by the user (as in the case of POM-specified
* configuration). This is useful when you want to force the user to use common POM elements rather than plugin
* configurations, as in the case where you want to use the artifact's final name as a parameter. In this case, you
* want the user to modify <code>&lt;build&gt;&lt;finalName/&gt;&lt;/build&gt;</code> rather than specifying a value
* for finalName directly in the plugin configuration section. It is also useful to ensure that - for example - a
* List-typed parameter which expects items of type Artifact doesn't get a List full of Strings.
*
* @return <code>true</code> if the user should not be allowed to configure the parameter directly
*/
// 只读的表示用户无法对该参数进行配置
boolean readonly() default false;
}

其他项目配置使用该插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<build>
<plugins>
<plugin>
<groupId>com.zhanghe.study</groupId>
<artifactId>test_maven_plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 配置参数值 -->
<configuration>
<version>0.0.1-SNAPSHOT</version>
</configuration>

<!-- 指定什么时候进行触发 -->
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>customMojo</goal>
</goals>
</execution>
</executions>

</plugin>
</plugins>
</build>

欢迎关注我的其它发布渠道