Friday, December 28, 2012

Maven - How to deploy WAR to vFabric TC Server remotely using Maven build?

Four easy steps on How to deploy the WAR file to vFabric TC 2.8.* server  or Tomcat 7 remotely using Maven build (pom.xml)

Step 1: Open your tomcat-user.xml from your <CATALINA_HOME>/conf folder and insert the code:

Code:
  <?xml version="1.0" encoding="UTF-8"?>
  <tomcat-users>
     <role rolename="manager-gui" />
     <role rolename="manage-script" />
     <role rolename="manager-status" />
     <role rolename="manager-jmx" />
     <user username="adminuser" password="adminpwd" roles="manager-gui" />
  </tomcat-users>

Note: Role name script, status and jmx are optional. In previous version of tomcat, you can use role "manager" but for tomcat 7, it was divided into four role. For depolyment, we need only "manager-gui".

Step 2: Update your Maven settings.xml with the following code. To check where is your maven settings located, just open a command prompt and type maven -X and it will show the global settings and user settings. I choose global settings since I will be allowing other users to use the same settings.

Open <MAVEN_HOME>/conf/settings.xml then insert the code:

Code:
  <?xml version="1.0" encoding="UTF-8"?>
  <settings>
   <servers>
     <server>
        <id>remoteserver</id>
        <username>adminuser</username>
        <password>adminpwd</password>
     </server>
   </servers>
  </settings>


Step 3: Open your pom.xml and insert the following code

  Code:
  <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <!-- for tomcat 6 -->
      <!--
      <artifactId>tomcat6-maven-plugin</artifactId>
      -->
      <artifactId>tomcat7-maven-plugin</artifactId>
      <configuration>
         <url>http://<remote_ip_address>:<port>/manager/html</url>
         <server>remoteserver</server>
         <path>/your_web_context_path_here</path>
      </configuration>
  </plugin>


Note: if you have a parent pom.xml you need to insert the following code to your parent pom.xml

Code:
  <plugin>
     <groupId>org.apache.tomcat.maven</groupId>
     <artifactId>tomcat7-maven-plugin</artifactId>
     <version>2.0</version>
  </plugin>


Step 4: Deploy or Redeploy
  Run maven command to deploy
  Code:
   mvn tomcat7:deploy
  For redeploy:
  Code:
   mvn tomcat7:redeploy

6 comments:

Anonymous said...

This post really helps me and save time. I've been searching about remote deployment and I found some forum but this one is better because it's plain and simple.

Thanks for sharing.

Unknown said...

I just can't get this to work. The tomcatManager just vomits a "status code:403, ReasonPhrase:Forbidden" followed by a web page that talks something about that the HTML interface has CSRF (Cross Site Request Forgery) protection:

[INFO]
[INFO] <<< tomcat7-maven-plugin:2.0:deploy (default-cli) @ spring-mvc-experiment <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.0:deploy (default-cli) @ spring-mvc-experiment ---
[INFO] Deploying war to http://localhost:8080/spring-mvc-experiment
Uploading: http://localhost:8080/manager/html/deploy?path=%2Fspring-mvc-experiment
Uploaded: http://localhost:8080/manager/html/deploy?path=%2Fspring-mvc-experiment (3977 KB at 68562.1 KB/sec)

[INFO] tomcatManager status code:403, ReasonPhrase:Forbidden
[INFO] <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
[INFO] <html>

... (lots of HTML omitted for brevity)

I'm pretty sure that I've specified the correct password, username and role ("manager-gui"). What am I missing here?

Unknown said...

Ok, here are the changes I made to Ray's code above, to make it work for me:

1. In the file "tomcat-users.xml" I had to assign adminuser to roles="manager-script", NOT roles="manager-gui" as Ray does.

2. In the file "settings.xml" I had to surround Ray's code with <settings> and <servers> elements, thus:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
<servers>
<server>
<id>remoteserver</id>
<username>adminuser</username>
<password>adminpwd</password>
</server>
</servers>
</settings>

3. In the file "pom.xml", I had to use "manager/text" (NOT "manager/html") in the <url> element:
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<url>http://<remote_ip_address>:<port>/manager/text</url>
<server>remoteserver</server>
<path>/your_web_context_path_here</path>
</configuration>
</plugin>
</plugins>
</build>
</project>

i360 said...

@Mikko - thanks for the feedback and nice catch. I missed the and tag. I have updated my post with tags as per your comment except for the URL. I used HTML since it's inside a firewall and I have configured a web filter to allow specific IP address to access the URL.

i360 said...

I just tried it for vfabricTC 2.9.3 and the latest Tomcat version, it works as well.

James said...

Thanks for sharing. I just tried it in latest Eclipse Kepler version, it works as well.