重生之林朝英txt云盘:Maven教程初级篇02:pom.xml配置初步

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 22:08:50

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,该文件内容如下:

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

net.jianxi.tutorials
numopers
1.0
jar

numopers
http://maven.apache.org


UTF-8




junit
junit
3.8.1
test


其中:

  • 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文件为:

代码
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

net.jianxi.tutorials
numopers
1.0
jar
numopers
http://bluesfeng.javaeye.com




maven-compiler-plugin

1.5
1.5







UTF-8




junit
junit4.7test



现在你可以运行一下命令来自动测试了:

mvn test

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