一、Maven简介
一个软件构建的生命周期有: 清除--> 编译-->测试-->报告-->打包-->安装-->部署
Apache Maven,就是一个一个采用纯Java编写的软件项目管理及自动构建工具,由Apache软件基金会所提供。基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断(pom.xml)能管理一个项目的构建、报告和文档等步骤。
Maven也可被用于构建和管理各种项目,例如C#,Ruby,Scala和其他语言编写的项目。Maven曾是Jakarta项目的子项目,现为由Apache软件基金会主持的独立Apache项目。
二、下载Maven
如果速度较慢的话,可以尝试在CSDN下载,这里提供Maven3.5.0的链接 Maven3.5.0
下载之后直接将Maven解压到文件夹即可。
三、配置Maven
Maven的运行需要依赖JDK,所以先要确认你是否成功安装了JDK,打开cmd,输入java -version 出现以下结果说明配置正确。
C:\Users\XXX>java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
如果没有安装JDK,可以参照这篇文章进行安装配置:JDK安装与配置
接下来进行Maven的配置, 打开控制面板->系统->高级系统设置->环境变量, 在系统变量处加入以下环境变量
M2_HOME=你的Maven安装路径
M2=%M2_HOME%\bin
MAVEN_OPTS=-Xms256m -Xmx512m
之后在系统变量Path的末尾加入 ;%M2% 即可。
以上只针对Windows,其他系统可以参照这篇文档: 极客学院-wiki-Maven环境变量配置
四、运行Maven
打开cmd,输入mvn -v,如果出现下面的结果,说明已经安装成功了。
C:\Users\XXX>mvn -v
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: C:\Program Files\apache-maven-3.5.0\bin\..
Java version: 1.8.0_121, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_121\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
五、修改默认仓库路径
Maven默认会在${user}/.m2/repository/ (如:C:/Users/XXX/.m2/repository/)目录下建立仓库,而C盘通常是系统盘,如果文件过多会影响系统速度,因此需要修改。
打开Maven的安装路径,找到conf/settings.xml,在文件中搜索repository,可以找到以下位置
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
可以看到默认的仓库路径,我们在后面添加以下代码(仓库路径根据自己喜好而定)
<!-- 配置本地maven仓库路径 -->
<localRepository>C:/Program Files/apache-maven-3.5.0/repository</localRepository>
六、第一个Maven项目
1.建立以下目录结构
HelloMaven
|-src
| |--main
| | |-java
| | |-resources
| |-test
| |-java
| |-resources
|-target
|-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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.maven</groupId>
<artifactId>Hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<packaging>jar</packaging>
<name>HelloMaven</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.创建java类
在src/main/java/cn/maven
目录下新建文件Hello.java 记得保存时要以UTF-8格式保存,内容如下
package cn.maven;
public class Hello{
public String getValue(){
return "Hello Maven";
}
}
3.创建测试类
在/src/test/java/cn/maven
目录下新建测试文件TestHello.java,内容如下
package cn.maven;
import org.junit.Test;
public class TestHello {
@Test
public void testGetValue(){
Hello h = new Hello();
System.out.println(h.getValue());
}
}
4.项目编译与测试
在项目根路径打开cmd,输入命令mvn compile
E:\code\maven> mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Hello 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Hello ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Hello ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\code\maven\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.397 s
[INFO] Finished at: 2017-07-17T11:05:42+08:00
[INFO] Final Memory: 13M/257M
[INFO] ------------------------------------------------------------------------
出现BUILD SUCCESS说明maven项目编译成功! 接下来输入mvn test即可执行maven测试
E:\code\maven> mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Hello 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Hello ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Hello ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Hello ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ Hello ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\后端视频\code\maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ Hello ---
[INFO] Surefire report directory: E:\后端视频\code\maven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running cn.maven.TestHello
Hello Maven
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.621 s
[INFO] Finished at: 2017-07-17T11:11:47+08:00
[INFO] Final Memory: 15M/264M
[INFO] ------------------------------------------------------------------------
可以看到测试类里的结构输出正确,为“Hello Maven‘’。
至此,恭喜你已经完成了第一个Maven项目!
感谢您的阅读,希望能对您有所帮助!
更多关于Maven的文章,请参阅: