maven部署專案到遠端伺服器
在專案部署的過程中,通常需要打war包,在將war包遠端拷貝到目的伺服器。操作有點麻煩,也是能接受。
不過有了一種更方便的操作:是IDEA通過maven將打好的war直接部署到遠端伺服器的步驟。
一、在安裝的Tomcat目錄下找到/conf/tomcat-user.xml這個檔案,在檔案的末尾新增以下ho紅色內容:
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="admin"/>
<role rolename="manager"/>
<user username="tomcat" password="tomcat"
roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui,admin,manager"/>
Ps:
manager-gui
允許訪問
manager-script
允許訪問純文字介面(即URL路徑為/manager/text/*)
manager-jmx
允許訪問JMX代理介面(即URL路徑為/manager/jmxproxy/*)
manager-status
允許訪問Tomcat只讀狀態頁面(即URL路徑為/manager/status/*)
其中紅色內容中username和password可以修改為自己合適的。
二、在web專案的pom檔案中新增:tomcat7-maven-plugin外掛,在pom檔案中新增如下紅色內容:
<!--tomcat外掛 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<!-- tomcat地址,manager/text該地址可追加命令-->
<!--http://localhost:8080/manager/stop?path=/ //停止--> <!--http://localhost:8080/manager/start?path=/ //啟動-->
<!--http://localhost:8080/manager/text/deploy?path=/ //部署-->
<!--http://localhost:8080/manager/text/undeploy?path=/ //解除安裝-->
<url>http://localhost:8080/manager/text</url><!--遠端伺服器url-->
<username>tomcat</username>
<password>tomcat</password>
<!-- 此處的名字是專案釋出的工程名-->
<path>/webDemo</path>
</configuration>
</plugin>
<!-- war外掛-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<webResources>
<resource>
<!-- WEB-INF檔案地址,idea是web,eclipse是webRoot-->
<directory>\src\main\webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
三、在確認你的伺服器上tomcat是開啟的狀態下,確認使用你配置的使用者名稱和密碼是可以登入http://localhost:8080/manager的(這是我測試使用的本地tomcat),否則會報錯。
Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.1:deploy (default-cli) on project webDemo: Cannot invoke Tomcat manager: Connection to http://localhost:8080 refused: Connection refused: connect -> [Help 1]
四、
成功登入後,即可遠端部署你的專案了,在你的web專案中使用命令即可:
mvn tomcat7:deploy
五、附上pom.xml檔案