Last Updated on 20 February 2020   |   Print Email
In this post, you will learn how to quickly create a Spring MVC project from a Maven’s archetype in command line prompt (on Windows) or terminal (Linux, Mac).You know, one of tedious issues with Spring framework is that it requires a lot of boilerplate code for configuration - just to setup non-business stuffs. Though Spring Boot can speed up initial development time by implementing sensible defaults under the hood, sometimes we still need to create a Spring MVC project in the plain way (a standard Java web application that can be deployed on a servlet container like Tomcat).First, make sure that you have Maven installed on your computer and setup the system PATH environment variable so you can run Maven’scommands from anywhere.Change the current directory to the one where you want to put your Spring MVC project, for example:
cd g:\Java\Spring
And type the following command to create a Spring MVC project:
Note that this command is quite long so I use the line-continuation character ^ (on Windows). On Linux, the line-continuation character is backslash (\):
Depend on your Maven, it might download necessary JAR files from Maven’s central repository. And then it will be generating project in interactive mode – asking for your project’s information, for example:
Define value for property 'groupId': net.codejava
Define value for property 'artifactId': MySpringMvc
Define value for property 'version' 1.0-SNAPSHOT: : 1.0
Define value for property 'package' net.codejava: : net.codejava
Once confirmed, Maven will generate your Spring MVC project (BUILD SUCCESS):
Note that this INFO line:
Project created from Archetype in dir: G:\Java\Spring\MySpringMvc
That means the Spring MVC project named MySpringMvc has been create successfully. Import this project in Eclipse, you will see the following project structure:
Explore the project and you can see it generated the following files:
pom.xmlfile that specifies dependencies for Spring MVC framework, Serlvet & JSP API, Spring test…
web.xml that configures Spring Dispatcher servlet.
MvcConfiguration class that configures Spring MVC’s view resolver and resources handler.
HomeController class is for Spring MVC controller with a handler method for the application’s context root.
home.jsp file is the homepage of the application.
Watch this video to see the steps visually:However, since the spring-mvc-archetype was created long time ago, it uses old versions of Java, Spring, Servlet, JSP and some dependencies are no longer needed. So I recommend you to watch the following video to see how to update the Spring MVC project with new versions (Java 11, Spring framework 5, Servlet 4, etc):BONUS: You can create your Spring MVC project faster by specifying all necessary information in one command. For example:
That’s how to use Maven’s command to quickly create a Spring MVC project. You can also use the project as a guide to your first, hello world Spring MVC application.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.