--- tags: maven, vscode, tutorial --- # 在 VS Code 上使用 Maven [TOC] 此分享其實適用於各大Editor,因為其實全都是用terminal來操作 不是用Extension內建的功能 ## 安裝好Java Extension Pack 就到Extention那邊搜尋java,然後裝好 ## 下載Maven 到Maven官網載好Maven然後放到PATH裡 如果有套件管理工具就看看有沒有能直接用的(ex. Mac: brew install maven) ## 生成一個Maven專案 - maven example的 - `mvn archetype:generate` - 我自己用的 - `mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4` 生成一個mvn的專案,照著走然後輸入groupid, artifact id, package就會跑出一堆檔案 ## ~~修理~~ 修改pom 開始編輯pom.xml 像是加dependency之類的 我這邊加了mongodb的driver ```xml <dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>4.0.3</version> </dependency> </dependencies> ``` 為了能用terminal快速測試code 我加了一個plugin ```xml <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <executable>java</executable> <arguments> <argument>-classpath</argument> <classpath /> <argument>這邊要放MainClass的完整path (ex. MainPackage.MainClass)</argument> </arguments> </configuration> </plugin> </plugins> </build> ``` ## 理論上這樣應該就能用了吧 code打好之後,在project的根目錄執行 `mvn clean compile exec:exec` 就會自己編譯跟執行了(灑花) 如果不想看maven的build訊息的話,可以加一個flag -q `mvn clean compile exec:exec -q` 就會只剩下程式的output~~跟一堆錯誤訊息~~
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.