SSM projects are frequently deployed as war packages, using tomcat and maven to implement hot deployment configuration

SSM projects are frequently deployed as war packages, using tomcat and maven to implement hot deployment configuration

background

As we all know, after we develop a JavaEE project, we need to deploy the project to the server's tomcat. The common deployment method is to package the project into a war package and put it under Tomcat's webapps, then restart Tomcat, and then access it through the IP address + port number. There is no problem with this deployment itself, but the problem is that if you are still in a production environment and need to frequently change and optimize the project, you will need to frequently package the project into a war package and replace the war package under webapps, which is cumbersome.

Next, we will describe how to implement local programming and then deploy the project to the remote server's tomcat to achieve hot deployment.

Technology & Tools Used

  • Maven (project building and dependency management)
  • tomcat7 plugin (plugin deployed to tomcat)
  • tomcat server (web server)
  • The compiler recommended is IDEA

1. Ensure that the local computer has permission to use remote Tomcat

Modify the {TOMCAT_HOME}conf/tomcat-users.xml configuration file under Tomcat and add the user name, password, and permissions.

<role rolename="manager-gui" />
<role rolename="manager-script" />
<role rolename="admin-gui" />
<role rolename="admin-script" />
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,admin-gui,admin-script"/>

2. Configure Tomcat to allow remote access

Create a manager.xml file in the remote server's {TOMCAT_HOME}conf/Catalina/localhost/ directory and configure the following:

<?xml version="1.0" encoding="UTF-8"?>
<Context privileged="true" antiResourceLocking="false" docBase="${catalina.home}/webapps/manager">
     <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />
</Context>

Note: If you only want some users to use it, you can configure the IP in allow , for example

allow="192.168.0.102"

3. Restart remote Tomcat

Execute in the bin directory of tomcat

//Shut down tomcat
./shutdown.sh
//Start tomcat
./startup.sh

4. Test whether you have permission to use

Access tomcat, for example http://192.168.0.102:8080 (use your own server or virtual machine's IP address)
Click Manager APP


insert image description here

Enter the tomcat account and password just configured

insert image description here

If you jump to this page, the configuration is complete.

insert image description here

Of course, you can also deploy and replace war on the current page, which is another way of deployment, but it is still not as convenient as hot deployment.

Question: If a 403 error occurs, the following

403 Access Denied

You are not authorized to view this page.
 
By default the Manager is only accessible from a browser running on the same machine as Tomcat. If you wish to modify this restriction, you'll need to edit the Manager's context.xml file.
 
If you have already configured the Manager application to allow access and you have used your browsers back button, used a saved book-mark or similar then you may have triggered the cross-site request forgery (CSRF) protection that has been enabled for the HTML interface of the Manager application. You will need to reset this protection by returning to the main Manager page. Once you return to this page, you will be able to continue using the Manager application's HTML interface normally. If you continue to see this access denied message, check that you have the necessary permissions to access this application.
 
If you have not changed any configuration files, please examine the file conf/tomcat-users.xml in your installation. That file must contain the credentials to let you use this webapp.

solve

Modify the /webapps/manager/META_INF/context.xml file and comment out the restricted source settings in the file.

<Context antiResourceLocking="false" privileged="true" >
 <!--Comment here to remove the setting of access rights <Valve className="org.apache.catalina.valves.RemoteAddrValve"
     allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
     -->
</Context>

Then just refresh the page without restarting tomcat

5. Configure the remote Tomcat administrator account in Maven

Add the following content to the node in the local Maven {MAVEN_HOME}/conf/settings.xml file:

<!-- Configure the username and password that can operate tomcat-->
<server>
  <id>crocutax</id>
  <!-- server login name -->
  <username>tomcat</username>
  <!-- server login password -->
  <password>tomcat</password>
</server>

6. Configure Maven's tomcat7 plugin in the project

<!-- Configure Tomcat plugin-->
<plugin>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat7-maven-plugin</artifactId>
	<version>2.2</version>

	<configuration>
		<!-- The name here must be consistent with the id of the server node configured in {MAVEN_HOME}/conf/settings.xml-->
		<server>crocutax</server>
		<!--Server port number-->
		<port>8080</port>
		<!-- The path for project publishing is the tomcat/webapps directory by default, and you can specify a deeper directory.
		If you leave "/", the ROOT.war package will be deployed in the webapps directory by default.
		<path></path>
		<!-- Note that the URL of tomcat7 cannot be modified at will, and the suffix must be text, not html.
		 If it is a local tomcat deployment, you can use localhost or ip.
		<url>http://localhost:8080/manager/text</url>
		<!--<url>http://117.62.110.110:8080/manager/text</url>-->
		<!--Solve the problem of garbled Chinese parameters-->
		<uriEncoding>UTF-8</uriEncoding>
		<update>true</update>
		<!--Configure the user name defined in tomcat\conf\tomcat-users.xml -->
		<username>tomcat</username>
		<password>tomcat</password>
	</configuration>
</plugin>
  • server : The name must be consistent with the id of the server node configured in {MAVEN_HOME}/conf/settings.xml
  • port : server port number
  • path : The path where the project is published. The default directory is tomcat/webapps. You can specify a deeper directory. If you leave "/", the ROOT.war package will be deployed in the webapps directory by default.
  • url : Note that the url here in tomcat7 cannot be modified at will, and the suffix must be text, not html. If it is a local tomcat deployment, you can use localhost and ip. uriEncoding : Solve the problem of garbled Chinese parameters
  • update : hot deployment, otherwise an error will be reported later
  • Username : Configure the user name defined in {TOMCAT_HOME}\conf\tomcat-users.xml
  • password : The password defined in {TOMCAT_HOME}\conf\tomcat-users.xml

7. Start Maven's tomcat deployment command in the project

For initial deployment, you can use the "tomcat7:deploy" command (used when there is no Root folder under tomcat's webapps)

If you have already deployed, use the command "tomcat7:redeploy"
If you encounter project conflicts, you can use the command
-DskipTests means skip the test

clean tomcat7:redeploy -DskipTests

When using it, an error occurs that the file cannot be found. Recompile or package it.

Use IDEA as shown below


insert image description here

Of course, you can also configure quick start


insert image description here

You can also use IDE->Terminal or the project root directory to open a DOS window and enter the Maven command

At this point, the hot deployment of tomcat+maven has been configured, and you no longer have to worry about the cumbersome packaging and deployment.

Summarize

The above is the SSM project that I introduced to you. It is frequently deployed in war packages and hot deployment configuration is achieved using tomcat and maven. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • idea2020.3 detailed tutorial on configuring Maven environment and configuring Tomcat
  • Detailed tutorial on Java (JDK/Tomcat/Maven) runtime environment configuration and tool (idea/eclipse) installation
  • IDEA configures the Java development environment (maven, gradle, tomcat)
  • Maven project remote deployment && How to configure database connection using tomcat
  • Idea configures maven-tomcat-plugin to implement project deployment
  • Import Maven Web project into Eclipse and configure it to run in Tomcat
  • Two ways to configure Tomcat in Maven projects

<<:  WeChat applet implements fixed header and list table components

>>:  Examples of simple add, delete, modify, and query operations using mysql statements

Recommend

How to implement paging query in MySQL

SQL paging query:background In the company's ...

Detailed explanation of MySQL InnoDB secondary index sorting example

Sorting Problem I recently read "45 Lectures...

Detailed explanation of Vue element plus multi-language switching

Table of contents Preface How to switch between m...

How to create users and manage permissions in MySQL

1. How to create a user and password 1. Enter the...

Understanding v-bind in vue

Table of contents 1. Analysis of key source code ...

Detailed steps to install VMware Tools from scratch (graphic tutorial)

VMware Tools is a tool that comes with VMware vir...

A detailed introduction to JavaScript primitive values ​​and wrapper objects

Table of contents Preface text Primitive types Pr...

Detailed explanation of MLSQL compile-time permission control example

Preface The simple understanding of MySQL permiss...

Example of how rem is adapted for mobile devices

Preface Review and summary of mobile terminal rem...

View the frequently used SQL statements in MySQL (detailed explanation)

#mysql -uroot -p Enter password mysql> show fu...

JavaScript to achieve full screen page scrolling effect

After I finished reading JavaScript DOM, I had a ...

Seven solutions for classic distributed transactions between MySQL and Golan

Table of contents 1. Basic theory 1.1 Transaction...

Complete steps to quickly configure HugePages under Linux system

Preface Regarding HugePages and Oracle database o...

WeChat Mini Programs Achieve Seamless Scrolling

This article example shares the specific code for...