How to create Spring MVC project using Maven command
- Details
- Written by Nam Ha Minh
- 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:
mvn archetype:generate ^ -DarchetypeGroupId=co.ntier ^ -DarchetypeArtifactId=spring-mvc-archetype ^ -DarchetypeVersion=1.0.2
Note that this command is quite long so I use the line-continuation character ^ (on Windows). On Linux, the line-continuation character is backslash (\):
mvn archetype:generate \ -DarchetypeGroupId=co.ntier \ -DarchetypeArtifactId=spring-mvc-archetype \ -DarchetypeVersion=1.0.2
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
Then it asks you to confirm (press Y to confirm):
Confirm properties configuration: groupId: net.codejava artifactId: MySpringMvc version: 1.0 package: net.codejava Y: : Y
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.xml file 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:
mvn archetype:generate ^ -DarchetypeGroupId=co.ntier ^ -DarchetypeArtifactId=spring-mvc-archetype ^ -DarchetypeVersion=1.0.2 ^ -DgroupId=net.codejava ^ -DartifactId=SpringMvcApp ^ -Dversion=1.0 ^ -Dpackage=net.codejava.spring ^ -DinteractiveMode=false
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.
Related Spring Tutorials:
- How to create a Spring MVC project using Maven and Eclipse in one minute
- Spring MVC beginner tutorial with Spring Tool Suite IDE
- How to bootstrap a Spring Web MVC application programmatically
Other Spring Tutorials:
- Spring Dependency Injection Example (XML)
- Understand the core of Spring framework
- Understand Spring MVC
- Understand Spring AOP
- Spring MVC Form Handling Tutorial
- Spring MVC Form Validation Tutorial
- 14 Tips for Writing Spring MVC Controller
- Spring Web MVC Security Basic Example (XML Configuration)
- Understand Spring Data JPA with Simple Example
- Spring MVC + Spring Data JPA + Hibernate - CRUD Example
Comments