My Java & Maven Setup on VSCode
Have you ever used VSCode? It’s an open-sourced code editor backed by Microsoft. It allows some really nice flexibility thanks to its extensions and ecosystem. I’ve been using it for TypeScript, Rust, Python and it’s definitely my top favorite editor.
But the problem is, I recently started learning Java and I’ve heard that Java support on VSCode is sort of infamous for being incomplete. That’s probably why many people would prefer fully-featured IDEs like IntelliJ. But it can be pretty useful if you do some setups! I’ll tell you how I did mine!
Prerequisites
Java and Maven must be installed to follow this tutorial. To install them, refer to links below.
- Java Downloads
- Downloading Apache Maven
- https://maven.apache.org/download.cgi
- How to Install Apache Maven on Windows?
Extensions
First, I installed some extensions for language feature support.
Language Support
- Language Support for Java(TM) by Red Hat
- Debugger for Java
- Project Manager for Java
- Test Runner for Java
Package Manager
Formatting
This post was useful for setting up Google’s style guide.
https://dev.to/marcushellberg/how-to-configure-vs-code-java-formatting-1p10
Usage Example: Project Initialization
You can create a new project with the help of “Maven for Java” extension. Simply run “Maven: New Project…” in the command palette.
If you wish to create a Spring project, try “Spring Initializr: Create a Maven Project…” instead.
Usage Example: Add Dependencies
For adding dependencies, you could use “Maven: Add a dependency…” but I prefer using Maven Repository.
In the Maven Repository website, you can simply search the name of the dependency…
select a version…
and copy-paste the declaration into your pom.xml file.
Then maybe click Yes or Always! Selecting Always will update “.vscode/settings.json“.
Usage Example: Code Execution
To run your code, you can either click “Run -> Run Without Debugging,”
or run this following command in your terminal.
mvn exec:java -Dexec.mainClass="fully-qualified-class-name"
* Example of fully qualified class Name: com.mycompany.app.App
That’d be it! You’re now ready to work on VSCode. Hope you found this post useful!
P.S. Don’t forget to add “Java Projects” in the sidebar. It has some useful tools.
Leave a Reply