將一個普通的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.