Maven_1_环境搭建
项目管理利器——maven http://www.imooc.com/learn/443
---2-1 maven下载---------------------------------
maven下载:http://maven.apache.org/download.cgi
Binary zip archive: apache-maven-3.5.0-bin.zip
Source zip archive: apache-maven-3.5.0-src.zip
++
apache-maven-3.5.0
\bin :bin目录包含mvn的运行脚本
\boot 类加载器的框架(maven用它来加载自己的类库)
\conf 配置文件 如settings.xml
\lib 类库 maven和第三方以来的类库
----------------------------------------------------------------------
配置环境变量:
用户变量:多人公用时在此设置。
系统变量:私人使用电脑时就可以设在此处
1.添加:
变量名:M2_HOME 值:maven安装目录
2. Path:
添加";%M2_HOME%\bin"
验证:cmd中输入mvn -v 显示版本信息(echo %M2_HOME% 显示该变量的值)
---2-2 maven快速入门-------------------------------------------------------------------
maven目录结构
src
-main
-java
-package
-test
-java
-package
pom.xml 在根目录下配置pom.xml
example:
1.HelloWorld.java
D:\java\code\maven01\src\main\java\com\imooc\maven01\model
package com.imooc.maven01.model; public class HelloWorld { public String sayHello() { return "HelloWorld !"; } }
2.HelloWorldTest.java
D:\java\code\maven01\src\test\java\com\imooc\maven01\model
package com.imooc.maven01.model; import org.junit.*; import org.junit.Assert.*; public class HelloWorldTest { @Test public void testHello() { Assert.assertEquals("HelloWorld !",new HelloWorld().sayHello()); } }
3.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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion><!--固定--> <groupId>com.iooc</groupId><!--groupId项目包名--> <artifactId>maven01-model</artifactId><!--artifactId模块名;建议使用项目名--> <version>0.0.1-SNAPSHOT</version><!--快照版本--> <dependencies> <dependency> <!--junit依赖--> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> </dependencies> </project>
cmd:
1.cd 进入maven01 项目根目录中 cd url
以下 命令执行时会下载需要的jar包 (下载的jar包会保存在用户文件夹下的.m2\repository 目录(directory)中 如 C:\Users\30478\.m2\repository)
2.mvn compile 对项目进行编译
编译结果
D:\java\code\maven01>mvn compile [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven01-model 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven01-model --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\java\code\maven01\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven01-model --- [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:\java\code\maven01\target\classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.857 s [INFO] Finished at: 2017-06-27T15:36:46+08:00 [INFO] Final Memory: 13M/161M [INFO] ------------------------------------------------------------------------ D:\java\code\maven01>
3.mvn test 运行测试用例
自动生成target文件夹
\target
\classes 字节码文件
\maven-status
\surefire-reports 生成的测试报告
\test-classes
------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.imooc.maven01.model.HelloWorldTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.102 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 19.230 s [INFO] Finished at: 2017-06-27T15:39:16+08:00 [INFO] Final Memory: 17M/140M [INFO] ------------------------------------------------------------------------
5.mvn package
在target目录中生成maven01-model的jar
INFO] Building jar: D:\java\code\maven01\target\maven01-model-0.0.1-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 13.251 s [INFO] Finished at: 2017-06-27T15:44:13+08:00 [INFO] Final Memory: 13M/123M [INFO] ------------------------------------------------------------------------
---------------------------------------------------------------------------
1.cmd 中cd到maven项目的根目录。
2.maven的运行
mvn compile 编译 mvn test 运行test包下面的代码 生成target文件夹 其中classes中为编译后的字节码文件
默认在根目录生成target目录 mvn package 给整个项目打包,在target目录下生成jar文件(文件类型可用pom中的packaging标签自己定义)
----------------------------------------------------------------------
----------------------------------------------------------------------
----------------------------------------------------------------------