教學 開一個Maven專案
- Maven的用處:管理jar檔
- Maven的優點:有網路就可以抓jar檔,且會和網上同步更新版本
- 本文介紹兩種使用Maven的方法,請自己適合的選擇服用
- 法一:原有專案轉成Maven專案
- 法二:生成一個Maven專案
法一:原有專案轉成Maven專案
在所有故事開始前,請先建一個workspace
New –> Dynamic Web Project –> 記得勾選web.xml
- Group Id是組織名稱,請將所有網址反著打,避免之後會產生衝突
- Artifact Id是專案名稱
- Package是將原本專案檔案轉成什麼檔。因為我原本是web專案所以會轉成war檔,原本是java專案就會轉成jar檔。
- Name可以輸入自己的名字(建議英文名)
- Description可以描述一下專案
如下圖所示
Group Id會由小排到大
Name和Description可寫可不寫
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
step2 Google搜尋 "maven repository"
https://mvnrepository.com/
這網站上面有全世界的jar檔
舉例說明:
我想下載servlet
而根據 Tomact官網的對照表
我使用的java版本是java8,所以要使用 servlet4.0
- 在Maven Repository 搜尋「servlet4.0」
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
我選擇第一個,因為下載數最多xD
這個下載次數以億為單位,所以即使是個位數其實也是很可觀的
點進第一個後,一樣選擇下載數最多的xD
也就4.0.1版
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
點進去後把下方這串<dependency>…</dependency>複製
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
step3 回到Eclipse,找到專案中的pom.xml檔
在專案的pom.xml檔案中新增一個<dependencies>…</dependencies>標籤
再把剛剛複製的這段貼在中間,如圖所示
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
最後記得 強制更新! 你要的jar檔就會加進來了~
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
重複步驟:
- 搜尋
- 複製<dependency>…</dependency>這串
- 回到專案打開pom.xml
- 建一個<dependencies></dependencies>標籤
- 把第二點複製的內容貼上,貼在dependencies中間
- 強制更新
- 完成
其他jar檔也是重複上述步驟
例如:我還需要jsp, jdbc, gson
搜尋jsp
–> 複製<dependency>…</dependency>這串
–> 回到專案打開pom.xml
–> 建一個<dependencies></dependencies>標籤
–> 把搜尋到的複製貼上
–> 強制更新
–> 完成
法二: 直接生成Maven Project
step1 左上角按New –> Maven –> Maven Project
建好之後因為是新版Eclipse
所以一開始會有兩個叉叉很正常
step2 先解決環境問題
有叉叉的原因有二
- 第一個叉叉是因為沒有web.xml
解法:
–> 專案上按右鍵
–> Java EE Tools
–> Genertate Deployment Descriptor Stub
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 另一個叉叉是因為缺少某一段標籤
解法:
把下面這段複製貼到你的pom.xml
💡 貼的位置請參考下面這張圖
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
step3 參考重複步驟,貼上dependency
解決這兩個問題之後
我們可以開始使用Maven
請參考上方的重複步驟,就可以完成囉~
💡 ps.儲存點預設是Maven Dependencies,這個可以自己設
相關文章連結: