During software development, sometimes we need to convert a normal Maven project into a web project so that it can be deployed into a web container to run. This article will explain in detail how to complete this conversion process in simple steps.
Preparation
Install the JDK: Make sure that your development environment has Java Development Kit (JDK) installed.
Install Maven: Make sure that Apache Maven is installed on your system and that environment variables are configured.
IDE: It is recommended to use integrated development environments such as IntelliJ IDEA or Eclipse that support Maven.
Step 1: Modify
First, open the file of your Maven project and add or modify the following:
1.1 Add packaged tags
Set the <packaging> tag to war, which means that the project will be packaged as a web application.
<packaging>war</packaging>
1.2 Adding Web Dependencies
In order for the project to run as a web application, the Servlet API and other necessary web dependencies need to be added. For example:
<dependencies> <!-- Servlet API --> <dependency> <groupId></groupId> <artifactId>-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- JSP API --> <dependency> <groupId></groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- Other dependencies as needed --> </dependencies>
1.3 Configuration plug-in
To generate a WAR file, you can configure Maven War Plugin. For example:
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.1</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build>
Step 2: Create a Web resource directory structure
In Maven projects, web resources are usually placed in the src/main/webapp directory. If the directory does not exist, create it manually.
2.1 Create a WEB-INF directory
Create a WEB-INF directory under the src/main/webapp directory to store configuration files for web applications, such as .
2.2 Writing
Create a file in the WEB-INF directory, which is the deployment descriptor for the web application. A simple example is as follows:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="/xml/ns/javaee" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <display-name>My Web Application</display-name> <servlet> <servlet-name>myServlet</servlet-name> <servlet-class></servlet-class> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/myServlet</url-pattern> </servlet-mapping> </web-app>
Step 3: Write Servlet
Create a simple Servlet class in the src/main/java directory. For example:
package ; import ; import ; import ; import ; import ; import ; public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ("text/html"); PrintWriter out = (); ("<h1>Hello, World!</h1>"); } }
Step 4: Build and deploy
4.1 Building a project
On the command line, navigate to the project root directory and run the following command to build the project:
mvn clean package
This will generate a WAR file, usually located in the target directory.
4.2 Deploy to a web container
Deploy the generated WAR file into a web container such as Tomcat. For example, copy the WAR file to Tomcat's webapps directory and start the Tomcat server.
cp target/ /path/to/tomcat/webapps/
Start Tomcat:
/path/to/tomcat/bin/
Visit the web application:
http://localhost:8080/my-web-app/myServlet
Method supplement
Method 1
How to convert a normal Maven project into a web project, including steps such as modifying, creating a web resource directory structure, writing servlets, and building and deploying. Hope it will be helpful to readers. Converting a Maven project to a web project (i.e. adding web application support) usually involves the following steps:
- Modify files: Add dependencies and plugins related to web applications.
- Create a web application directory structure: Make sure that the project contains a standard web application directory structure.
- Configure web applications: Write or modify files.
- Create Servlets, JSP and other web components.
Here is a concrete example, assuming you already have a basic Maven project structure.
1. Modify the file
First, open your file and add the necessary dependencies and plugins. For example, add the Tomcat plugin to run a web application during development.
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>my-web-app</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- Servlet API --> <dependency> <groupId></groupId> <artifactId>-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- JSP API --> <dependency> <groupId></groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId></groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>my-web-app</finalName> <plugins> <!-- Tomcat Plugin for running the web application --> <plugin> <groupId></groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
2. Create a web application directory structure
Make sure your project contains a standard web application directory structure. The typical directory structure of a Maven Web project is as follows:
my-web-app/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └──
│ │ ├── resources/
│ │ └── webapp/
│ │ ├── WEB-INF/
│ │ │ └──
│ │ └──
├──
3. Configure web applications
Create or modify files, configure Servlets and other web components.
<web-app xmlns="/xml/ns/javaee" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>myServlet</servlet-name> <servlet-class></servlet-class> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file></welcome-file> </welcome-file-list> </web-app>
4. Create Servlets, JSP and other web components
Create a Servlet
Create a simple Servlet in src/main/java/com/example/.
package ; import ; import ; import ; import ; import ; import ; @WebServlet("/hello") public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ("text/html"); ().println("<h1>Hello, World!</h1>"); } }
Create a JSP
Create a simple JSP page in src/main/webapp/.
<!DOCTYPE html> <html> <head> <title>My Web App</title> </head> <body> <h1>Welcome to My Web App</h1> <a href="hello" rel="external nofollow" >Say Hello</a> </body> </html>
5. Run a web application
Use the Maven command to start the Tomcat server and run the web application:
mvn tomcat7:run
Open the browser and visit http://localhost:8080/. You should be able to see the welcome page. After clicking the link, the Servlet will be called and "Hello, World!" will be displayed.
Method 2
Through the above steps, you have successfully converted a Maven project to a web project. Hope this example helps you! Converting a Maven project to a web project usually involves several steps, including configuring the structure of the project, modifying files to contain the dependencies and plugins required by the web application, and setting up the entry point for the web application (such as ) . Here are the detailed steps:
1. Modify the project structure
First, make sure your project structure complies with the standard directory structure of your web application. A typical web project structure is as follows:
my-web-app/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └──
│ │ ├── resources/
│ │ └── webapp/
│ │ ├── WEB-INF/
│ │ │ └──
│ │ └──
├──
src/main/java/: Stores Java source code.
src/main/resources/: Store resource files, such as configuration files, etc.
src/main/webapp/: Stores static resources and configuration files for web applications.
src/main/webapp/WEB-INF/: Deployment descriptors for storing web applications.
src/main/webapp/: The default homepage.
2. Modify
In, you need to add some specific dependencies and plugins to support the construction of your web application.
Add a web application packaging type
Change the package type of the project from jar to war:
<packaging>war</packaging>
Add web application dependencies
Add necessary web application dependencies according to your needs. For example, if you use the Servlet API, you can add the following dependencies:
<dependencies> <dependency> <groupId></groupId> <artifactId>-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- Other dependencies --> </dependencies>
Adding Maven plugin
To support the construction of web applications, you may need to add some Maven plugins such as maven-war-plugin:
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.1</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <!-- Other plug-ins --> </plugins> </build>
3. Create
Create a file in the src/main/webapp/WEB-INF/ directory. This is a standard web application deployment descriptor file for configuring Servlets, filters, listeners, etc.
<web-app xmlns="/xml/ns/javaee" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class></servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/myservlet</url-pattern> </servlet-mapping> <!-- Other configurations --> </web-app>
4. Write Servlet
Create a simple Servlet class in the src/main/java/com/example/ directory:
package ; import ; import ; import ; import ; import ; import ; public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ("text/html"); PrintWriter out = (); ("<h1>Hello, World!</h1>"); } }
5. Build and deploy
Build the project with Maven and generate the WAR file:
mvn clean package
The generated WAR file will be located in the target/ directory. You can deploy this WAR file to any server that supports Java web applications, such as Tomcat, Jetty, etc.
6. Run the project
If you are using Tomcat, you can copy the generated WAR file to Tomcat's webapps/ directory and start Tomcat:
cd /path/to/tomcat/bin ./
Access your web application, for example:
http://localhost:8080/my-web-app/myservlet
In this way, you successfully convert a Maven project into a web project.
The above is a detailed article about how to convert a maven project into a web project. For more information about converting a maven project to a web project, please pay attention to my other related articles!