部署基於Maven的war檔案到Tomcat


在本教學中,我們將學習如何使用Maven的Tomcat外掛打包並部署一個WAR檔案到Tomcat(Tomcat的6和7。

要用到工具:

  1. Maven 3
  2. Tomcat 6.0.37
  3. 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

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>

	<role rolename="manager-gui"/>
	<role rolename="manager-script"/>
	<user username="admin" password="password" roles="manager-gui,manager-script" />

</tomcat-users>


1.2 Maven 認證
新增在上面Maven 設定檔案的 Tomcat 使用者,是之後Maven使用此使用者來登入Tomcat伺服器。

%MAVEN_PATH%/conf/settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings ...>
	<servers>
	   
		<server>
			<id>TomcatServer</id>
			<username>admin</username>
			<password>password</password>
		</server>

	</servers>
</settings>


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

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>

	<role rolename="manager-gui"/>
	<role rolename="manager-script"/>
	<user username="admin" password="password" roles="manager-gui,manager-script" />

</tomcat-users>

2.2 Maven 認證

%MAVEN_PATH%/conf/settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings ...>
	<servers>
	   
		<server>
			<id>TomcatServer</id>
			<username>admin</username>
			<password>password</password>
		</server>

	</servers>
</settings>


2.3 Tomcat6 Maven 外掛

pom.xml

<plugin>
		<groupId>org.apache.tomcat.maven</groupId>
		<artifactId>tomcat6-maven-plugin</artifactId>
		<version>2.2</version>
		<configuration>
			<url>http://localhost:8080/manager</url>
			<server>TomcatServer</server>
			<path>/yiibaiWebApp</path>
		</configuration>
	</plugin>	


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] ------------------------------------------------------------------------

參考

  1. Apache Tomcat 7 Manager App HOW-TO
  2. Apache Tomcat 6 Manager App HOW-TO
  3. Tomcat Maven Plugin
  4. Tomcat Maven Plugin – Context Goals