Apache Maven(一):快速入门

Maven 是什么?

Maven 是一个项目管理和整合工具。Maven 为开发者提供了一套完整的构建生命周期框架。开发团队几乎不用花多少时间就可以自动完成工程的基础构建配置,由于 Maven 使用了一个标准的目录结构和一个默认的构建生命周期。java

在有多个开发团队环境的状况下,Maven 可以在很短的时间内使得每项工做都按照标准进行。由于大部分的工程配置操做都很是简单而且可复用,在建立报告、检查、构建和测试自动配置时,Maven 可让开发者的工做变得更简单。apache

Maven 安装

这里介绍一下在Windows 系统上安装Maven。注:Maven 3.3+ 须要JDK 1.7或更高版本才能执行windows

Maven 的下载地址:官方下载 

咱们将下载下来的zip文件,解压到本地磁盘目录,而后配置环境变量。安全

环境变量配置(windows7为例):

1.添加M2_HOME 和 MAVEN_HOME 环境变量。注:Maven 说只是添加 M2_HOME , 但一些项目仍引用 Maven 的文件夹 MAVEN_HOME, 所以,为了安全也把它添加进去框架

2.更新Path变量的变量值。在最后追加maven

命令:mvn -v 或 mvn -version: 查看Maven版本信息,并肯定是否安装成。ide

C:\Users\Administrator>mvn -v Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:0
5+08:00) Maven home: D:\apache-maven-3.5.3\bin\.. Java version: 1.8.0_161, vendor: Oracle Corporation Java home: D:\DevSoft\jdk1.8.0_161 Default locale: zh_CN, platform encoding: GBK OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

若是你看到相似消息,说明 Apache Maven 在 Windows 上已安装成功。工具

第一个Maven项目

能够在项目目录下执行以下命令:会在你的项目路径下生成一个名称为hello的Maven项目。单元测试

mvn archetype:generate -DgroupId=com.cnblogs.hello -DartifactId=hello -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 

 hello目录结构以下图:测试

全部的源代码放在文件夹 /src/main/java/, 全部的单元测试代码放入 /src/test/java/。

pom.xml文件内容:该文件是在Maven项目中的核心配置文件。它是一个单独的配置文件,其中包含以您构建项目所需的大部分信息。后面我会详细介绍这个文件。

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4   <groupId>com.cnblogs.hello</groupId>
 5   <artifactId>hello</artifactId>
 6   <packaging>jar</packaging>
 7   <version>1.0-SNAPSHOT</version>
 8   <name>hello</name>
 9   <url>http://maven.apache.org</url>
10   <dependencies>
11     <dependency>
12       <groupId>junit</groupId>
13       <artifactId>junit</artifactId>
14       <version>3.8.1</version>
15       <scope>test</scope>
16     </dependency>
17   </dependencies>
18 </project>

接下来执行命令:mvn package 将项目进行打包处理。注:这个命令必须在hello项目木木下执行即:D:\MavenWork\hello目录

D:\MavenWork\hello>mvn package [INFO] Scanning for projects... [INFO] [INFO] ----------------------< com.cnblogs.hello:hello >----------------------- [INFO] Building hello 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e . build is platform dependent! [INFO] skip non existing resourceDirectory D:\MavenWork\hello\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil d is platform dependent! [INFO] Compiling 1 source file to D:\MavenWork\hello\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he llo --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e . build is platform dependent! [INFO] skip non existing resourceDirectory D:\MavenWork\hello\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello -
-- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil d is platform dependent! [INFO] Compiling 1 source file to D:\MavenWork\hello\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello --- [INFO] Surefire report directory: D:\MavenWork\hello\target\surefire-reports Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
rg/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
g/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom (1.7 k B at 740 B/s) Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
rg/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
g/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom (2.3 kB at 1.0 kB/s) Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
rg/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
g/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar (26 kB at 10 kB/s) ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.cnblogs.hello.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello --- [INFO] Building jar: D:\MavenWork\hello\target\hello-1.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.482 s [INFO] Finished at: 2018-06-15T14:46:34+08:00 [INFO] ------------------------------------------------------------------------

最后执行java命令进行测试打包生成的jar

java -cp target/hello-1.0-SNAPSHOT.jar com.cnblogs.hello.App

结果打印以下:

Hello World!

最后生成站点,执行命令:mvn site

mvn site

执行命令会会根据项目的信息生成一个网站。您能够查看target/site下生成的文档

 

经过快速入门我能够简单了解Maven的一些功能,这只是一个快速入门指南。后续几章会有更全面的详细介绍。