部署基於Maven的war文件到Tomcat
在本教程中,我們將學習如何使用Maven的Tomcat插件打包並部署一個WAR文件到Tomcat(Tomcat的6和7。
要用到工具:
- Maven 3
- Tomcat 6.0.37
- Tomcat 7.0.53
Tomcat 7
發佈URL = http://localhost:8080/manager/text
命令 = mvn tomcat7:deploy
Tomcat 6
發佈 URL = http://localhost:8080/manager/
命令 = mvn tomcat6:deploy
1. Tomcat 7 示例
這個例子說明了如何在Tomcat7打包並部署WAR文件。
1.1 Tomcat 認證
添加具有角色管理器GUI和管理腳本的用戶。
%TOMCAT7_PATH%/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="admin" password="password" roles="manager-gui,manager-script" />
1.2 Maven 認證
添加在上面Maven 設置文件的 Tomcat 用戶,是之後Maven使用此用戶來登錄Tomcat服務器。
%MAVEN_PATH%/conf/settings.xml
<settings ...>
<server>
<id>TomcatServer</id>
<username>admin</username>
<password>password</password>
</server>
</servers>
1.3 Tomcat7 Maven 插件
聲明一個Maven的Tomcat插件。
pom.xml
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/yiibaiWebApp</path>
</configuration>
</plugin>
怎麼運行的?
在部署過程中,它告訴 Maven 通過部署 WAR 文件Tomcat服務器, 「http://localhost:8080/manager/text」 , 在路徑「/yiibaiWebApp「上, 使用 「TomcatServer」 (settings.xml) 用戶名和密碼來進行認證。
1.4 發佈到Tomcat
以下的命令是用來操縱Tomcat WAR文件。
mvn tomcat7:deploy
mvn tomcat7:undeploy
mvn tomcat7:redeploy
示例
> mvn tomcat7:deploy
...
[INFO] Deploying war to http://localhost:8080/yiibaiWebApp
Uploading: http://localhost:8080/manager/text/deploy?path=%2FyiibaiWebApp&update=true
Uploaded: http://localhost:8080/manager/text/deploy?path=%2FyiibaiWebApp&update=true (13925 KB at 35250.9 KB/sec)
[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /yiibaiWebApp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.507 s
[INFO] Finished at: 2015-08-05T11:35:25+08:00
[INFO] Final Memory: 28M/308M
[INFO] ------------------------------------------------------------------------
2. Tomcat 6 示例
這個例子說明了如何在Tomcat6打包和部署WAR文件。這些步驟和Tomcat7是一樣的,只是部署URL和命令名稱不同。
2.1 Tomcat 認證
%TOMCAT6_PATH%/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="admin" password="password" roles="manager-gui,manager-script" />
2.2 Maven 認證
%MAVEN_PATH%/conf/settings.xml
<settings ...>
<server>
<id>TomcatServer</id>
<username>admin</username>
<password>password</password>
</server>
</servers>
2.3 Tomcat6 Maven 插件
pom.xml
2.4 發佈到Tomcat
mvn tomcat6:deploy
mvn tomcat6:undeploy
mvn tomcat6:redeploy
示例
> mvn tomcat6:deploy
...
[INFO] Deploying war to http://localhost:8080/yiibaiWebApp
Uploading: http://localhost:8080/manager/deploy?path=%2FyiibaiWebApp
Uploaded: http://localhost:8080/manager/deploy?path=%2FyiibaiWebApp (13925 KB at 32995.5 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.652 s
[INFO] Finished at: 2014-08-05T12:18:54+08:00
[INFO] Final Memory: 30M/308M
[INFO] ------------------------------------------------------------------------