將一個普通的java專案轉換成Maven專案並不是一個很大的任務,僅僅只需要下面的幾步就能將轉換成功。下面我是用一個簡單的Selenium測試小demon作為例子來說的。
- 移調專案中所有關聯的Libraries
- 將Selenium專案轉換成Maven專案
- 增加Maven依賴庫到專案中
- 建立Maven的測試資料夾架構
Step1:移調專案中所有關聯的Libraries
選中你需要轉換的Project:Build Path > Configure Build Path
Step2: 將Selenium專案轉換成Maven專案
1) 右鍵需轉換的專案,Configure > Convert Maven Project
2) 要求填入Group ID和Artifact ID,這個不需要改變,用預設專案提供的
3) 轉換成功後,該專案在Eclipse中的結構變成下面這樣
沒轉換前的結構 轉換後的結構
Step3:增加Maven依賴庫到專案中
現在我們就需要將專案中所需要的依賴包加到本地中心源中,我就針對這個專案來說,我們知道我們現在缺少Selenium jar包,junit jar包等等。
1) 到 http://www.mvnrepository.com/ 上面查詢Selenium
2) 點選Selenium Java
3) 選擇你所需要使用的Selenium的版本
4) 關於Selenium在Maven中資訊就會顯示出來,然後將這些資訊加到pom.xml中
5) 將Selenium的dependency的資訊加到pom.xml,可以在Dependencies以流程的方式新增進去,儲存,這個時候會下載相關的依賴包到本地源中心中。(需要一點時間,有任何問題,可以參考之前Maven(1)中提到,解決問題)
6)直接在pom.xml中將查詢到的資訊拷貝到xml檔案中,儲存。
7)等待依賴包被下載完後,所有的錯誤就會解決
Step4 建立Maven的測試資料夾架構
接下來就是檢驗真理的時候,看這個轉換的Selenium專案能否跑起來,選擇pom.xml > Run As > Maven Test 結果-----------測試用例沒有被執行,這是為什麼呢?仔細看看log,發現Maven預設是在\src\main\resource這個路徑下找的原始碼資源,測試原始碼是在\src\test\resources 。
輸出結果為:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building MavenConvertDemon1 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenConvertDemon1 --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\Junit\MavenConvertDemon1\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenConvertDemon1 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenConvertDemon1 --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\Junit\MavenConvertDemon1\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenConvertDemon1 --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenConvertDemon1 --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.999 s [INFO] Finished at: 2015-02-06T16:22:09+08:00 [INFO] Final Memory: 6M/15M [INFO] ------------------------------------------------------------------------
那麼現在的目的就是怎麼自定義Maven的目錄結構?
下面這個程式碼是把原始碼設定成src目錄下,把測試的原始碼放在src/test下面,這樣修改後就解決剛才的問題了,測試就跑起來了。這樣一個Selenium專案就轉換成Maven專案了。
<build> <sourceDirectory>src/</sourceDirectory> <testSourceDirectory>src/test</testSourceDirectory> </build>
成功後的輸出結果:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building MavenConvertDemon1 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenConvertDemon1 --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\Junit\MavenConvertDemon1\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenConvertDemon1 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenConvertDemon1 --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\Junit\MavenConvertDemon1\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenConvertDemon1 --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent! [INFO] Compiling 1 source file to D:\Junit\MavenConvertDemon1\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenConvertDemon1 --- [INFO] Surefire report directory: D:\Junit\MavenConvertDemon1\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running test.TestSeleniumDemon1 Feb 6, 2015 4:32:55 PM org.openqa.selenium.os.UnixProcess$SeleniumWatchDog destroyHarder INFO: Command failed to close cleanly. Destroying forcefully (v2). org.openqa.selenium.os.UnixProcess$SeleniumWatchDog@9ffe3f Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 31.452 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 38.029 s [INFO] Finished at: 2015-02-06T16:32:57+08:00 [INFO] Final Memory: 11M/26M [INFO] ------------------------------------------------------------------------
PS;附一張Maven的標準目錄結構:
————————————————————————————————
WHOOOOSHHHHHHHHHHHH…………
Blimey what was that?
That was your life mate
Oh, I was not quite ready. Can I have another go?
Sorry mate, only one per person.