Maven入门学习1 - 下载,安装,验证,配置
(1)下载maven
下载地址(官方地址):http://maven.apache.org/download.cgi
(2)配置环境变量
此处用的是版本apache-maven-3.5.3。官网上的最新版本是apache-maven-3.5.4,配置环境变量不影响。
将下载好的压缩包解压放到D:\maven\ 下。
开始配置环境变量:
MAVEN_HOME:D:\maven\apache-maven-3.5.3
PATH:追加上 %MAVEN_HOME%\bin 或者 D:\maven\apache-maven-3.5.3\bin
(3)验证
maven是基于JAVA语言的,所以需要Java环境。
命令行打开CMD。输入 mvn -version。输入版本及系统信息,则正确。
C:\Users\qq>mvn -version
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00)
Maven home: D:\maven\apache-maven-3.5.3\bin\..
Java version: 1.8.0_162, vendor: Oracle Corporation
Java home: D:\App\Java\jdk1.8.0_162\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
(4)配置maven本地仓库
进入%MAVEN_HOME%\conf\,找到settings.xml文件
打开文件,找到localRepository,配置到D:\maven\apache-maven-3.5.3\local_resp 作为本地仓库。
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:\maven\apache-maven-3.5.3\local_resp</localRepository>
如果不配置,则默认仓库在C:\users\XX\.m2文件夹下。
(5)maven项目的目录约定
MavenProjectRoot(项目根目录)
|----src
| |----main
| | |----java ——存放项目的.java文件
| | |----resources ——存放项目资源文件,如spring, hibernate配置文件
| | |----webapp ——web资源文件
| |----test
| | |----java ——存放所有测试.java文件,如JUnit测试类
| | |----resources ——存放项目资源文件,如spring, hibernate配置文件
| | |----webapp ——web资源文件
|----target ——项目输出位置,所有的输出都在这里
|----pom.xml ----用于标识该项目是一个Maven项目
(6)示例
1),手动创建一个Maven项目,在D:\JavaDevelop\maven01\下新建项目。
结构如下:
2),其中,cn\edu\HelloWorld是package名。
D:\JavaDevelop\maven01\src\main\java\cn\edu\HelloWorld\Hello.java
D:\JavaDevelop\maven01\src\test\java\cn\edu\HelloWorld\TestClass.java
D:\JavaDevelop\maven01\pom.xml
Hello.java代码如下:
package cn.edu.HelloWorld;
public class Hello{
public String sayHello(){
return "hello world maven,this is test !!!";
}
}
TestClass.java代码如下:
package cn.edu.HelloWorld;
import org.junit.*;
public class TestClass{
@Test
public void testHello(){
System.out.println(new Hello().sayHello());
}
}
pom.xml代码如下:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--groupId、artifactId、version为必需,将在后面介绍-->
<groupId>cn.edu.HelloWorld</groupId>
<artifactId>maven01</artifactId>
<version>0.0.1SNAPSHOT</version>
<dependencies>
<!--在这里引入junit4的依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
3),编译
打开cmd,进入到该项目路径下,执行mvn compile,执行结果如下,build success(构建成功)。
D:\JavaDevelop\maven01>mvn compile
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for cn.edu.HelloWorld:maven01:jar:0.0.1SNAPSHOT
[WARNING] 'version' uses an unsupported snapshot version format, should be '*-SNAPSHOT' instead. @ line 7, column 14
[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] ---------------------< cn.edu.HelloWorld:maven01 >----------------------
[INFO] Building maven01 0.0.1SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven01 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\JavaDevelop\maven01\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven01 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\JavaDevelop\maven01\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.988 s
[INFO] Finished at: 2018-06-29T21:49:23+08:00
[INFO] ------------------------------------------------------------------------
打开路径看到,出现了target文件夹。
构建成功的输出文件都在target下。
打开cmd,进入到该项目路径下,执行mvn test,执行结果如下。
测试成功,输出了Hello.java里的话。
"hello world maven,this is test !!!"
mvn teste结果:
D:\JavaDevelop\maven01>mvn test
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for cn.edu.HelloWorld:maven01:jar:0.0.1SNAPSHOT
[WARNING] 'version' uses an unsupported snapshot version format, should be '*-SNAPSHOT' instead. @ line 7, column 14
[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] ---------------------< cn.edu.HelloWorld:maven01 >----------------------
[INFO] Building maven01 0.0.1SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven01 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\JavaDevelop\maven01\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven01 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven01 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\JavaDevelop\maven01\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven01 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\JavaDevelop\maven01\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven01 ---
[INFO] Surefire report directory: D:\JavaDevelop\maven01\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running cn.edu.HelloWorld.TestClass
hello world maven,this is test !!!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.05 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.802 s
[INFO] Finished at: 2018-06-29T21:54:34+08:00
[INFO] ------------------------------------------------------------------------