Maven - 实例-1-手工创建Maven项目
1- 根据包结构创建maven项目目录
TestMaven
- src
- src/main/java/anliven/testmaven01/HelloMaven.java
- src/test/java/anliven/testmanven01/HelloMavenTest.java
- pom.xml
2- 示例代码和pom文件
<groupId></groupId> :项目的包名
<artifactId></artifactId> : 模块名
<version></version> : 版本,遵循Maven版本管理规范
HelloMaven.java
package anliven.testmaven01;
public class HelloMaven {
public String sayHello() {
return "Hello Maven!";
}
}
HelloMavenTest.java
package anliven.testmaven01;
import org.junit.*;
import org.junit.Assert.*;
public class HelloMavenTest {
@Test
public void testMaven() {
System.out.println("Run test!");
Assert.assertEquals("Hello Maven!", new HelloMaven().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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>anliven.testmaven01</groupId>
<artifactId>testmaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
3- 运行mvn compile
如果是第一次运行mvn compile等命令时,将会下载很多的第三方和maven所依赖的jar包。
在Maven项目根目录下,默认生成target目录:
target 构建过程中的默认生成的临时目录
target/classes/ 存放src/main/java目录下源文件编译出来的字节码文件(.class)
target/maven-status/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
[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:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.064 s
[INFO] Finished at: 2017-10-20T10:56:12+08:00
[INFO] Final Memory: 15M/292M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l
total 1
-rw-r--r-- 1 guowli 1049089 561 Oct 19 17:44 pom.xml
drwxr-xr-x 1 guowli 1049089 0 Oct 19 13:21 src/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 target/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ls -l target/classes/anliven/testmaven01/HelloMaven.class
-rw-r--r-- 1 guowli 1049089 406 Oct 20 10:56 target/classes/anliven/testmaven01/HelloMaven.class
4- 运行mvn test
生成如下目录:
target/surefire-reports/ 存放生成的测试报告
target/test-classes/ 存放src/test/java目录下源文件编译出来的字节码文件(.class)
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven ---
[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:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running anliven.testmaven01.HelloMavenTest
Run test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.064 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.286 s
[INFO] Finished at: 2017-10-20T11:01:06+08:00
[INFO] Final Memory: 16M/291M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 surefire-reports/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 test-classes/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/surefire-reports/
total 9
-rw-r--r-- 1 guowli 1049089 282 Oct 20 11:01 anliven.testmaven01.HelloMavenTest.txt
-rw-r--r-- 1 guowli 1049089 6398 Oct 20 11:01 TEST-anliven.testmaven01.HelloMavenTest.xml
5- 运行mvn package
生成如下目录及文件:
target/maven-archiver/
target/xxx-y.y.y-zzz.jar 生成的jar包
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 surefire-reports/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 test-classes/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven ---
[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:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running anliven.testmaven01.HelloMavenTest
Run test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ testmaven ---
[INFO] Building jar: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\testmaven-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.437 s
[INFO] Finished at: 2017-10-20T11:13:48+08:00
[INFO] Final Memory: 16M/213M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 4
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:13 maven-archiver/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 surefire-reports/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 test-classes/
-rw-r--r-- 1 guowli 1049089 2151 Oct 20 11:13 testmaven-0.0.1-SNAPSHOT.jar
行动是绝望的解药!
欢迎转载和引用,但请在明显处保留原文链接和原作者信息!
本博客内容多为个人工作与学习的记录,少数内容来自于网络并略有修改,已尽力标明原文链接和转载说明。如有冒犯,即刻删除!
以所舍,求所得,有所获,方所成。