1.环境变量
添加:M2HOME:maven的安装目录(不是bin层)
修改:path中新加maven的安装目录,bin层
命令行中
mvn -v查看版本
2.pom.xml
在D:\Study\Maven\Demo\Maven_001_HelloWorld中建立
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>com.artistninth.app</groupId>
<artifactId>HelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.建立程序文件
在D:\Study\Maven\Demo\Maven_001_HelloWorld\src\main\java\com\artistninth\app中建立
SayHello.java
package com.artistninth.app;
public class SayHello{
public String say(String username){
return "Hello "+username;
}
}
4.建立测试文件(也可不要此步)
在D:\Study\Maven\Demo\Maven_001_HelloWorld\src\test\java\com\artistninth\app中建立
SayHelloTest.java
package com.artistninth.app;
import org.junit.*;
import static org.junit.Assert.*;
import com.artistninth.app.SayHello;
public class SayHelloTest{
@Test
public void myTest(){
SayHello sh = new SayHello();
String str = sh.say("ArtistNinth");
assertEquals(str,"Hello ArtistNinth");
}
}
4.编译
切换到D:\Study\Maven\Demo\Maven_001_HelloWorld,即pom.xml所在文件夹
mvn test 测试,可到D:\Study\Maven\Demo\Maven_001_HelloWorld\target\surefire-reports文件夹中查看详细信息
mvn compile编译
mvn package 打包
mvn clean 清空打包后的文件