Tomcat 9应用启动与重启方法及自定义部署配置重复执行失效问题咨询
Hi there! Let's tackle your questions one by one—starting with basic Tomcat operations, then diving into fixing your deployment and auto-reload problem.
1. How to Start & Restart Apps in Tomcat 9
Starting Tomcat
- Command Line: Navigate to your Tomcat
bindirectory:- Windows: Run
startup.bat(foreground) orcatalina.bat start(background) - Linux/macOS: Run
./startup.sh(foreground) or./catalina.sh start(background)
- Windows: Run
- Windows Service: If you installed Tomcat as a Windows service, open the Services panel, find the Tomcat service, and click "Start".
Restarting Apps
- Full Tomcat Restart: Stop Tomcat first with
shutdown.bat/./shutdown.sh, then restart using the startup commands above. Or usecatalina.bat restart/./catalina.sh restartfor a one-step restart. - Reload Specific App via Manager Console:
- Enable the Tomcat Manager app (if not already enabled) by configuring users in
conf/tomcat-users.xml. - Access
http://localhost:8080/manager/html, log in, find your app, and click Reload to restart it without stopping the entire server.
- Enable the Tomcat Manager app (if not already enabled) by configuring users in
2. Fixing Your Deployment & Auto-Reload Problem
What's Causing the Issue?
When the mytestapp folder exists in webapps, Tomcat prioritizes this unpacked directory over the docBase war path you defined in server.xml. Tomcat's auto-deploy system automatically creates contexts for unpacked apps in webapps, which override your custom Context configuration.
Solution 1: Adjust server.xml Configuration
Modify your Host and Context settings to force Tomcat to use your specified war and config, and ignore the existing mytestapp directory:
<Host name="localhost" appBase="webapps" autoDeploy="true" deployOnStartup="true"> <!-- Your custom context: points to the war in myapp_1, uses the external config --> <Context path="/mytestapp" docBase="c:/Apache-tomcat/webapps/myapp_1/mytestapp.war" debug="0" reloadable="true" override="true" antiResourceLocking="true" unpackWARs="true"> <Environment name="spring.config.location" value="file:c:/Apache-tomcat/webapps/myapp_1/application.properties" type="java.lang.String"/> </Context> <!-- Disable auto-deployment of the existing mytestapp directory --> <Context path="/mytestapp" docBase="webapps/mytestapp" reloadable="false" enabled="false"/> </Host>
override="true": Forces your customContextto override any auto-generated contexts for the same path.antiResourceLocking="true": Fixes file locking issues on Windows that prevent replacing the war file.unpackWARs="true": Ensures Tomcat unpacks the war towebapps/mytestappand replaces the old directory when the war is updated.
Solution 2: Use a Context XML File (Recommended)
Instead of modifying server.xml (which requires a Tomcat restart), create a separate context file in conf/Catalina/localhost/mytestapp.xml:
<Context docBase="c:/Apache-tomcat/webapps/myapp_1/mytestapp.war" reloadable="true" antiResourceLocking="true" unpackWARs="true"> <Environment name="spring.config.location" value="file:c:/Apache-tomcat/webapps/myapp_1/application.properties" type="java.lang.String"/> </Context>
This approach:
- Lets you deploy/modify the context without restarting Tomcat.
- Automatically overrides any unpacked
mytestappdirectory inwebapps. - Tomcat will monitor the war file's modification time; when you replace it, Tomcat will delete the old
mytestappdirectory, unpack the new war, and reload the app automatically.
3. Verification Steps
- Stop Tomcat and delete the
webapps/mytestappdirectory (if it exists). - Apply either solution above (update
server.xmlor create the context XML file). - Start Tomcat and verify the app is deployed correctly, using the
application.propertiesfrommyapp_1. - Replace
myapp_1/mytestapp.warwith your new version. Wait 10-30 seconds (Tomcat's default monitoring interval) and check if the app reloads automatically with the new code.
内容的提问来源于stack exchange,提问作者Sergey_USB

