Maven初步入门(二)

1. 什么是POM

Maven项目的核心是pom.xml, POM(Project Object Model)定义了项目的基本信息,用于描述项目如何构建、声明项目依赖等等。

首先我们使用Eclipse新建一个Maven项目,项目名为hello-world,如下图:

Maven初步入门(二)_第1张图片

在pom.xml中最重要几个重要的元素有groupId、artifactId、version、dependencies等。

groupId:定义了项目属于哪个组,该组一般和项目所在的组织或公司有关。比如你在googlecode上建立一个名为myapp的项目,那么groupId为com.googlecode.myapp。

artifactId: 定义了当前Maven项目中中唯一的ID,比如前面的groupId为com.googlecode.myapp,我们可以为不同的子项目或者模块分配不同的artifactId,如myapp-util, myapp-dao,myapp-web等。

version: 指定了项目当前的版本。SNAPSHOT意为快照,表示项目还在开发中,还不稳定。

dependencies: 该元素下可以包含多个dependency以声明项目的依赖。这里添加了一个依赖--groupId为junit,artifactId为junit,version为4.10.有了这段声明Maven会自动从中央仓库下载junit-4.10.jar。

2. 编写代码

1) Maven项目的主代码位于src/main/java目录中,在上图中我们可以看到。代码编写完毕后,右键pom.xml --> Run as -->Maven build,在Goals输入 clean compile,会得到如下结果:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-world 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting D:\code\maven\hello-world\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\code\maven\hello-world\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.567s
[INFO] Finished at: Thu Nov 21 22:13:29 CST 2013
[INFO] Final Memory: 9M/101M
[INFO] ------------------------------------------------------------------------

2) 测试代码位于独立的目录中,默认的测试代码目录是src/test/java,编写测试代码:
package com.alvinliang.maven;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class HelloWorldTest {
	
	@Test
	public void testSayHello() {
		HelloWorld helloWorld = new HelloWorld();
		String result = helloWorld.sayHello();
		assertEquals("Hello World", result);
	}
}
之后调用Maven执行测试,右键pom.xml --> Run as --> Maven build, 在goals中输入clean test:
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-world 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting D:\code\maven\hello-world\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\code\maven\hello-world\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hello-world ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\code\maven\hello-world\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
[INFO] Surefire report directory: D:\code\maven\hello-world\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.alvinliang.maven.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.214s
[INFO] Finished at: Thu Nov 21 22:35:11 CST 2013
[INFO] Final Memory: 11M/101M
[INFO] ------------------------------------------------------------------------

这样我们就学会如何简单的使用Maven新建项目了,重点要理解POM的基本概念。

你可能感兴趣的:(Maven初步入门(二))