Maven 部署项目到 Tomcat
来源:原创 发布时间:2015-04-29 归档:maven
环境 :
JDK 7
Maven 3
Tomcat 7
Eclipse Luna
首先需要配置好一个 Tomcat 的账户, 并给该账户分配管理权限的角色 :
tomcat-users.xml
<tomcat-users> <role rolename="manager-gui"/> <role rolename="manager-script"/> <user username="admin" password="admin" roles="manager-gui,manager-script"/> </tomcat-users>
角色列表可在 %tomcat7%\webapps\manager\WEB-INF\web.xml 配置文件中找到, 下面是摘自其中的一个片段 :
<security-constraint> <web-resource-collection> <web-resource-name>HTML Manager interface (for humans)</web-resource-name> <url-pattern>/html/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>manager-gui</role-name> </auth-constraint> </security-constraint> <security-constraint> <web-resource-collection> <web-resource-name>Text Manager interface (for scripts)</web-resource-name> <url-pattern>/text/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>manager-script</role-name> </auth-constraint> </security-constraint> <security-constraint> <web-resource-collection> <web-resource-name>JMX Proxy interface</web-resource-name> <url-pattern>/jmxproxy/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>manager-jmx</role-name> </auth-constraint> </security-constraint> <security-constraint> <web-resource-collection> <web-resource-name>Status interface</web-resource-name> <url-pattern>/status/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>manager-gui</role-name> <role-name>manager-script</role-name> <role-name>manager-jmx</role-name> <role-name>manager-status</role-name> </auth-constraint> </security-constraint>
Role | Desc |
---|---|
manager-gui | 允许访问 /manager/html 页面接口 ( Manager App ), 以及 Server Status 页面 |
manager-status | 只允许访问 Server Status 页面 |
manager-script | 允许访问 /manager/text 文本接口, 以及 Server Status 页面 |
manager-jmx | 允许访问 jmx 代理接口, 以及 Server Status 页面 |
<servers> <server> <id>tomcat</id> <username>admin</username> <password>admin</password> </server> </servers>项目 pom.xml 配置
<build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <server>tomcat</server> <url>http://localhost:8081/manager/text</url> </configuration> </plugin> </plugins> </build>
这里的 server 需与 settings.xml 中 tomcat server id 一致, 这样才能找到服务的账号和密码。url 指定到你的 /manager/text 接口路径, 注意不能是 /manager/html 接口路径, /manager/html 接口部署会报 403 的错误。
一切准备妥当, 启动 Tomcat, 执行 mvn tomcat7:redeploy 即可部署项目到外部 Tomcat 服务。
[INFO] . . . . . .
[INFO] <<< tomcat7-maven-plugin:2.2:redeploy (default-cli) < package @ simapp <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:redeploy (default-cli) @ simapp ---
[INFO] Deploying war to http://localhost:8081/simapp
Uploading: http://localhost:8081/manager/text/deploy?path=%2Fsimapp&update=true
Uploaded: http://localhost:8081/manager/text/deploy?path=%2Fsimapp&update=true (3 KB)
[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /simapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] <<< tomcat7-maven-plugin:2.2:redeploy (default-cli) < package @ simapp <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:redeploy (default-cli) @ simapp ---
[INFO] Deploying war to http://localhost:8081/simapp
Uploading: http://localhost:8081/manager/text/deploy?path=%2Fsimapp&update=true
Uploaded: http://localhost:8081/manager/text/deploy?path=%2Fsimapp&update=true (3 KB)
[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /simapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
更多 goals 信息可参考 :http://tomcat.apache.org/maven-plugin-2.2/context-goals.html