Maven教程初级篇02:pom.xml配置初步
1. 创建项目并更改项目基本配置信息
在命令行下运行如下命令创建一个项目:
1 |
mvn archetype:create -DgroupId=net.jianxi.tutorials |
2 |
-DartifactId=numopers |
3 |
-DpackageName=net.jianxi.tutorials |
4 |
-Dversion=1.0 |
进入到numopers目录,打开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
>
net.jianxi.tutorials
</
groupId
>
<
artifactId
>
numopers
</
artifactId
>
<
version
>
1.0
</
version
>
<
packaging
>
jar
</
packaging
>
<
name
>
numopers
</
name
>
<
url
>
http://maven.apache.org
</
url
>
<
properties
>
<
project.build.sourceEncoding
>
UTF-8
</
project.build.sourceEncoding
>
</
properties
>
<
dependencies
>
<
dependency
>
<
groupId
>
junit
</
groupId
>
<
artifactId
>
junit
</
artifactId
>
<
version
>
3.8.1
</
version
>
<
scope
>
test
</
scope
>
</
dependency
>
</
dependencies
>
</
project
>
其中:
- groupId: 通常为项目的顶级包名。
- artifactId: 通常为项目名
- version:项目的版本号,在开发的不同阶段,你需要更改这个版本号。
- packaging:项目发布时的打包类型。比如对于普通Java程序打包为jar文件;对于Java web项目则打包为war文件。
- name:通常也是项目名
- url:项目的主页。
2. 添加源代码
在你的项目的src\main\java\net\jianxi\tutorials目录下,删除原有的App.java, 添加一个新的Java源文件: NumOpers.java, 其源代码如下:
01 |
package net.jianxi.tutorials; |
02 |
03 |
public class NumOpers |
04 |
{ |
05 |
public int add(int i, int j) { |
06 |
return i + j; |
07 |
} |
08 |
|
09 |
public int minus(int i, int j) { |
10 |
return i - j; |
11 |
} |
12 |
} |
之后可运行如下命令进行编译:
mvn compile
你应该可以看到如下结果:

3. 添加JUnit 4.x单元测试类
在你的项目的src\test\java\net\jianxi\tutorials目录下,删除原有的AppTest.java, 添加一个新的Java源文件: NumOpersTest.java, 其源代码如下:
01 |
package net.jianxi.tutorials; |
02 |
03 |
import org.junit.* ; |
04 |
import static org.junit.Assert.* ; |
05 |
06 |
public class NumOpersTest { |
07 |
NumOpers no = new NumOpers(); |
08 |
09 |
@Test |
10 |
public void testAdd() { |
11 |
assertEquals(no.add(3,5), 8); |
12 |
} |
13 |
|
14 |
@Test |
15 |
public void testMinus() { |
16 |
assertEquals(no.minus(10,5), 5); |
17 |
} |
18 |
} |
4. 配置pom.xml限定JDK版本号为5, 并支持JUnit 4.7
修改后的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
>
net.jianxi.tutorials
</
groupId
>
<
artifactId
>
numopers
</
artifactId
>
<
version
>
1.0
</
version
>
<
packaging
>
jar
</
packaging
>
<
name
>
numopers
</
name
>
<
url
>
http://bluesfeng.javaeye.com
</
url
>
<
build
>
<
plugins
>
<
plugin
>
<
artifactId
>
maven-compiler-plugin
</
artifactId
>
<
configuration
>
<
source
>
1.5
</
source
>
<
target
>
1.5
</
target
>
</
configuration
>
</
plugin
>
</
plugins
>
</
build
>
<
properties
>
<
project.build.sourceEncoding
>
UTF-8
</
project.build.sourceEncoding
>
</
properties
>
<
dependencies
>
<
dependency
>
<
groupId
>
junit
</
groupId
>
<
artifactId
>
junit
</
artifactId
><
version
>
4.7
</
version
><
scope
>
test
</
scope
>
</
dependency
>
</
dependencies
>
</
project
>
现在你可以运行一下命令来自动测试了:
mvn test
如果测试通过,你可以看到如下结果:

Maven教程初级篇02:pom.xml配置初步的更多相关文章
- Maven 教程(16)— pom.xml 文件详解
原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79733577 <project xmlns="http://ma ...
- Thrift教程初级篇——thrift安装环境变量配置第一个实例
前言: 因为项目需要跨语言,c++客户端,web服务端,远程调用等需求,所以用到了RPC框架Thrift,刚开始有点虚,第一次接触RPC框架,后来没想到Thrift开发方便上手快,而且性能和稳定性也不 ...
- maven 配置篇 之pom.xml
http://www.blogjava.net/zyl/archive/2006/12/30/91055.html http://maven.apache.org/pom.html的翻译. m ...
- Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):4、Maven项目转换与pom.xml配置
文章目录: Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):1.JIRA账号注册 Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):2.PGP ...
- pom.xml 配置maven私服
1.pom.xml 配置maven私服 <repositories> <repository> <id>caf_repositories& ...
- Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):3、Maven独立插件安装与settings.xml配置
文章目录: Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):1.JIRA账号注册 Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):2.PGP ...
- NSIS安装制作基础教程[初级篇], 献给对NSIS有兴趣的初学者
NSIS安装制作基础教程[初级篇], 献给对NSIS有兴趣的初学者 作者: raindy 来源:http://bbs.hanzify.org/index.php?showtopic=30029 时间: ...
- pom.xml配置,针对mvn clean install -P参数(环境参数)打包
pom.xml配置,针对mvn clean install -P参数(环境参数)打包 比如你有2个环境,一个dev,一个prod, 然后你在mvn打包的时候,可以通过-P来打包,是打dev包,还是pr ...
- Maven-SSM项目pom.xml配置以及springmvc配置以及mybatis配置及web.xml配置
一.Maven本地仓库的pom.xml配置 (全部是mysql数据库) <project xmlns="http://maven.apache.org/POM/4.0.0" ...
随机推荐
- 委托、Lambda表达式、事件系列03,从委托到Lamda表达式
在"委托.Lambda表达式.事件系列02,什么时候该用委托"一文中,使用委托让代码简洁了不少. namespace ConsoleApplication2 { internal ...
- SQLServer2008:在查看表记录或者修改存储过程时出现错误。错误消息为: 目录名无效
登陆数据库后,右键打开表提示:目录名无效,执行SQL语句也提示有错误,本来想重装的这个肯定能解决,但是这个方法真的不视为上上策啊,于是在网上找到了这个解决办法,还真是立即见效啊!分享给大家,希望有帮助 ...
- C#编程(六十九)----------DLR简介
DLR 一.近年来,在TIOBE公司每个月发布的编程语言排行榜中,C#总是能挤进前十名,而在最近十年来,C#总体上呈现上升的趋势.C#能取得这样的成绩,有很多因素,其中它在语言特性上的锐意进取让人印象 ...
- Oracle 导入导出 dmp 文件
导入dmp文件,需要知道这个dmp文件创建的用户.因此需要先创建用户,并授权给它. (1)用户的创建 首先,以system用户登录Oracle SQL Developer 其次,在sql工作表(可以用 ...
- 技术人生:special considerations that are very important
For the most part, a lot of what we know about software development can be applied to different envi ...
- SharePoint 应用程序页匿名
前言 最近,有朋友问开发应用程序页,都是需要先登录再访问,无法开发匿名的应用程序页. 解决方法 其实,SharePoint帮我们提供了匿名访问的应用程序页的方法,只是和普通应用程序页继承的基类不一样, ...
- Swift - CALayer的contents属性动画
Swift - CALayer的contents属性动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // LiveImageVi ...
- 波吉亚家族第一季/全集The Borgias 1迅雷下载
波吉亚家族 第一季 The Borgias Season 1 (2011)本季看点:<波吉亚家族>是一个非常复杂的故事,是现代人描绘这个臭名昭著的王朝家族过往历史的一副有趣又坦率的肖像画. ...
- 危机边缘第五季/全集Fringe迅雷下载
本季Fringe Season 5 第五季(2012)看点:Walter 用琥珀将Olivia.Peter.Astrid和自己封存,以便在2036年的未来世界实现自己抵抗观察者的完美计划,“解冻”后的 ...
- 实用ExtJS教程100例-009:ExtJS Form无刷新文件上传
文件上传在Web程序开发中必不可少,ExtJS Form中有一个filefield字段,用来选择文件并上传.今天我们来演示一下如何通过filefield实现ExtJS Form无刷新的文件上传. 首先 ...