Getting started with Weld
Weld comes with several numberguess examples in various flavors based on what environment you use. In its classic variant, it is a web (war) example containing only non-transactional managed beans. This example can be run on a wide range of servers, including WildFly, GlassFish, Apache Tomcat, Jetty, and any compliant Jakarta EE container.
The example uses JSF as the web framework and, as such, can be
found in the examples/jsf
directory of the Weld distribution.
Prerequisites
To run the examples with the provided build scripts, you’ll need the following:
-
the latest release of Weld, which contains the examples
-
Maven 3, to build and deploy the examples
-
optionally, a supported runtime environment (minimum versions shown)
-
WildFly,
-
GlassFish,
-
Apache Tomcat, or
-
Jetty
-
Note that the version of these runtimes need to target the same Jakarta EE version that Weld does.
In the next few sections, you’ll be using the Maven command (mvn
) to
invoke the Maven project file in each example to compile, assemble and
deploy the example to WildFly and, for the war example, Apache Tomcat.
You can also deploy the generated artifact (war) to any other
container that supports Jakarta EE, such as GlassFish.
The sections below cover the steps for deploying with Maven in detail.
First try
If you simply want to run the numberguess example without the requirement of a specific runtime you can start with the following commands:
$> cd examples/jsf/numberguess
$> mvn wildfly:run
The Maven WildFly plugin will run WildFly and deploy the example and the server will be automatically downloaded in the target
directory.
The numberguess application is available at http://localhost:8080/weld-numberguess.
Deploying to WildFly
To deploy the examples to your own WildFly instance, you’ll need to download WildFly first. 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 WildFly, extract it. You can move the extracted folder anywhere you
like. Wherever it lays to rest, that’s what we’ll call the WildFly
installation directory, or JBOSS_HOME
.
$> unzip wildfly-31.x.y.Final.zip
$> mv wildfly-31.*/ wildfly-31
In order for the build scripts to know where to deploy the example, you
have to tell them where to find your WildFly installation. Set the
JBOSS_HOME
environment variable to point to the WildFly installation,
e.g.:
$> export JBOSS_HOME=/path/to/wildfly-31
Next up, start your WildFly server.
Assuming default configuration and Linux, you can do that with the following command (for Windows, use the .bat
file instead):
$> cd path/to/wildfly
$> ./bin/standalone.sh
You’re now ready to run your first example!
Switch to the examples/jsf/numberguess
directory in Weld repository and execute the Maven deploy
target:
$> cd examples/jsf/numberguess
$> mvn wildfly:deploy
Wait a bit for the application to deploy and see if you can determine the most efficient approach to pinpoint the random number at the local URL http://localhost:8080/weld-numberguess.
Note
|
The Maven WildFly plugin includes additional goals for WildFly to deploy and undeploy the archive.
For more information on the WildFly Maven plugin see the plugin documentation. |
TODO this ain’t true!!!
You can also run some simple integration tests to verify that the example works as expected. Keep the server with deployed application running and execute the following:
$> mvn verify -Pintegration-testing
You should see the following output:
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
Deploying to Apache Tomcat
Servlet containers are not required to support Jakarta 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 servlet integration extension which bootstraps the CDI environment and provides injection into servlets components. Basically, it emulates some of the work done by the Jakarta EE container, but you don’t get the enterprise features such as session beans and container-managed transactions.
Note
|
Note that due to limitations of servlet containers (e.g. read-only JNDI) your application might require some additional configuration as well (see Tomcat and Jetty for more info). |
Let’s give the Weld servlet extension a spin on Apache Tomcat. First, you’ll need to download Tomcat 10.1 or later from tomcat.apache.org and extract it.
$> unzip apache-tomcat-10.1.x.zip
The Maven plugin communicates with Tomcat over HTTP, so it doesn’t care
where you have installed Tomcat. However, the plugin configuration
assumes you are running Tomcat in its default configuration, with a
hostname of localhost and port 8080
. The readme.txt
file in the
example directory has information about how to modify the Maven settings
to accommodate a different setup.
You can either start Tomcat from a Linux shell:
$> cd /path/to/apache-tomcat-10.1
$> ./bin/startup.sh
a Windows command window:
$> cd c:\path\to\apache-tomcat-10\bin
$> start
or you can start the server using an IDE, like Eclipse.
Change to the examples/jsf/numberguess
directory again and run the
following Maven command:
$> cd examples/jsf/numberguess
$> mvn clean package -Ptomcat
Now you’re ready to deploy the numberguess example to Tomcat!
$> cp examples/jsf/numberguess/target/weld-numberguess.war apache-tomcat/webapps/