Create a Maven version of the Web project and package the war package
Article directory
- Create a Maven version of the Web project and package the war package
-
- 1. Description
- 2. Operation
- 3. Generated pom.xml
- 4. The directory structure of the generated Web project
- 5. Create Servlet
-
- ① Create a java directory under the main directory
- ②Create the directory of the package where the Servlet class is located in the java directory
- ③ Create a Servlet class under the package
- ④ Register Servlet in web.xml
- 6. Write a hyperlink in the index.jsp page
- 7. Compile
- 8. Configure dependencies on the servlet-api.jar package
- 9. Package the web project as a war package
- 10. Deploy the war package to run on Tomcat
- 11. Finally: Thanks
1. Description
When using the mvn archetype:generate command to generate a web project, you need to use a special archetype. This archetype, which specifically generates the skeleton of a web project, can refer to the official website to see its usage:
Parameters archetypeGroupId, archetypeArtifactId, archetypeVersion are used to specify the coordinates of the currently used maven-archetype-webapp. And in the previous chapter in the packaging jar
: groupld, artfactld, version means the coordinates have the same meaning:
- archetypeGroupId: The reverse order of the company or organization domain name, usually with the project name
- For example: com.baidu.maven, positive sequence: domain name like maven.baidu.com, the domain name you know is com
- archetypeArtifactId: The name of the module, which will be used as the project name of the Maven project in the future
- archetypeVersion: The version number of the module, set according to your needs
- For example: SNAPSHOT indicates the snapshot version, which is in the process of iteration, and the unstable version. If you don’t choose, Maven will default to this.
- For example: RELEASE means the official release
2. Operation
Note: Similarly, if you want to execute the plugin in Maven, you need to be in the path of the pom.xml
file.
Note: It is impossible to create multiple pom.xml
files in a Maven project/module, that is to say, it cannot be created in an existing Maven In the project/module of strong>, if you create a project/module of Maven, you must create a new project/module of Maven in a new folder
If you execute the mvn archetype:generate command in the directory of the previous project, Maven will report an error: Cannot create other projects under a non-pom project. So don’t create a new project in the project just created, please go back to the root directory of the workspace to operate.
Here we create a new Maven project/module in E:\Maven_workspane\spaceVideo, named: pro02-maven-web
Enter the E:\Maven_workspane\spaceVideo path, execute the command: execute mvn archetype:generate
to create a Maven project/module
Note: Creating a web project is: execute the following command, you can just copy and paste it, no modification is required.
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4
Then run the command to generate the project:
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4
Follow the prompts to perform the following operations:
Define value for property 'groupId': com.rainbowSea.maven Define value for property 'artifactId': pro02-maven-web Define value for property 'version' 1.0-SNAPSHOT: :【Enter directly, use the default value】 Define value for property 'package' com.rainbowSea.maven: :【Enter directly, use the default value】 Confirm properties configuration: groupId: com.rainbowSea.maven artifactId: pro02-maven-web version: 1.0-SNAPSHOT package: com.rainboweSea.maven Y: :【Enter directly to confirm】
3. Generated pom.xml
As follows in our: E:\Maven_workspane\spaceVideo directory to generate a: pro02-maven-web file.
There is also one: pom.xml
the configuration file of the project.
Open it to confirm whether the packaging method is in the form of war
package
<packaging>war</packaging>
4. The directory structure of the generated Web project
There is index.jsp
under the webapp directory, and web.xml
under the WEB-INF directory. It is the same as the directory structure we learned in Tomcat.
5. Create Servlet
①Create a java directory under the main directory
Because our Maven did not automatically create a directory structure corresponding to java
for us, we need to create it manually:
Create a java folder
②Create the directory of the package where the Servlet class is located in the java directory
Our path is: E:\Maven_workspane\spaceVideo\ pro02-maven-web\src\main\java\com\rainbowSea\maven
③ Create a Servlet class under the package
package com.rainbowSea.maven; // Note that the package name needs to be written import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import java.io.IOException; public class HelloServlet extends HttpServlet{ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("hello maven web"); } }
④ Register Servlet in web.xml
Register Servlet in: pro02-maven-web\src\main\webapp\WEB-INF\web.xml
<servlet> <servlet-name>helloServlet</servlet-name> <servlet-class>com.rainbowSea.maven.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloServlet</servlet-name> <url-pattern>/helloServlet</url-pattern> </servlet-mapping>
6. Write a hyperlink on the index.jsp page
In: E:\Maven_workspane\spaceVideo\pro02-maven-web\src\main\webapp\index.jsp path
<html> <body> <h2>Hello World!</h2> <a href="helloServlet">Access Servlet</a> </body> </html>
The full name of JSP is Java Server Page, which, like Thymeleaf, is a server-side page rendering technology. Here we don’t need to care about the details of JSP syntax, just write a hyperlink tag.
7. Compile
At this point, an error occurs when the mvn compile command is executed directly:
The package javax.servlet.http does not exist Package javax.servlet does not exist symbol not found Symbol: class HttpServlet ...
The above error message shows: Our Web project uses the HttpServlet class, and the HttpServlet class belongs to the servlet-api.jar
jar package. At this point we say that the Web project needs to rely on the servlet-api.jar
package.
8. Configure dependencies on the servlet-api.jar package
For dependencies that do not know the details, you can go to the https://mvnrepository.com/ website to check. Use keywords to search, and then select the appropriate application in the search result list.
For example, we found the dependency information of servlet-api:
The copied information depends on the jar package of javax.servlet, and the coordinates are as follows:.
<!--In order to use HttpServlet normally, you need to import servlet-api dependencies--> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
In this way, the above information can be added to pom.xml. Execute the mvn compile command again.
After the configuration is complete, press Ctrl + S to save the following, and we will re-execute mvn compile
to compile.
Here we execute:
mvn clean compile # First delete the .class folder target we generated earlier, and then recompile, so that the compiled one is the latest.
9. Package the web project as a war package
Run the mvn package command to generate the war package as shown in the following figure:
mvn package
10. Deploy the war package to run on Tomcat
Copy the war package to the Tomcat server we installed: Tomcat/webapps directory
Start Tomcat:
Try to access through browser: http://localhost:8080/pro02-maven-web/index.jsp
OK, it works fine.
Explanation: The war
package we encapsulated has no problem and can be executed normally.
11. Finally: Thanks
This article refers to the following blogger’s sharing. Here again, we sincerely thank the bloggers for their enthusiastic sharing of their technology.
Thanks to the following bloggers for sharing
[1]: Weapon | Code Rework