Maven 默认插件以及功能

Maven 默认插件

已知 Maven 使用 plugin 来执行实际操作的,在默认情况下,Maven 会绑定以下几个插件来完成基本操作。

plugin function life cycle phase
maven-clean-plugin 清理上一次执行创建的目标文件 clean
maven-resources-plugin 处理源资源文件和测试资源文件 resources,testResources
maven-compiler-plugin 编译源文件和测试源文件 compile,testCompile
maven-surefire-plugin 执行测试文件 test
maven-jar-plugin 创建 jar jar
maven-install-plugin 安装 jar,将创建生成的 jar 拷贝到 .m2/repository 下面 install
maven-deploy-plugin 发布 jar deploy

如果针对各个 plugin 有特殊配置的话,需要显示指定 plugin 和 属性配置。

样例

举个🌰:创建一个空的 maven 项目 HelloWorld,pom.xml 文件不包含任何 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>com.meituan.log</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.1</version>
        </dependency>
    </dependencies>
</project>

下面是执行 mvn clean install 产生的执行日志,通过日志可以看到每个步骤都显示 plugin 名称,版本号,生命周期阶段,执行操作等。
maven-clean-plugin:2.5:clean (default-clean)
maven-resources-plugin:2.6:resources (default-resources)
maven-compiler-plugin:3.1:compile (default-compile)
maven-resources-plugin:2.6:testResources (default-testResources)
maven-compiler-plugin:3.1:testCompile (default-testCompile)
maven-surefire-plugin:2.12.4:test (default-test)
maven-jar-plugin:2.4:jar (default-jar)
maven-install-plugin:2.4:install (default-install)

zhangguanghuideMacBook-Pro-3:HelloWorld zhangguanghui$ mvn clean install
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.meituan.log:HelloWorld:jar:1.0-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.apache.hadoop:zookeeper:jar -> duplicate declaration of version 3.3.1 @ line 20, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building HelloWorld 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ HelloWorld ---
[INFO] Deleting /Users/zhangguanghui/git/IDEA_workspace/HelloWorld/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloWorld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloWorld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/zhangguanghui/git/IDEA_workspace/HelloWorld/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloWorld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/zhangguanghui/git/IDEA_workspace/HelloWorld/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ HelloWorld ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloWorld ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HelloWorld ---
[INFO] Building jar: /Users/zhangguanghui/git/IDEA_workspace/HelloWorld/target/HelloWorld-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ HelloWorld ---
[INFO] Installing /Users/zhangguanghui/git/IDEA_workspace/HelloWorld/target/HelloWorld-1.0-SNAPSHOT.jar to /Users/zhangguanghui/.m2/repository/com/meituan/log/HelloWorld/1.0-SNAPSHOT/HelloWorld-1.0-SNAPSHOT.jar
[INFO] Installing /Users/zhangguanghui/git/IDEA_workspace/HelloWorld/pom.xml to /Users/zhangguanghui/.m2/repository/com/meituan/log/HelloWorld/1.0-SNAPSHOT/HelloWorld-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.232 s
[INFO] Finished at: 2017-02-13T17:43:06+08:00
[INFO] Final Memory: 17M/331M
[INFO] ------------------------------------------------------------------------

参考

maven 入门指南
maven 生命周期
Maven 默认插件以及功能
maven 依赖管理
maven-shade-plugin 入门指南
maven-assembly-plugin 入门指南

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 116,632评论 15 132
  • 所有项目的构建都是有生命周期的,这个生命周期包括:项目清理、初始化、编译、测试、打包、集成测试、验证、部署、站点生...
    zlcook阅读 2,063评论 0 21
  • 1. Maven 构建生命周期 Maven 构建生命周期就是 Maven 将一个整体任务划分为一个个的阶段,类似于...
    data4阅读 22,228评论 3 29
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 41,442评论 6 343
  • 1.编写POM Maven项目的核心文件是pom.xml,POM(Project Objcet Model)项目对...
    zlcook阅读 3,400评论 7 26