## Chapter 6. Getting started with Weld

6.1. Prerequisites
6.2. Deploying to JBoss AS
6.3. Deploying to GlassFish
6.4. Deploying to Apache Tomcat
6.4.1. Deploying with Ant
6.4.2. Deploying with Maven
6.5. Deploying to Jetty

Weld comes with two starter example applications, in addition to more specialized examples. The first, weld-numberguess, is a web (WAR) example containing only non-transactional managed beans. This example can be run on a wide range of servers, including JBoss AS, GlassFish, Apache Tomcat, Jetty, Google App Engine, and any compliant Java EE 6 container. The second example, weld-translator, is an enterprise (EAR) example that contains session beans. This example must be run on JBoss AS 5.2, Glassfish 3.0 or any compliant Java EE 6 container.

Both examples use JSF 2.0 as the web framework and, as such, can be found in the examples/jsf directory of the Weld distribution.

## 6.2. Deploying to JBoss AS

To deploy the examples to JBoss AS, you'll need JBoss AS 5.2.0.Beta1 or above. If a release of the JBoss AS 5.2 line isn't yet available, you can download a nightly snapshot. The reason JBoss AS 5.2.0.Beta1 or above is required is because it's the first release that has both CDI and Bean Validation support built-in, making it close enough to Java EE 6 to run the examples. The good news is that there are no additional modifications you have to make to the server. It's ready to go!

After you have downloaded JBoss AS, extract it. (We recommended renaming the folder to include the as qualifier so it's clear that it's the application server). You can move the extracted folder anywhere you like. Wherever it lays to rest, that's what we'll call the JBoss AS installation directory, or JBOSS_HOME.

$> unzip jboss-5.2.*.zip$> mv jboss-5.2.*/ jboss-as-5.2

In order for the build scripts to know where to deploy the example, you have to tell them where to find your JBoss AS installation (i.e., JBOSS_HOME). Create a new file namedlocal.build.properties in the examples directory of the Weld distribution and assign the path of your JBoss AS installation to the property key jboss.home, as follows:

jboss.home=/path/to/jboss-as-5.2

Switch to the examples/jsf/numberguess directory and execute the Ant deploy target:

$> cd examples/jsf/numberguess$> ant deploy

If you haven't already, start JBoss AS. You can either start JBoss AS from a Linux shell:

$> cd /path/to/jboss-as-5.2$> ./bin/run.sh

a Windows command window:

$> cd c:\path\to\jboss-as-5.2\bin$> run

or you can start the server using an IDE, like Eclipse.

## Note

If you are using Eclipse, you should seriously consider installing the JBoss Tools add-ons, which include a wide variety of tooling for JSR-299 and Java EE development, as well as an enhanced JBoss AS server view.

Wait a few seconds for the application to deploy (or the application server to start) and see if you can determine the most efficient approach to pinpoint the random number at the local URL http://localhost:8080/weld-numberguess.

## Tip

The Ant build script includes additional targets for JBoss AS to deploy and undeploy the archive in either exploded or packaged format and to tidy things up.

• ant restart - deploy the example in exploded format to JBoss AS

• ant explode - update an exploded example, without restarting the deployment

• ant deploy - deploy the example in compressed jar format to JBoss AS

• ant undeploy - remove the example from JBoss AS

• ant clean - clean the example

The second starter example, weld-translator, will translate your text into Latin. (Well, not really, but the stub is there for you to implement, at least. Good luck!) To try it out, switch to the translator example directory and execute the deploy target:

$> cd examples/jsf/translator$> ant deploy

## Note

The translator uses session beans, which are packaged in an EJB module within an EAR. Java EE 6 will allow session beans to be deployed in WAR modules, but that's a topic for a later chapter.

Again, wait a few seconds for the application to deploy (if you're really bored, read the log messages), and visit http://localhost:8080/weld-translator to begin pseudo-translating.

TODO Insert note about upgrading the Weld deployer that directs reader to section on the JBoss AS environment

## 6.3. Deploying to GlassFish

Deploying to GlassFish should be easy, right? After all, it's the Java EE 6 reference implementation. Since it's the Java EE 6 reference implementation, that means it also bundles the JSR-299 reference implementation, Weld! So yes, it's very easy.

To deploy the examples to GlassFish, you'll need the final GlassFish V3 release (the preview release won't do). If the final release isn't yet available, you can download a promoted build in the meantime. Select the b69 preview release or above that ends in either -unix.sh or -windows.exe depending on your platform. After the download is complete, execute the installer. On Linux/Unix, you'll need to first make the script executable.

$> chmod 755 glassfish-v3-b69-unix.sh$> ./glassfish-v3-b69-unix.sh

On Windows you can just click on the executable. Follow the instructions in the installer. It will create a single domain named domain1. You'll use that domain to deploy the example. We recommend that you choose 7070 as the main HTTP port to avoid conflicts with a running instance of JBoss AS (or Apache Tomcat).

If you've deployed either of the starter examples, weld-numberguess or weld-translator, to JBoss AS, then you already have the deployable artifact you need. If not, switch to either of the two directories and build it.

$> cd examples/jsf/numberguess (or examples/jsf/translator)$> ant package

The deployable archive for the weld-numberguess, named weld-numberguess.war, ends up in the example's target directory. The archive for the weld-translator example, named weld-translator.ear, ends up in the example's ear/target directory. All you need to do now is deploy them to GlassFish.

You deploy applications to GlassFish using the GlassFish Admin Console. To get the Admin Console running, you need to start a GlassFish domain, in our case domain1. Switch to the bin folder in the directory where you installed GlassFish and execute the following command:

$> asadmin start-domain domain1 After a few seconds you can visit the Admin Console in the browser at the URL http://localhost:4848. In the tree on the left-hand side of the page, click on "Applications", then click on the "Deploy..." button under the heading "Applications" and select the deployable artifact for either of the two examples. The deployer should recognize that you have selected a Java EE artifact and allow you to start it. You can see the examples running at either http://localhost:7070/weld-numberguess or http://localhost:7070/weld-translator, depending on which example you deployed. The reason the same artifact can be deployed to both JBoss AS and GlassFish, without any modifications, is because all of the features being used are part of the standard platform. And what a capable platform it has become! ## 6.4. Deploying to Apache Tomcat Servlet containers are not required to support Java EE services like CDI. However, you can use CDI in a servlet container like Tomcat by embedding a standalone CDI implementation such as Weld. Weld comes with a servlet listener which bootstraps the CDI environment, registers the BeanManager in JNDI and provides injection into servlets. Basically, it emulates some of the work done by the Java EE container. (But you don't get enterprise features such as session beans and container-managed transactions.)$> unzip apache-tomcat-6.0.18.zip

You have two choices for how you can deploy the application to Tomcat. You can deploy it by pushing the artifact to the hot deploy directory using Ant or you can deploy to the server across HTTP using a Maven plugin. The Ant approach doesn't require that you have Maven installed, so we'll start there. If you want to use Maven, you can just skip ahead.

## 6.5. Deploying to Jetty

Support for Jetty in the examples is a more recent addition. Since Jetty is traditionally used with Maven, there are no Ant targets. You must invoke the Maven build directly to deploy the examples to Jetty out of the box. Also, only the weld-numberguess example is configured for Jetty support at the time of writing.

If you've read through the entire Tomcat section, then you're all ready to go. The Maven build parallels the embedded Tomcat deployment. If not, don't worry. We'll still go over everything that you need to know again in this section.

The Maven POM (pom.xml) includes a profile named jetty that activates the Maven Jetty plugin, which you can use to start Jetty in embedded mode and deploy the application in place. You don't need anything else installed except to have the Maven command (mvn) on your path. The rest will be downloaded from the internet when the build is run.

To run the weld-numberguess example on Jetty, switch to the example directory and execute the inplace goal of the Maven WAR plugin followed by the run goal of the Maven Jetty plugin with the jetty profile enabled, as follows:

$> cd examples/jsf/numberguess$> mvn war:inplace jetty:run -Pjetty

The log output of Jetty will be shown in the console. Once Jetty reports that the application has deployed, you can access it at the following local URL: http://localhost:9090/weld-numberguess. The port is defined in the Maven Jetty plugin configuration within the jetty profile.

Any changes to assets in src/main/webapp take effect immediately. If a change to a webapp configuration file is made, the application may automatically redeploy. The redeploy behavior can be fined-tuned in the plugin configuration. If you make a change to a classpath resource, you need to execute a build and the inplace goal of the Maven WAR plugin, again with the jetty profile enabled.

$> mvn compile war:inplace -Pjetty The war:inplace goal copies the compiled classes and JARs inside src/main/webapp, under WEB-INF/classes and WEB-INF/lib, respectively, mixing source and compiled files. However, the build does work around these temporary files by excluding them from the packaged WAR and cleaning them during the Maven clean phase. You have two options if you want to run the example on Jetty from the IDE. You can either install the m2eclispe[link] plugin and run the goals as described above. Your other option is to start the Jetty container from a Java application. First, initialize the Eclipse project:$> mvn clean eclipse:clean eclipse:eclipse -Pjetty-ide

Next, assemble all the necessary resources under src/main/webapp:

\$> mvn war:inplace -Pjetty-ide

Now, you are ready to run the server in Eclipse. Import the project into your Eclipse workspace using "Import Existing Project into Workspace. Then, find the start class in src/jetty/java and run its main method as a Java Application. Jetty will launch. You can view the application at the following local URL: http://localhost:8080. Pay particular attention to the port in the URL and the lack of a trailing context path.

Now that you have gotten the starter applications deployed on the server of your choice, you probably want to know a little bit about how they actually work.