<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-jar-pluginartifactId>
<version>3.2.2version>
<configuration>
<archive>
<addMavenDescriptor>trueaddMavenDescriptor>
<manifest>
<addClasspath>trueaddClasspath>
<classpathPrefix>lib/classpathPrefix>
<mainClass>com.example.demo.DemoApplicationmainClass>
manifest>
<manifestEntries>
manifestEntries>
archive>
<outputDirectory>${project.build.directory}outputDirectory>
<excludes>
<exclude>${project.basedir}/xml/*exclude>
excludes>
<includes>
<include>**/*.classinclude>
<include>**/*.propertiesinclude>
includes>
configuration>
plugin>
如上标签就是打包插件自定义的。其他的如等都是maven基础标签。
所有标签中的内容,都是用@Parameter注入的,如果必填,加上 required = true。
@Parameter(defaultValue = "${project.build.outputDirectory}", required = true)
private File classesDirectory;
apache打包插件官网地址:
https://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html
github地址:
https://github.com/apache/maven-jar-plugin.git
创建类的时候就定义了。 defaultPhase = LifecyclePhase.PACKAGE 这一行就是。
@Mojo(
name = "jar",
defaultPhase = LifecyclePhase.PACKAGE,
requiresProject = true,
threadSafe = true,
requiresDependencyResolution = ResolutionScope.RUNTIME)
public class JarMojo extends AbstractJarMojo {}
想要解答这个问题,先要知道控制执行有几种方式。
两种:
1、 @Mojo注解中defaultPhase设置默认phase(阶段)。
2、配置文件中指定。
如:
<plugin>
<groupId>com.examplegroupId>
<artifactId>my-pluginartifactId>
<version>1.0.0version>
<executions>
<execution>
<id>execution-1id>
<phase>compilephase>
<goals>
<goal>goal-1goal>
goals>
execution>
<execution>
<id>execution-2id>
<phase>installphase>
<goals>
<goal>goal-2goal>
<goal>goal-3goal>
goals>
execution>
executions>
plugin>
这样也实现了goal和phase的绑定。
那么另外一个问题也迎刃而解,maven自定义插件可以有多个goal(mojo)吗?
当然可以,参考maven-install-plugin,有两个mojo:install和install-file。