Maven项目目录结构:
src
-main
-java
-package(包名)
-test
-java
-package(包名)
resources-存放资源文件
以"Maven项目之HelloWorld"来说明如何纯手工构建一个Maven项目. 按照上面给出的目录结构,来构建第一个Maven项目
项目的目录结构:
注: 蓝框内的为包名及类文件.
类HelloWorld,HelloWorld.java:
package com.thera.maven01.model;
public class HelloWorld {
public String sayHello() {
return "HelloWorld!";
}
}
测试类HelloWorldTest,HelloWorldTest.java:
package com.thera.maven01.model;
import org.junit.*;
import org.junit.Assert.*;
public class HelloWorldTest {
@Test
public void testHello() {
// 引用junit框架,并启用断言,为HelloWorld类中的sayHello()方法提供验证;
Assert.assertEquals("HelloWorld!", new HelloWorld().sayHello());
}
}
注意: 在HelloWorldTest中,使用了junit单元测试框架(重点)
到这里已经完成了maven-01主要代码编写.
我们在使用Eclipse或手工编写一个JavaProject时,对引入.jar包的处理通常都是在项目路径下新建一个lib目录,用来存放项目中引入的jar包. 如果是手工编写,我们需要在启动脚本中加入
java -DLabel="Test" -Djava.ext.dirs=./:./lib com.thera.main.Test &
对lib包的引入后,就可以使用jar包了.
在Maven中提供了对包的新的管理方式,通过pom.xml配置文件来管理包引用。
pom.xml(存放在src目录下):
<?xml version="1.0"?>
<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>com.thera.maven01</groupId>
<artifactId>maven01-model</artifactId>
<version>0.0.1SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
关于Maven的配置文件-pom.xml 详细了解,请到这里:
到这里完成了Maven项目之HelloWorld的全部编码工作,接下来使用命令编译及测试
编译:
D:\Maven-Workspace\maven-01>mvn compile
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.thera.maven01:maven01-model:jar:0.0.1SNAPSHOT
[WARNING] 'version' uses an unsupported snapshot version format, should be '*-SNAPSHOT' instead. @ line 11, 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] ------------------------------------------------------------------------
[INFO] Building maven01-model 0.0.1SNAPSHOT
[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:\Maven-Workspace\maven-01\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:\Maven-Workspace\maven-01\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.579 s
[INFO] Finished at: 2016-02-15T21:57:33+08:00
[INFO] Final Memory: 11M/179M
[INFO] ------------------------------------------------------------------------
D:\Maven-Workspace\maven-01>
测试:
D:\Maven-Workspace\maven-01>mvn test
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.thera.maven01:maven01-model:jar:0.0.1SNAPSHOT
[WARNING] 'version' uses an unsupported snapshot version format, should be '*-SNAPSHOT' instead. @ line 11, 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] ------------------------------------------------------------------------
[INFO] Building maven01-model 0.0.1SNAPSHOT
[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:\Maven-Workspace\maven-01\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven01-model ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven01-model ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Maven-Workspace\maven-01\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ 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:\Maven-Workspace\maven-01\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven01-model ---
[INFO] Surefire report directory: D:\Maven-Workspace\maven-01\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.thera.maven01.model.HelloWorldTest
HelloWorld!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.158 s
[INFO] Finished at: 2016-02-15T22:00:56+08:00
[INFO] Final Memory: 13M/227M
[INFO] ------------------------------------------------------------------------
D:\Maven-Workspace\maven-01>
编译执行后目录结构: