In a previous post. I showed how to do the absolute bare minimum to get a CDI/Weld project running in Jetty. Jetty is my favorite container because when launched from maven, it automatically redeploys classes as you modify them without configuring my IDE.
However, in every Java job I’ve ever held, the final product is deployed to another container, such as Tomcat. Fortunately, the Tomcat web application is almost identical to Jetty. Just take the identical application, add a context.xml and deploy.
Create context.xml
#from your project home directory (where pom.xml lives) touch src/main/webapp/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?> <Context> <Manager pathname="" /> <!-- disables storage of sessions across restarts --> <Resource name="BeanManager" auth="Container" type="javax.enterprise.inject.spi.BeanManager" factory="org.jboss.weld.resources.ManagerObjectFactory" /> <!-- Uncomment to enable injection into Servlet --> <!-- <Listener className="org.jboss.weld.environment.tomcat.WeldLifecycleListener"/> --> </Context>
Now, build the project using the standard Maven way:
mvn clean package
In your target directory, you’ll find a war you can deploy using tomcat manager.
Automate Tomcat Deployments using Cargo
If you’re like me, you hate doing things more than once when a script can do the job for you. Simply add:
<!-- Cargo - deploys to Tomcat using command 'mvn clean package cargo:redeploy'. --> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <container> <containerId>tomcat6x</containerId> <type>remote</type> </container> <configuration> <type>runtime</type> <properties> <!-- Declare a tomcat.user and tomcat.password in your settings file --> <cargo.remote.username>${tomcat.username}</cargo.remote.username> <cargo.remote.password>${tomcat.password}</cargo.remote.password> <!--defaults to localhost:8080--> <!--<cargo.hostname>localhost</cargo.hostname>--> <!--<cargo.servlet.port>8080</cargo.servlet.port>--> </properties> </configuration> </configuration> </plugin>
If you have a settings.xml file in your .m2 directory, then add the tomcat.username and tomcat.password variables. If you don’t have one, create a file named settings.xml in ~/.m2/
touch ~/.m2/settings.xml
If you have not yet declared a settings.xml, use the one below to get started and add your manager username and password.
settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <profiles> <profile> <id>default</id> <properties> <tomcat.username>(a valid tomcat user in 'manager' role)</tomcat.username> <tomcat.password>(the manager password)</tomcat.password> </properties> </profile> </profiles> <activeProfiles> <activeProfile>default</activeProfile> </activeProfiles> </settings>
Ready to deploy
Now you can deploy to localhost:8080 using the command below.
mvn clean package cargo:redeploy
The host and port can be configured by setting cargo.hostname and cargo.servlet.port.
All Done
Now you can deploy your application to tomcat automatically.
hi Steven,
Thanks for this great tutorial.
I’m new to all this stuff maven, jsf, tomcat etc.
I lost when you said to do “Automate Tomcat Deployments using Cargo”. I searched hours to put that code block. Finally found that. we need to put that on pom.xml file.
I found that here.
http://cargo.codehaus.org/Maven2+Plugin+Installation
Thanks.