Maven初步入门(二)

1. 什么是POM

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

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

在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的基本概念。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值