In the past, the usual way to publish a Java Web application was to put it into a WAR package, then use a tool like SSH to upload it to the server, put it in the appropriate directory, and let Tomcat automatically unpack and complete the deployment.
Obviously, this is not convenient, and we might make a mistake when we use SSH to drag files up. (Of course, big factories don’t have this problem because they have an operation and maintenance team dedicated to doing this, haha)
Now what I want is a one-line command to deploy to the local server, test it locally, and if there is no problem, one-line command to deploy to the official server. Besides, only I know the password of the official server, and only I can execute the deployment (the other developers do not know the password of the official server).
OK, let’s first introduce this plugin in Maven(pom.xml) :
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
The tomcat7-Maven-plugin uses Tomcat’s “manager” module to deploy the WAR package, so make sure Tomcat has the manager module installed. (Check webApp for Manager directory) It’s named “Tomcat7”, but Tomcat8 also works, and I’m using Tomcat8. Add the following configuration to the local Tomcat manager module:
1
The user named “deployer” has a role of “manager-script” which indicates that the user can use the backend script management of Tomcat’s Manager module, BTW, if the role is “manager-gui” it means that the user can use the front-end web management of Manager module. Then do a similar configuration for the official server.
The tomcat7-Maven-plugin can be configured as follows:
OK, now to explain:
Configuration-server The Server in the Configuration is a variable called tomcat.deploy. Server. I didn’t write it dead because we are going to deploy the WAR package to different servers. As you can see, tomcat.deploy.server is assigned to localTestServer in properties, which is our default value, but we can change it to something else by adjusting Maven’s run parameters. So what does localTestServer stand for? As I mentioned earlier, the server password is set by myself and I don’t want anyone to know it. Other developers can decide their own password for the local server, but they don’t need to know my password, so the localTestServer content is defined in “~/.m2/settings.xml”. In Windows7, this is usually in the “C:\Users(user name).m2\settings.xml” area. Open the configuration file and add two servers to the Servers on which we will deploy the application. ProductionServer deployer 123456 localTestServer deployer 654321 this configuration is very self-explanatory. Configuration-url The URL in the Configuration is also a variable, and obviously I can’t write it out, but it’s not as sensitive as the user name and password, so it doesn’t matter if it appears directly in pom.xml. By default, Its value is “http://localhost/manager/text”, this address is the Tomcat Manager module background scripts entrance, the value of the url can also be through Maven operation parameters adjustment. Where does configuration-Path deploy the WAR package? To specify a path, if path is “/”, which is deployed as the legendary root.war, this time we specify a path named MyWebAppDemo. After successful deployment can be access via http://localhost/MyWebAppDemo. The profile parameter, also known as Maven’s parameter, specifies a parameter called deploy2Production, which is used to run MVN with tomcat.deploy. Server, Tomcat.deploy. serverUrl instead of the default value. OK, all that work is done, just deployment. Here we go: 1mvn Tomcat7: Redeploy Why Redeploy and not deploy? Because using deploy will cause some problems in my office, I won’t say what the specific problems are, maybe your office is normal, if there is no problem, you can also use deploy. Once this is done, our application is deployed to the local Tomcat server. Again, this is MVN with parameters: 1MVN Tomcat7: redeploy-pdeploy2Production so our application is deployed to the official server. If you want to remove the program, you can simply use undeploy instead of redeploy.
Welcome Java engineers who have worked for one to five years to join Java architecture development: 854393687
Group provides free Java architecture learning materials (which have high availability, high concurrency, high performance and distributed, Jvm performance tuning, Spring source code, MyBatis, Netty, Redis, Kafka, Mysql, Zookeeper, Tomcat, Docker, Dubbo, multiple knowledge architecture data, such as the Nginx) reasonable use their every minute and second time to learn to improve yourself, don’t use “no time” to hide his ideas on the lazy! While young, hard to fight, to the future of their own account!