Maven初步入門(二)

jopen 8年前發布 | 11K 次閱讀 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的基本概念。

來自: http://my.oschina.net/liangbo/blog/178171

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!