插件plugin是绑定到生命周期,承担实际功能的组件。mvn运行时,自动关联插件来运行
下图是maven默认的各阶段对应的插件列表:
生命周期 | 生命周期阶段 | 插件目标 | 执行任务 |
clean | pre-clean |
|
|
| clean | maven-clean-plugin:clean | 删除项目的输出目录。 |
| post-clean |
|
|
site | pre-site |
|
|
| site | maven-site-plugin:site |
|
| post-site |
|
|
| site-deploy | maven-site-plugin:deploy |
|
default | process-resources | maven-resources-plugin:resources | 复制主资源文件至主输出目录 |
| compile | maven-compiler-plugin:compile | 编译主代码至主输出目录 |
| process-test-resources | maven-resources-plugin:testResources | 复制测试资源文件至测试输出目 |
| test-compile | maven-compiler-plugin:testCompile | 编译测试代码至测试输出目录 |
| test | maven-surefire-plugin:test | 执行测试用例 |
| package | maven-jar-plugin:jar(ejb:ejb jar:jar rar:rar war:war) | 创建项目jar包 |
| install | maven-install-plugin:install | 将项目输出构件安装到本地仓库 |
| deploy | maven-deploy-plugin:deploy | 将项目输出构件部署到远程仓库 |
常用Maven命令:
mvn clean 清理
mvn compile 编译主程序
mvn package 打包
mvn install 安装jar到本地库
使用maven命令生成项目(idea和eclipse生成项目最终也是依赖maven插件生成的)
mvn archetype:generate -DgroupId=enjoy -DartifactId=simple -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0
mvn archetype:generate -DgroupId=enjoy -DartifactId=simple-web -DarchetypeArtifactId=maven-archetype-webapp -Dversion=1.0
maven插件开发
可以自定义插件,来扩展maven的功能。插件的开发步骤如下:
1、引入maven api的依赖
<packaging>maven-plugin</packaging>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
2、编写简单Mojo类---继承AbstractMojo
@Mojo(name="log")
public class MyMavenPlugin extends AbstractMojo {
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("deploy to your local re-------------------");
}
}
3、执行插件
先mvn install
mvn groupId :artifactId :version :goal
然后再mvn com.caojiulu:caojiulu-plugin:1.0:log
ean-plugin:2.5:clean (default-clean) @ caojiulu-plugin ---
[INFO] Deleting D:\bim_develop\workspaces\maventest\caojiulu-plugin\target
[INFO]
[INFO] --- caojiulu-plugin:1.0:log (default) @ caojiulu-plugin ---
[INFO] deploy to your local re-------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -------------------------------------------------------------------
4、关联插件到生命周期来执行
<build>
<plugins>
<plugin>
<groupId>com.caojiulu</groupId>
<artifactId>caojiulu-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<!-- 配置compile执行 -->
<phase>clean</phase>
<goals>
<!-- 配置执行目标 -->
<goal>log</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
构建项目对应的生命周期
标签:插件,plugin,caojiulu,maven,clean,mvn From: https://blog.51cto.com/u_14906615/5899442