Deploying Web Applications
You have already seen how to deploy a Web application for in-place Web development in the previous section, so now the focus will be on deploying a packaged WAR to your target container. This example uses the Cargo Maven plugin to deploy to any container supported by Cargo (see http://cargo.codehaus.org/Containers). This is very useful when you’re developing an application and you want to verify it works on several containers.
The web module’s pom.xml file has the following added for Cargo configuration:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<container>
<containerId>${containerId}</containerId>
<zipUrlInstaller>
<url>${url}</url>
<installDir>${installDir}</installDir>
</zipUrlInstaller>
</container>
<configuration>
<properties>
<cargo.servlet.port>8280</cargo.servlet.port>
</properties>
</configuration>
</configuration>
</plugin>
As you can see this is a configuration similar to the one you have used to deploy your EJBs in the Deploying EJBs section of this chapter. There are two differences though:
- Two new properties have been introduced (
containerIdandurl) in order to make this build snippet generic. Those properties will be defined in a Profile. - A
cargo.servlet.portelement has been introduced to show how to configure the containers to start on port 8280 instead of the default 8080 port. This is very useful if you have containers already running your machine and you don’t want to interfere with them.
As seen in the Deploying EJBs section the installDir property is user-dependent and should be defined in a profiles.xml file. However, the containerId and url properties should be shared for all users of the build. Thus the following profiles have been added to the web/pom.xml file:
[...]
</build>
<profiles>
<profile>
<id>jboss4x</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<containerId>jboss4x</containerId>
<url> http://ovh.dl.sourceforge.net/sourceforge/jboss/jboss4.0.2.zip </url>
</properties>
</profile>
<profile>
<id>tomcat5x</id>
<properties>
<containerId>tomcat5x</containerId>
<url>http://www.apache.org/dist/jakarta/tomcat-5/v5.0.30/bin/ jakarta-tomcat-5.0.30.zip</url>
</properties>
</profile>
</profiles>
</project>
You have defined two profiles: one for JBoss and one for Tomcat and the JBoss profile is defined as active by default (using the activation element). You could add as many profiles as there are containers you want to execute your Web application on.
Executing mvn install cargo:start generates the WAR, starts the JBoss container and deploys the WAR into it:
C:devm2bookcodej2eedaytraderweb>mvn install cargo:start [...] [INFO] [cargo:start] [INFO] [talledLocalContainer] Parsed JBoss version = [4.0.2] [INFO] [talledLocalContainer] JBoss 4.0.2 starting... [INFO] [talledLocalContainer] JBoss 4.0.2 started on port [8280] [INFO] Press Ctrl-C to stop the container...
To deploy the WAR using Tomcat tell Maven to execute the tomcat5x profile by typing mvn cargo:start -Ptomcat5x:
C:devm2bookcodej2eedaytraderweb>mvn cargo:start -Ptomcat5x [...] [INFO] [cargo:start] [INFO] [talledLocalContainer] Tomcat 5.0.30 starting... [INFO] [CopyingLocalDeployer] Deploying [C:devm2bookcodej2eedaytraderwebtargetdaytrader-web-1.0.war] to [C:[...]Tempcargo50866webapps]... [INFO] [talledLocalContainer] Tomcat 5.0.30 started on port [8280] [INFO] Press Ctrl-C to stop the container...
This is useful for development and to test that your code deploys and works. However, once this is verified you’ll want a solution to deploy your WAR into an integration platform. One solution is to have your container running on that integration platform and to perform a remote deployment of your WAR to it.
To deploy the DayTrader’s WAR to a running JBoss server on machine remoteserver and executing on port 80, you would need the following Cargo plugin configuration in web/pom.xml:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<container>
<containerId>jboss4x</containerId>
<type>remote</type>
</container>
<configuration>
<type>runtime</type>
<properties>
<cargo.hostname>${remoteServer}</cargo.hostname>
<cargo.servlet.port>${remotePort}</cargo.servlet.port>
<cargo.remote.username>${remoteUsername}</cargo.remote.username>
<cargo.remote.password>${remotePassword}</cargo.remote.password>
</properties>
</configuration>
</configuration>
</plugin>
When compared to the configuration for a local deployment above, the changes are:
- A remote container and configuration type to tell Cargo that the container is remote and not under Cargo’s management.
- Several configuration properties (especially a user name and password allowed to deploy on the remote JBoss container) to specify all the details required to perform the remote deployment. All the properties introduced need to be declared inside the POM for those shared with other users and in the
profiles.xmlfile (or thesettings.xmlfile) for those user-dependent. Note that there was no need to specify a deployment URL as it is computed automatically by Cargo.
/Deploying+to+a+running+container.