Maven tomcat plugin for hot deployment, maventomcat
Preface:
The method here applies to Tomcat 6 and tomcat 7. The latest Tomcat 8 has not been tested yet. If you are interested, you can test it on your own.
There are a total of five steps:
1. Configure user permissions in tomcat to add an Administrator Account
2. Add the server to maven and configure the tomcat Administrator account and password.
3. Add the plug-in to the project and the server configured in maven,
4. Set deployment commands
5. Deployment
Related errors and Solutions
The steps are as follows:
1. Configure user permissions in tomcat to add an administrator account.
We need to implement hot deployment. Naturally, we need to use maven to operate tomcat. Therefore, we need maven to obtain the permissions to operate tomcat. Now this step is to configure the operational permissions of tomcat.
In the tomcat installation directory, modify the conf/tomcat-user.xml file and add the following configuration under the <tomcat-users> node:
<role rolename="manager-gui" /><role rolename="manager-script" /><user username="tomcat" password="tomcat" roles="manager-gui, manager-script" />
2. Add the server to maven and configure the tomcat Administrator account and password.
Now That tomcat has enabled the permission, maven has to get the tomcat Administrator account and password to operate tomcat.
In the maven installation directory, modify the conf/setting. xml file. Add the following configuration under the <server> node:
<server> <id>admin</id> <username>tomcat</username> <password>tomcat</password> </server>
3. Add plug-ins to the project and the server configured in maven,
Now maven has the permission to operate tomcat, but the communication between the two requires a bridge, that is, configuring the tomcat plug-in maven.
Modify the pom. xml file of the project and add the following configuration under the <build> node:
<Plugins> <! -- Method 1: apache official tomcat plug-in, supporting deploy --> <plugin> <groupId> org. apache. tomcat. maven </groupId> <artifactId> tomcat7-maven-plugin </artifactId> <version> 2.0-SNAPSHOT </version> <configuration> <url> http: // localhost: 8080/manager/text </url> <server> admin </server> </configuration> </plugin> <! -- Method 2: The third-party tomcat plug-in supports redeploy --> <plugin> <groupId> org. codehaus. mojo </groupId> <artifactId> tomcat-maven-plugin </artifactId> <version> 1.1 </version> <configuration> <url> http: // localhost: 8080/manager/text </url> <server> admin </server> <ignorePackaging> true </ignorePackaging> </configuration> </plugin> </plugins>
For the above plug-ins, there are two types of plug-ins available for use after testing, but note that the above configuration can only be used for Tomcat 7. If you want to use Tomcat 6, you need to configure it as follows:
<Plugins> <! -- Apache official tomcat plug-in, supporting deploy --> <plugin> <groupId> org. apache. tomcat. maven </groupId> <artifactId> tomcat6-maven-plugin </artifactId> <version> 2.0-SNAPSHOT </version> <configuration> <url> http: // localhost: 8080/manager/html </url> <server> admin </server> </configuration> </plugin> <! -- Third-party tomcat plug-in, supports redeploy --> <plugin> <groupId> org. codehaus. mojo </groupId> <artifactId> tomcat-maven-plugin </artifactId> <version> 1.1 </version> <configuration> <url> http: // localhost: 8080/manager </url> <server> admin </server> <ignorePackaging> true </ignorePackaging> </configuration> </plugin> </plugins>
Either Tomcat 7 or Tomcat 6, you only need to use one of the plug-ins. However, the two plug-ins are different:
The first type is the official apache plug-in, which supports the deploy command. If it has been deployed to the tomcat container, an error will be reported during the second deployment, prompting that the project has been deployed.
The second is a third-party plug-in. You can use the redeploy command to redeploy the plug-in.
At the same time, add the repository configuration under the <project> node. Otherwise, the plug-in may not be found, resulting in an error:
<repositories><repository><id>people.apache.snapshots</id><url>http://repository.apache.org/content/groups/snapshots-group/</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>apache.snapshots</id><name>Apache Snapshots</name><url>http://repository.apache.org/content/groups/snapshots-group/</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository></pluginRepositories>
Iv. set deployment commands
In eclipse, you can right-click the project to be deployed, Run as-> Run deployments-> maven build-> right-click new to configure a new maven command.
Specific configuration command method:
1. Select your project in base directory
2. Goals Configuration
If you use the official apache plug-in, use the "tomcat7: deploy" command.
If a third-party plug-in is used, use the "tomcat: redeploy" command.
5. Deployment
Right-click your project and choose Run as> Maven build.
Note:
Some users are not used to using IDE, so the same is true if maven is used in the command line.
Run the command to enter your project folder and use the mvn command, for example, mvn tomcat7: deploy.
Attached errors and solutions:
1. Connection refused Error
The error message is as follows:
[ERROR] Failed to execute goal org. apache. tomcat. maven: tomcat7-maven-plugin: 2.0-SNAPSHOT: deploy (default-cli) on project helloworld: Cannot invoke Tomcat manager: Connection refused: connect-> [Help 1]
Cause: the Tomcat server is not started.
Solution: first start the Tomcat server and then select Run
2. error 401
The error message is as follows:
[ERROR] Failed to execute goal org. apache. tomcat. maven: tomcat7-maven-plugin: 2.0-SNAPSHOT: deploy (default-cli) on project helloworld: Cannot invoke Tomcat manager: Server returned HTTP response code: 401 for URL: http: // localhost: 8080/manager/text/deploy? Path = % 2 Fhelloworld-> [Help 1]
Cause: permission issues
Solution in $ CATALINA_BASE/conf/tomcat-users.xml,
For example, add permissions to the D: \ apache-tomcat-7.0.34 \ conf \ tomcat-users.xml File
<Role rolename = "manager"/>
<User username = "admin" password = "admin" roles = "manager"/>
Modify the pom. xml file and add it in <configuration> </configuration>.
<Username> admin </username> <password> admin </password>
Error 3.403
The error message is as follows:
[ERROR] Failed to execute goal org. apache. tomcat. maven: tomcat7-maven-plugin: 2.0-SNAPSHOT: deploy (default-cli) on project helloworld: Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL: http: // localhost: 8080/manager/html/deploy? Path = % 2 Fhelloworld-> [Help 1]
Cause: the problem may be caused by two reasons. For details, refer to the solution.
Solution:
1) if Tomcat 7 is used, modify the pom. modify <url> http: // localhost: 8080/manager </url> to <url> http: // localhost: 8080/manager/text </url>
2) To assign tomcat user permissions, you must have both the manager-gui and manager-script permissions. In this case, I forgot to assign the manager-script permissions.
The correct conf/tomcat-users.xml configuration should be:
<Tomcat-users>
<Role rolename = "manager-gui"/>
<Role rolename = "manager-script"/>
<User username = "admin" password = "admin" roles = "manager-gui, manager-script"/>
</Tomcat-users>