Maven教程初级篇02:pom.xml配置初步

本文详细介绍了如何使用Maven创建Java项目,并配置基本信息。包括创建项目、添加源代码、实现JUnit4.x单元测试及配置JDK版本和JUnit版本。通过实际操作,展示了从项目初始化到编译、测试的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 创建项目并更改项目基本配置信息

在命令行下运行如下命令创建一个项目:

mvn archetype:create -DgroupId=net.jianxi.tutorials -DartifactId=numopers
-DpackageName=net.jianxi.tutorials  -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, 其源代码如下:

package net.jianxi.tutorials;
public class NumOpers {
public int add(int i, int j){
  return i+j;
}
public int minus(int i, int j){
return i-j;
}

}

之后可运行如下命令进行编译:

mvn compile

你应该可以看到如下结果:

3. 添加JUnit 4.x单元测试类

在你的项目的src\test\java\net\jianxi\tutorials目录下,删除原有的AppTest.java, 添加一个新的Java源文件: NumOpersTest.java, 其源代码如下:

package net.jianxi.tutorials;

import org.junit.* ;

import static org.junit.Assert.* ;

public class NumOpersTest {

  NumOpers no = new NumOpers();

  @Test

public void testAdd() {

assertEquals(no.add(3,5), 8);

}

  @Test

public void testMinus() {

  assertEquals(no.minus(10,5), 5);

}

}

 

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

如果测试通过,你可以看到如下结果:

转载自:

http://www.cnblogs.com/dcba1112/archive/2011/05/01/2033781.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值