为什么要用 Maven?
Maven 主要帮助用户完成以下 3 个方面的工作:
生命周期管理,便捷的构建过程;
依赖管理,方便引入所需依赖 Jar 包;
仓库管理,提供统一管理所有 Jar 包的工具;
如何安装 Maven?
- 下载 Maven 二进制包
- 解压缩:
tar xzvf apache-maven-3.3.9-bin.tar.gz
- 设置 Maven 全局变量
在 .bash_profile 中添加内容:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_76.jdk/Contents/Home/
export M2_HOME=/Users/zhangguanghui/Public/apache-maven-3.3.9
export M2=$M2_HOME/bin:$PATH
export MAVEN_OPTS="-Xms256m -Xmx512m"
- 高级配置
如果你想改变本地的 jar 包仓库,或者想增加代理服务器以减少从远程仓库获取 jar 包的次数,请参考http://maven.apache.org/guides/mini/guide-configuring-maven.html
创建第一个 Maven 工程
- 使用 archetype 模板创建工程
mvn -B archetype:generate \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DgroupId=com.mycompany.app \
-DartifactId=my-app
命令执行成功后,产生一个目录文件 my-app,该目录下有一个文件 pom.xml。Maven 就是通过 pom.xml 来构建工程。
- pom.xml 文件内容
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
该 pom.xml 文件内包含了最基础的信息,主要有:
| 标识符 | 含义 |
|: ------- :|: ------- :|
| groupId|一般用该项目的组织或团体的域名来标识,例如:org.apache.maven.plugins |
| artifactId | 代表唯一的工程名 |
|version| 版本号|
|packaging| 标识打包的类型,例如有:jar, war, tar |
|dependencies| 该工程内依赖的其他 jar 包|
如果想深入了解 pom.xml 文件,请阅读
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
- 坐标
每一个 Jar 包都需要定义一个唯一标识,方便管理维护,因此 Maven 使用 groupId, artifactId, versionId 三元素组成一个 Jar 的坐标。当我们依赖该 Jar 包时,同样需要指定该 Jar 包的坐标 <groupId, artifactId, versionId> ,Maven 根据这些信息查询下载。
- 工程的目录结构
localhost:my-app zhangguanghui$ tree
.
|____pom.xml
|____src
| |____main
| | |____java
| | | |____com
| | | | |____mycompany
| | | | | |____app
| | | | | | |____App.java
| |____test
| | |____java
| | | |____com
| | | | |____mycompany
| | | | | |____app
| | | | | | |____AppTest.java
目录 | 含义 |
---|---|
${basedir}/pom.xml | pom 文件 |
${basedir}/src/main/java | 源代码目录 |
${basedir}/src/test/java | 测试代码目录 |
如果你想对 Maven 工程的目录结构更多的了解,请阅读:Introduction to the Standard Directory Layout
如何构建工程?
在 my-app 目录下(pom.xml 的当前文件)执行 mvn clean install。
该命令依次会删除上一次构建产生的结果,编译源代码,编译测试代码,运行测试代码,打包,将 Jar 包存储到本地仓库。
zhangguanghuideMacBook-Pro-3:my-app zhangguanghui$ mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ my-app ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ my-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/zhangguanghui/git/IDEA_workspace/my-app/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ my-app ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/zhangguanghui/git/IDEA_workspace/my-app/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ my-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/zhangguanghui/git/IDEA_workspace/my-app/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ my-app ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/zhangguanghui/git/IDEA_workspace/my-app/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ my-app ---
[INFO] Surefire report directory: /Users/zhangguanghui/git/IDEA_workspace/my-app/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ my-app ---
[INFO] Building jar: /Users/zhangguanghui/git/IDEA_workspace/my-app/target/my-app-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ my-app ---
[INFO] Installing /Users/zhangguanghui/git/IDEA_workspace/my-app/target/my-app-1.0-SNAPSHOT.jar to /Users/zhangguanghui/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.jar
[INFO] Installing /Users/zhangguanghui/git/IDEA_workspace/my-app/pom.xml to /Users/zhangguanghui/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.615 s
[INFO] Finished at: 2017-02-15T15:08:55+08:00
[INFO] Final Memory: 18M/331M
[INFO] ------------------------------------------------------------------------
参考
maven 入门指南
maven 生命周期
Maven 默认插件以及功能
maven 依赖管理
maven-shade-plugin 入门指南
maven-assembly-plugin 入门指南
30 分钟了解 Maven