Jenkins部署150MB WAR包至Tomcat 9耗时过长问题求助
Hey there, sorry to hear you're stuck with these frustrating deployment hiccups—let's walk through some actionable fixes and ideas to smooth out your pipeline.
1. Fixing the "WAR Uploaded but Jenkins Stuck Waiting" Issue
This is almost always tied to either Tomcat's processing bottlenecks or timeout mismatches between Jenkins and Tomcat. Here's what to check first:
Verify Tomcat Manager's timeout & resource settings
Beyond themultipart-configyou already adjusted, check Tomcat'sserver.xmlfor the Connector handling Manager requests. Increase timeouts to give Tomcat enough time to process the large WAR:<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="300000" <!-- 5 minutes, up from default 20s --> keepAliveTimeout="60000" maxThreads="200" />Also, bump up Tomcat's heap memory via
CATALINA_OPTS(inbin/setenv.shorsetenv.bat) to handle large WAR unpacking and app initialization:export CATALINA_OPTS="-Xmx2g -Xms1g -XX:MaxMetaspaceSize=512m"Check Jenkins Deployment Plugin Timeouts
If you're using the "Deploy to container" plugin, double-check its timeout settings. By default, it might only wait 120 seconds—way too short for a 140MB WAR's initialization. Increase this to 300+ seconds in the plugin's configuration for your deployment step.Inspect Tomcat Logs in Real-Time
When Jenkins gets stuck, tailcatalina.outandlocalhost.logon your Tomcat server. You might see that Tomcat is still unpacking the WAR, initializing Spring beans, or running database migrations—all of which take time for large apps. If this is the case, optimizing your app's startup time (e.g., lazy-loading non-critical components) will help long-term.Validate Multipart Config is Active
Make sure you edited the correctweb.xml(it should beTOMCAT_HOME/webapps/manager/WEB-INF/web.xml) and restarted Tomcat. Test a manual upload via curl to rule out Jenkins-specific issues:curl -u your-tomcat-username:password -T your-app.war http://tomcat-host:8080/manager/text/deploy?path=/your-app&update=trueIf this succeeds without hanging, the problem is likely in Jenkins' plugin configuration.
2. Eliminating Manual Cleanup & Restarts
The need to manually delete old WARs and restart Tomcat usually means your deployment step isn't handling app replacement properly:
Enable Auto-Undeploy in Jenkins
In your "Deploy to container" step, check the box labeled Undeploy before deploy—this tells Tomcat to remove the existing app before pushing the new WAR. Alternatively, add theupdate=trueparameter to your deployment URL (like the curl example above) to trigger an in-place update.Add an Automated Tomcat Restart Step
If your app requires a restart after deployment (common with legacy apps), add an SSH build step to Jenkins to run a graceful restart script on the Tomcat server:# Gracefully shut down Tomcat /opt/tomcat/bin/shutdown.sh # Wait for processes to exit (adjust sleep time as needed) sleep 15 # Kill any lingering Tomcat processes pkill -f tomcat # Start Tomcat back up /opt/tomcat/bin/startup.shFor extra reliability, add a step to delete the old app directory from
webappsbefore deploying, to avoid leftover files causing conflicts.Check for Locked Files
Sometimes Tomcat holds locks on JARs or config files from the old app, preventing replacement. If you still see issues after auto-undeploy, add a step to stop Tomcat before deploying, push the WAR, then start it back up. This ensures no files are in use during the update.
3. Optimizing Overall Deployment Speed
To cut down on total pipeline time, try these tweaks:
Parallelize Build & Analysis
If you're using Jenkins Pipeline, run SonarQube analysis and Jacoco tests in parallel with your build (if they don't depend on each other). Example snippet:stage('Build & Test') { parallel { stage('Build') { steps { sh 'mvn clean package' } } stage('SonarQube Analysis') { steps { sh 'mvn sonar:sonar' } } } }Cache Dependencies
Use Jenkins' built-in dependency caching (for Maven/Gradle) or plugins like Pipeline Cache to avoid re-downloading dependencies every build. This can shave minutes off your build time.Incremental Builds
Configure Jenkins to only build modules that have changed (if you're working with a multi-module project). Plugins like Incremental Build can help with this.
内容的提问来源于stack exchange,提问作者Aurelius Baier

