iT邦幫忙

2025 iThome 鐵人賽

0
Software Development

我們與Maven的距離系列 第 31

Day30 - Visual Studio Code Integration with Maven

  • 分享至 

  • xImage
  •  

前言

Visual Studio Code簡稱VS Code是目前AI工具整合相當完善的IDE,寫前端已是滿主流使用的IDE,若能使用它寫Java這樣全端工程師就可以省去多學一套IDE工具,是不是相當棒呢

必要插件安裝與設定

  1. Extension for Java Pack
    https://ithelp.ithome.com.tw/upload/images/20251015/20128084hDEcxOSSBn.png
    內含6個Extension,Maven也包含其中
    https://ithelp.ithome.com.tw/upload/images/20251015/20128084NjmTcVTnoW.png
    透過Command Palette Ctrl+Shift+P可以叫出command列輸入
    https://ithelp.ithome.com.tw/upload/images/20251015/20128084yiEHosTF32.png
  • Java Extension Guide有其他建議安裝的Java Extension可直接點選下載
  • Tips for Beginners有著基本的操作說明
  1. 安裝Community Server Connectors extension
    此套件作為開發Java Web專案連線Application Server之用
    https://ithelp.ithome.com.tw/upload/images/20251015/20128084xGNaUlO7eN.png
  2. Java設定
    選取齒輪中的settings,user是全域設定,workspace是工作區域會存在.vscode/settings.json
    https://ithelp.ithome.com.tw/upload/images/20251015/20128084LZH8OQk8jV.png
    往下可以看到maven設置位置
    https://ithelp.ithome.com.tw/upload/images/20251015/20128084dshsCGHzF4.png

maven專案創建

在你的workspace路徑列輸入code.cmd .,開啟以當前資料夾作為vscode開啟目錄
https://ithelp.ithome.com.tw/upload/images/20251015/20128084lhJbsQ2mSM.png
右鍵選取maven project
https://ithelp.ithome.com.tw/upload/images/20251015/20128084sOl841yVNW.png
選取常用的maven-archetype-webapp
https://ithelp.ithome.com.tw/upload/images/20251015/20128084p0aXJnRV2J.png
選取1.4版
https://ithelp.ithome.com.tw/upload/images/20251015/20128084k0NhrDWJzZ.png
輸入groupid
https://ithelp.ithome.com.tw/upload/images/20251015/201280847k92if0MNX.png
輸入artifactid
https://ithelp.ithome.com.tw/upload/images/20251015/20128084mFsKpvHmJd.png
選擇當前目錄直接enter
https://ithelp.ithome.com.tw/upload/images/20251015/20128084lSkFDo9ZVz.png
此時terminal執行maven命令
https://ithelp.ithome.com.tw/upload/images/20251015/20128084suM4HYI76d.png
於command line cd進入demo-vscode 再輸入code . -r會以當前vscode開啟demo-vscode為根目錄的專案
https://ithelp.ithome.com.tw/upload/images/20251015/20128084VLd1llUt1l.png
左邊editor右鍵可以將maven viewer叫出來
https://ithelp.ithome.com.tw/upload/images/20251015/2012808486bJ0kjNKF.png

maven專案匯入

直接在你的maven專案夾路徑直接輸入code.cmd .,vscode會依據pom文件識別為maven專案

maven基本使用

可以在左側maven view的地方選取lifecycle執行或是plugin goal執行
https://ithelp.ithome.com.tw/upload/images/20251015/20128084Z9OEE2hzzz.png
點選Favorites可以設定執行組合,筆者設定的是clean package -DskipTests(清理打包但不執行測試)
https://ithelp.ithome.com.tw/upload/images/20251015/20128084B4ZbvbKNQR.png
在pom檔透過 Command Palette (Ctrl+Shift+P) maven:add搜尋新增套件
https://ithelp.ithome.com.tw/upload/images/20251015/20128084e9n67yaTpS.png

跑起你的web專案

在左側欄servers view中選取servers view,創建application server
https://ithelp.ithome.com.tw/upload/images/20251015/20128084ADTiqPP3p4.png
選取Yes,會看到支援的application server,這邊選取tomcat11
https://ithelp.ithome.com.tw/upload/images/20251015/20128084MvwHMRfCQ7.png
deploy war檔,右鍵選取add deployment,選取target/demo-vscode.war
https://ithelp.ithome.com.tw/upload/images/20251015/20128084H1CbBbZDLl.png
訪問頁面,右鍵選取Server Action
https://ithelp.ithome.com.tw/upload/images/20251015/2012808458VJyEewVv.png
選取show in browser
https://ithelp.ithome.com.tw/upload/images/20251015/20128084fj2ovvhY63.png
選取訪問localhost:8080/demo-vscode
https://ithelp.ithome.com.tw/upload/images/20251015/20128084PvlhcHzogG.png
看到首頁
https://ithelp.ithome.com.tw/upload/images/20251015/20128084jkT53OYh3x.png

小結

今日講解了VS code整合maven的使用與相關配置,並講述VS code與application server整合並部署war檔

Reference


上一篇
Day29 - IntelliJ IDEA Integration with Maven
系列文
我們與Maven的距離31
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言