DEV Community

Cover image for Spring MVC on VSCode - Maven Project
Osazuwa J. Agbonze
Osazuwa J. Agbonze

Posted on

Spring MVC on VSCode - Maven Project

In this post i'll explain how i was able to setup spring mvc on vscode using maven project.

Disclaimer
This guide only contains setup process and project structuring, all completely handled by maven.

If you don't have vscode installed, you'll have to install it from https://code.visualstudio.com/download.

Install OpenJDK

For this process i'll be using jdk-15 which you can download from https://jdk.java.net/15/ . I'm on windows platform so i downloaded the build for Windows/x64 as specified in the download page.

On successful download, unzip and set jdk's installation path (location to place the unzipped file) as C:\Program Files\Java\jdk-15.0.1.

Apache Maven Installation

We'll be using apache maven for project management which you can download from http://maven.apache.org/download.cgi. Maven version used in this guide is version 3.6.5. On successful download, unzip file and set it's installation path (location to place the unzipped file) to C:\Program Files\Apache\apache-maven-3.6.3

Copy the bin path of installed apache maven which should be C:\Program Files\Apache\apache-maven-3.6.3\bin and include it to PATH variable in system configuration. This will enable mvn (maven) command to be accessed from terminal as well as other maven commands.

Needful VSCode Extension

For development ease, we'll need to install Java Extension Pack which will help in providing intelligent code completion, debugging, language support and other features to give us the feel of developing with an IDE.

Open up vscode and click the extension icon on the sidebar (or use Cntrl + Shift + X). Search for Java Extension Pack on the search field and install it.

Creating Maven Web Project

Open vscode command palette from View -> Command palette on the menu bar ( or use Cntrl + Shift + P ). Search for Java: Create Java Project on the search field and choose the option from the list. Another view will be displayed to choose your project type, one of which is Maven. Select the Maven option. We're interested in web project, so select the maven-archtype-webapp from the archtype view.

Maven will run on vscode terminal and install all necessary dependencies for a basic webapp project. During the process you'll be prompted to interactively enter project's groupId, artifactId, Version, package name.

For this process, i used these values (you don't have to use same)

groupId : com.spaceofmiah
artifactId: blog
Enter fullscreen mode Exit fullscreen mode

I used default values for the others.

With the above details maven will automatically create a well structured webapp project.

Adding Spring Dependency

To add spring mvc dependencies, open vscode command palette from View -> Command pallet on the menu bar ( or use Cntrl + Shift + P ). Search for Maven: Add a dependency select it from the list of command. Another search field will be opened to search for dependencies. Search for

1) spring-core with groupId org.springframework
2) spring-beans with groupId org.springframework
3) spring-context with groupId org.springframework

Make sure the above dependencies are from org.springframework groupId.

NOTE: for each dependendcy search, make sure to select the appropriate dependency from the list of dependencies. This will automatically add the choosen dependencies to pom.xml.

Caution: After adding a dependency, pom.xml file might open. When this happens save the file with Cntrl + S and close the file. This is just to avoid structural errors in pom.xml.

To install the added dependencies, opening vscode command palette from View -> Command palette on the menu bar ( or use Cntrl + Shift + P ). Search for Maven: Execute Commands. This will open another view where you can select install command from the options listed.

Java Home Settings

There are various ways to allow our java application access our jdk we installed earlier.

1) Copy the installation path C:\Program Files\Java\jdk-15.0.1. create JAVA_HOME environment variable in system configurations and set it's value to the jdk path copied.

2) Add the copied jdk installation path to PATH variable in system configurations

3) user settings on vscode can also be used to configure jdk installation path. Open vscode command palette from View -> Command pallet on the menu bar ( or use Cntrl + Shift + P ). Search for Java: Configure Java Runtime. This should open an interface from which you can set jdk installation path.

Build And Compile Task

To compile or build project, in project root if you don't already have .vscode directory ( which is automatically created by vscode ), create it and within the directory create a tasks.json file containing

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "verify",
            "type": "shell",
            "command": "mvn -B verify",
            "group": "build"
        },
        {
            "label": "test",
            "type": "shell",
            "command": "mvn -B test",
            "group": "test"
        },
        {
            "label": "compile",
            "type": "shell",
            "command": "mvn -B test",
            "group": "build"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

With that set, you can run compile task to build project by first opening vscode command palette from View -> Command pallet from the menu bar ( or use Cntrl + Shift + P ). Search for Tasks: Run Task, select compile option, which will compile your source code into a .war file.

Warnings & Errors Encountered

Compiler compliance warning

Open up pom.xml, check if the version specified for maven.compiler.source and maven.compiler.target corresponds with the version of jdk installed, if not, change it to the version of jdk installed which is 15. (This step corrected the warning)

You've successfully setup a web application using maven on vscode that support spring mvc. Stay tuned for the next guide which will take this a step further. Drop your thoughts, questions or suggestions in the comment sections below. Click on the like button and share article with a friend. Thanks

source code can be gotten from github

Top comments (1)

Collapse
 
gregoryjohnson profile image
Gregory Johnson

Thank you for this concise and informative article.