How to deploy WAR file into Tomcat server using Maven? One article will guide you through it!

Preface

Deploying WEB applications to Tomcat server is an inevitable part of Java WEB development. Maven, as an artifact of Java project management, can greatly simplify this process. This article will teach you how to use Maven to deploy WAR files to the Tomcat server.

Summary

This article describes how to use Maven to deploy WAR files to the Tomcat server. We will use the Tomcat Maven plugin for deployment. First, we need to add the Tomcat Maven plug-in to the project and specify the address, username and password of the Tomcat server in the plug-in configuration. Finally, we can use the Maven command to compile the project and deploy the WAR file to the Tomcat server.

text

Implementation steps

  1. Add the dependency of the Tomcat Maven plug-in in the project's pom.xml file.
  2. Use Maven commands to compile the project and generate a WAR file.
  3. Use the Tomcat Maven plug-in to deploy the WAR file to the Tomcat server.
  4. Enter "http://localhost:8080/myapp" in your browser to access the application.

Deployment process

Add Tomcat Maven plugin

We need to add the dependency of the Tomcat Maven plug-in in the project's pom.xml file.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat-maven-plugin</artifactId>
      <version>3.2</version>
      <configuration>
        <url>http://localhost:8080/manager/text</url>
        <server>tomcat</server>
        <path>/myapp</path>
        <username>admin</username>
        <password>admin</password>
      </configuration>
    </plugin>
  </plugins>
</build>

In the plug-in configuration, we need to specify the Tomcat server's address, user name, password, and project deployment path on the server.

Compile WAR file

Use Maven commands to compile the project and generate a WAR file.

$ mvn package

Deploy WAR file

Use the Tomcat Maven plug-in to deploy the WAR file to the Tomcat server.

$ mvn tomcat7:deploy

If the same application has been deployed before, the old version needs to be uninstalled before redeploying.

$ mvn tomcat7:undeploy

Run tests

We can access the application by entering "http://localhost:8080/myapp" in the browser.

code

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>myapp</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
      <!-- Add your dependencies here -->
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <url>http://localhost:8080/manager/text</url>
          <server>tomcat</server>
          <path>/myapp</path>
          <username>admin</username>
          <password>admin</password>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <repositories>
    <repository>
      <id>central</id>
      <url>http://central</url>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <url>http://central</url>
    </pluginRepository>
  </pluginRepositories>

  <distributionManagement>
    <repository>
      <id>example-repo</id>
      <name>example Repository</name>
      <url>scp://example.com/home/example/repo</url>
    </repository>
    <snapshotRepository>
      <id>example-snapshot-repo</id>
      <name>example Snapshot Repository</name>
      <url>scp://example.com/home/example/snapshot-repo</url>
    </snapshotRepository>
  </distributionManagement>

  <profiles>
    <profile>
      <id>profile-1</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <property>
          <name>some.property</name>
          <value>some value</value>
        </property>
      </properties>
    </profile>
  </profiles>

  <servers>
    <server>
      <id>tomcat</id>
      <username>admin</username>
      <password>admin</password>
    </server>
  </servers>

</project>

test case

We can write a simple Servlet to test whether the deployment is successful, as shown below:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {
    
    

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    
    
    response.getWriter().println("Hello, World!");
  }

}

Code analysis

pom.xml is the core configuration file of the Maven project, which is used to define the basic information, dependencies, plug-ins, and build and deployment-related settings of the project. The configuration items in the above pom.xml include:

  • Basic project information: groupId represents the organization or company to which the project belongs, artifactId represents the unique identifier of the project, and version represents the version number of the project.
  • Dependencies: Add the required dependencies in the dependencies tag. No dependencies are added here.
  • Plug-ins: Add the required plug-ins in the plugins tag. The tomcat-maven-plugin plug-in is added here to deploy the project to the Tomcat server.
  • Warehouse: Define the Maven warehouse address, where the address of the central warehouse is defined.
  • Publishing settings: Define relevant information published to the remote warehouse, such as warehouse name, warehouse address, etc.
  • Profile: A profile named profile-1 is defined and set as the default activated profile. In this configuration file, a property named some.property is defined and its value is set to some value.
  • Server settings: A server named tomcat is defined in the servers tag for deploying projects to the Tomcat server.

HelloWorld is a simple Servlet. When receiving a GET request, it will return a string of "Hello, World!" to the client.

summary

This article describes how to use Maven to deploy a WAR file into a Tomcat server. We use the Tomcat Maven plug-in for deployment, and complete the deployment by specifying the Tomcat server's address, user name, password and other information in the plug-in configuration. Finally, we compile the project through the Maven command and deploy the WAR file to the Tomcat server.

Summarize

This article describes how to use Maven to deploy a WAR file to a Tomcat server. The deployment is completed by adding the Tomcat Maven plug-in and specifying the Tomcat server's address, user name, password and other information in the plug-in configuration. At the same time, we compile the project through the Maven command and deploy the WAR file to the Tomcat server, and use a simple Servlet to test whether the deployment is successful. These steps greatly simplify the process of WEB application deployment and improve development efficiency.

Guess you like

Origin blog.csdn.net/weixin_43970743/article/details/133497456