VSCode Java
Overview
VSCode is free, open source IDE. We look at some of the tips and tricks to work with VSCode for java developers
Github: https://github.com/gitorko/project61
VSCode
Feature 1: Explore Git Clone & Spring Init commands
You can use the command palette (Ctrl+Shift+P) to clone repositories, or create new projects using start.spring.io integration.
Feature 2: Explore Java language support
Use language support to avoid typing main: 'public static void main' or sysout: 'System.out.println'
Feature 3: Explore the Gradle Exentison
View the gradle tasks
Feature 4: Hide files you dont wish to view
Add this to settings.json
1"files.exclude": {
2 "**/.classpath": true,
3 "**/.DS_Store": true,
4 "**/.factorypath": true,
5 "**/.git": true,
6 "**/.gitignore": true,
7 "**/.gradle": true,
8 "**/.hg": true,
9 "**/.mvn": true,
10 "**/.project": true,
11 "**/.settings": true,
12 "**/.sts4-cache": true,
13 "**/.svn": true,
14 "**/.vscode": true,
15 "**/.idea": true,
16 "**/out": true,
17 "**/bin": true,
18 "**/build": true,
19 "**/CVS": true,
20 "**/gradle": true,
21 "**/target": true,
22 "**/.attach_pid*": true,
23 "**/logs": true
24}
Feature 5: Replace tabs with white spaces
Add this to settings.json
1"editor.renderWhitespace": "all",
2"editor.insertSpaces": true,
Feature 6: Increase page length to 120
Add this to settings.json to specify line length.
1"editor.rulers": [
2 120
3]
Feature 7: Decide on import order
Add this to settings.json to specify the import order.
1"java.completion.importOrder": [
2 "java",
3 "javax",
4 "org",
5 "com"
6 ],
Use Right click Source Action->Organize Imports or (Alt+Shift+O)
Feature 8: Enable eclipse formatting
Enable specific formatter.
1 "java.format.settings.url" :"file:///Users/home/dev_code-style_formatter.xml",
Use (Ctrl+Shift+I) to format
Feature 9: Install Lombok plugin
Avoid writing boilerplate code with lombok.
Lombok Annotations Support for VS Code
Feature 10: Add license info to each file
This will add a license header to the file.
Add this to settings.json
1"licenser.customHeader": "Copyright (c) 2021 Company, Inc. All Rights Reserved.",
2"licenser.customTermsAndConditions": "",
3"licenser.license": "Custom",
4"licenser.useSingleLineStyle": false,
5"licenser.author": "Company",
Use command palette to insert license
Feature 11: Explore java dependency tree
View the dependency tree of the project.
Feature 12: Explore Git
Check git blame inline and view git commits. View the git graph to visualize the tree.
Feature 13: Explore unit test support & debug unit tests
Run the unit tests and view the report.
Feature 14: Explore checkstyle support
View the inline highlight feature. Make the settings change in the workspace instead of global user settings file so that this applies only to the specific project.
1"java.checkstyle.configuration": "${workspaceFolder}/config/checkstyle/checkstyle.xml"
Feature 15: Explore inline code coverage
You need xml report enabled for this to work, check build.gradle, after the build the jacocoTestReport.xml is generated that is read by the coverage extension to highlight lines of code not covered by unit tests.
If the coverage file name is different then change the settings.json
1"coverage-gutters.xmlname": "jacocoTestReport.xml",
Feature 16: Explore Debugging and Hot Code Replacement/Hot Swap
Dock the debugger tool bar.
1"debug.toolBarLocation": "docked"
1curl --location --request GET 'http://localhost:9090/api/age/10-10-2020' --header 'Content-Type: application/json'
To enable hot code replace set the following properties, for spring boot projects with dev tools the reload is automatic, if dev tools is not present in the project then you can use Hot code replacement (HCR), which doesn’t require a restart, is a fast debugging technique in which the Java debugger transmits new class files over the debugging channel to the JVM. Make sure 'java.autobuild.enabled' is enabled.
1"java.debug.settings.hotCodeReplace": "auto",
2"java.autobuild.enabled" : true
Feature 17: Explore Spring Boot Support
Start or debug spring boot application
Get spring property support
Feature 18: Create shortcut to key bindings to build project
For gradle projects instead of running ./gradlew build each time in terminal you can map it to a task and give a keyboard shortcut.
Add this to the tasks.json, everytime you run a task called 'run' it will build the project.
1{
2 "version": "2.0.0",
3 "tasks": [
4 {
5 "label": "build",
6 "type": "shell",
7 "command": "./gradlew clean build",
8 "group": "none"
9 }
10 ]
11}
Now lets create a shortcut goto "Keyboard Shortcuts" and click on '{}' icon. Add this to keybindings.json, now press F6 to build the project
1[
2 {
3 "key": "f6",
4 "command": "workbench.action.tasks.runTask",
5 "args" : "build"
6 }
7]
Feature 19: Reading user input
To take user input from command line you need to change shell type in launch.json config to integratedTerminal
1{
2 "type": "java",
3 "name": "CodeLens (Launch) - Main",
4 "request": "launch",
5 "mainClass": "com.demo.project61.Application",
6 "projectName": "project61",
7 "console": "integratedTerminal"
8}
Feature 20: Explore Docker
Build the docker image
Run the docker image
Tag the docker image and push it to public docker hub registry. You need to run docker login before pushing the image.
1docker login
Push the image
Feature 21: Explore Kubernetes
Deploy to kubernetes cluster
View the deployments
Feature 22: Explore Rest Client
Explore Reset client Thunder Client
Feature 23: Sync the settings
Link the accounts to sync the settings
Feature 24: Connect to DB and query
Query a database
Feature 25: Use live share
Share your workspace
Feature 26: Explore Open API
Create Open API spec file and test it
Feature 27: Shortcuts
Goto Implementation - (Ctrl + F12) Goto Terminal - (Ctrl + ~ ) Quick Fix - (Ctrl + . )
Problems
Often times workspace gets corrupted so I delete the storage in %APPDATA%\Code\User\workspaceStorage and restart the IDE to get things back in order. Clean the workspace directory
Windows : %APPDATA%\Code[ - Variant]\User\workspaceStorage
MacOS : $HOME/Library/Application Support/Code[ - Variant]/User/workspaceStorage/ Linux : $HOME/.config/Code[ - Variant]/User/workspaceStorage/Another problem often seen is when multiple project exist on workspace but if one of them fails to build then all the projects in the workspace wont work. So for now keep one workspace to one project mapping.
Maven Execution
If you need to execute maven project from command line, You need to add org.codehaus.mojo.exec-maven-plugin in your pom.xml
1<plugin>
2 <groupId>org.codehaus.mojo</groupId>
3 <artifactId>exec-maven-plugin</artifactId>
4 <version>1.6.0</version>
5</plugin>
Then configure task by 'Ctrl+Shift+P' then 'Tasks: Configure task' and select the project. Edit the tasks.json
1{
2 "version": "2.0.0",
3 "tasks": [
4 {
5 "label": "run",
6 "type": "shell",
7 "command": "mvn exec:java '-Dexec.mainClass=com.myproject.Main'",
8 "group": "none"
9 }
10 ]
11}
Plugins recommended
To export the plugins you use
1code --list-extensions > extensions.list
To install all plugins at one time
1cat extensions.list |% { code --install-extension $_}
extensions.list
142Crunch.vscode-openapi
2Angular.ng-template
3CoenraadS.bracket-pair-colorizer-2
4DavidAnson.vscode-markdownlint
5dbaeumer.vscode-eslint
6DotJoshJohnson.xml
7eamodio.gitlens
8eg2.vscode-npm-script
9GabrielBB.vscode-lombok
10golang.go
11hashicorp.terraform
12humao.rest-client
13jim-moody.drools
14johnpapa.vscode-peacock
15mhutchie.git-graph
16ms-azuretools.vscode-docker
17ms-kubernetes-tools.vscode-kubernetes-tools
18ms-ossdata.vscode-postgresql
19ms-python.python
20ms-python.vscode-pylance
21ms-toolsai.jupyter
22ms-vscode-remote.remote-containers
23ms-vscode-remote.remote-ssh
24ms-vscode-remote.remote-ssh-edit
25ms-vscode-remote.remote-ssh-explorer
26ms-vscode-remote.remote-wsl
27ms-vscode-remote.vscode-remote-extensionpack
28ms-vscode.js-debug-nightly
29ms-vscode.vscode-typescript-next
30ms-vscode.vscode-typescript-tslint-plugin
31ms-vsliveshare.vsliveshare
32msjsdiag.debugger-for-chrome
33msjsdiag.vscode-react-native
34mtxr.sqltools
35mtxr.sqltools-driver-pg
36naco-siren.gradle-language
37Pivotal.vscode-spring-boot
38PKief.material-icon-theme
39rangav.vscode-thunder-client
40redhat.java
41redhat.vscode-commons
42redhat.vscode-xml
43redhat.vscode-yaml
44richardwillis.vscode-gradle
45richardwillis.vscode-gradle-extension-pack
46richardwillis.vscode-spotless-gradle
47ryanluker.vscode-coverage-gutters
48shengchen.vscode-checkstyle
49VisualStudioExptTeam.vscodeintellicode
50vscjava.vscode-java-debug
51vscjava.vscode-java-dependency
52vscjava.vscode-java-pack
53vscjava.vscode-java-test
54vscjava.vscode-maven
55vscjava.vscode-spring-boot-dashboard
56vscjava.vscode-spring-initializr
57vscode-icons-team.vscode-icons
58xabikos.JavaScriptSnippets
59ymotongpoo.licenser
60zhuangtongfa.material-theme