日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統 1998 0

?

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

你應該可以看到如下結果:

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

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教程初級篇02:pom.xml配置初步


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 宁海县| 蓬溪县| 雷山县| 三河市| 宣汉县| 株洲市| 甘孜县| 新丰县| 诏安县| 江安县| 株洲县| 肇州县| 莎车县| 苍梧县| 固安县| 平果县| 东城区| 青州市| 平山县| 休宁县| 荔波县| 德昌县| 玛纳斯县| 乌恰县| 呼玛县| 从化市| 雷州市| 松溪县| 陵水| 高阳县| 遵化市| 班戈县| 渑池县| 安仁县| 嘉兴市| 内江市| 中阳县| 潮州市| 苗栗市| 聂荣县| 灵武市|