一、插件的作用
Maven在构建项目的时候会划分为若干个阶段,主要包括以下几个阶段。在Maven中每一个阶段相当于是一个phase,插件的作用正是和不同的阶段进行绑定,从而达到在某个阶段执行某个插件的作用。制作Maven插件的目的是在于构建项目时希望在构建过程中有什么样的行为。
二、插件的制作
1、POM文件
<?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>plugin</groupId>
<artifactId>test.aogusitus</artifactId>
<version>1.0-SNAPSHOT</version>
<!--打包成一个插件-->
<packaging>maven-plugin</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.11</maven.compiler.source>
<maven.compiler.target>1.11</maven.compiler.target>
<java.version>1.11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5.2</version>
</plugin>
</plugins>
</build>
</project>
重点是<packaging>maven-plugin</packaging>
2、编写MOJO类(Maven old java object)
<!--helloTest为被命名的goal,defaultPhase为默认的绑定周期-->
@Mojo(name="helloTest",defaultPhase = LifecyclePhase.COMPILE)
public class NewPluginMojo extends AbstractMojo {
private static final List<String> DEFAULT_FILES = Arrays.asList("java", "xml", "properties");
@Parameter(defaultValue = "${project.build.testResources}", readonly = true)
private List<Resource> testResources;
<!--属性配置-->
@Parameter(property = "lc.file.includes")
private Set<String> includes = new HashSet<String>();
private Log logger = getLog();
public void execute() throws MojoExecutionException, MojoFailureException {
if (includes.isEmpty()) {
logger.debug("includes/lc.file.includes is empty!");
includes.addAll(DEFAULT_FILES);
}
logger.info("includes: " + includes);
System.out.println("hello plugin!!!");
}
}
3、将插件打包到本地资源库中去
执行命令 mvn clean isntall ,执行成功后可以在本地资源库中看到插件
4、测试插件的应用
新建工程testPlugin,引入新增加的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>plugin</groupId>
<artifactId>test.pluginApply</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.11</maven.compiler.source>
<maven.compiler.target>1.11</maven.compiler.target>
<java.version>1.11</java.version>
</properties>
<build>
<plugins>
<!--引入新创建的plugin-->
<plugin>
<groupId>plugin</groupId>
<artifactId>test-aogusitus-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<!-- 注入参数 -->
<includes>
<include>1111</include>
<include>2222</include>
</includes>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>helloTest</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5.2</version>
</plugin>
</plugins>
</build>
</project>
execution的作用在于将phase阶段和行为goal进行绑定
执行 编译命令后
可以在控制台中看到,成功执行插件中的内容