Este conteúdo não está disponível no idioma selecionado.
Chapter 11. The bootable JAR
You can build and package a microservices application as a bootable JAR with the JBoss EAP JAR Maven plug-in. You can then run the application on a JBoss EAP bare-metal platform or a JBoss EAP OpenShift platform.
11.1. About the bootable JAR Copiar o linkLink copiado para a área de transferência!
You can build and package a microservices application as a bootable JAR with the JBoss EAP JAR Maven plug-in.
A bootable JAR contains a server, a packaged application, and the runtime required to launch the server.
The JBoss EAP JAR Maven plug-in uses Galleon trimming capability to reduce the size and memory footprint of the server. Thus, you can configure the server according to your requirements, including only the Galleon layers that provide the capabilities that you need.
The JBoss EAP JAR Maven plug-in supports the execution of JBoss EAP CLI script files to customize your server configuration. A CLI script includes a list of CLI commands for configuring the server.
A bootable JAR is like a standard JBoss EAP server in the following ways:
- It supports JBoss EAP common management CLI commands.
- It can be managed using the JBoss EAP management console.
The following limitations exist when packaging a server in a bootable JAR:
- CLI management operations that require a server restart are not supported.
- The server cannot be restarted in admin-only mode, which is a mode that starts services related to server administration.
- If you shut down the server, updates that you applied to the server are lost.
Additionally, you can provision a hollow bootable JAR. This JAR contains only the server, so you can reuse the server to run a different application.
11.2. JBoss EAP JAR Maven plug-in Copiar o linkLink copiado para a área de transferência!
You can use the JBoss EAP JAR Maven plug-in to build an application as a bootable JAR.
Check that you have the latest Maven plug-in such as 9.minor.micro.Final-redhat-XXXXX, where 9 is the major version, minor is the minor version, micro micro version and X is the Red Hat build number. For example: 9.0.1.Final-redhat-00009.
In a Maven project, the src directory contains all the source files required to build your application. After the JBoss EAP JAR Maven plug-in builds the bootable JAR, the generated JAR is located in target/<application>-bootable.jar.
The JBoss EAP JAR Maven plug-in also provides the following functionality:
- Allows you to provision JBoss EAP XP server using JBoss EAP channels.
- Applies CLI script commands to the server.
-
Uses the
org.jboss.eap.xp:wildfly-galleon-packGalleon feature pack and some of its layers for customizing the server configuration file. - Supports the addition of extra files into the packaged bootable JAR, such as a keystore file.
- Includes the capability to create a hollow bootable JAR; that is, a bootable JAR that does not contain an application.
After you use the JBoss EAP JAR Maven plug-in to create the bootable JAR, you can start the application by issuing the following command. Replace target/myapp-bootable.jar with the path to your bootable JAR. For example:
java -jar target/myapp-bootable.jar
$ java -jar target/myapp-bootable.jar
To get a list of supported bootable JAR startup commands, append --help to the end of the startup command. For example, java -jar target/myapp-bootable.jar --help.
11.3. Bootable JAR arguments Copiar o linkLink copiado para a área de transferência!
View the arguments in the following table to learn about supported arguments for use with the bootable JAR.
| Argument | Description |
|---|---|
|
| Displays the help message for the specified command and exit. |
|
| Specifies the path to a JBoss CLI script that executes when starting the bootable JAR. If the path specified is relative, the path is resolved against the working directory of the Java VM instance used to launch the bootable JAR. |
|
| Argument specific to the hollow bootable JAR. Specifies the path to the WAR, JAR, EAR file or exploded directory that contains the application you want to deploy on a server. |
|
| Print the content of the generated Galleon configuration file. |
|
|
By default, the JVM settings are used to create a TEMP directory after the bootable JAR is started. You can use the |
|
| Runs the server with a security manager installed. |
|
|
Set system property |
|
|
Set system property |
|
| Specifies system properties that are set by the server at server runtime. The bootable JAR JVM does not set these system properties. |
|
| Loads system properties from a specified URL. |
|
| Set a security property. |
|
|
Set system property |
|
| Display the application server version and exit. |
11.4. Specifying Galleon layers for your bootable JAR server Copiar o linkLink copiado para a área de transferência!
You can specify Galleon layers to build a custom configuration for your server. Additionally, you can specify Galleon layers that you want excluded from the server.
Starting with JBoss EAP XP 5.0, it is necessary to configure the JBoss EAP JAR Maven plug-in with the JBoss EAP 8.0 and JBoss EAP XP 5.0 channels to retrieve the server artifacts. For more information about JBoss EAP Channels, see managing JBoss EAP installation channels.
To specify the JBoss EAP and JBoss EAP XP channels for provisioning the latest JBoss EAP XP 5.0 server, follow this example:
Use the <feature-pack-location> element to specify feature pack location. In the Maven plug-in configuration file, the following example specifies org.jboss.eap.xp:wildfly-galleon-pack within the <feature-pack-location> element.
If you need to reference more than one feature pack, list them in the <feature-packs> element. The following example shows the addition of the JBoss EAP datasources feature pack to the <feature-packs> element:
You can combine Galleon layers from multiple feature packs to configure the bootable JAR server to include only the supported Galleon layers that provide the capabilities that you need.
On a bare-metal platform, if you do not specify Galleon layers in your configuration file then the provisioned server contains a configuration identical to that of a default standalone-microprofile.xml configuration.
On an OpenShift platform, after you have added the <cloud/> configuration element in the plug-in configuration and you choose not to specify Galleon layers in your configuration file, the provisioned server contains a configuration that is adjusted for the cloud environment and is similar to a default standalone-microprofile-ha.xml.
Prerequisites
- Maven is installed.
-
You have checked the latest Maven plug-in version, such as
9.minor.micro.Final-redhat-XXXXX, where 9 is the major version, minor is the minor version, micro micro version and X is the Red Hat build number. For example:9.0.1.Final-redhat-00009.
The examples shown in the procedure specify the following properties:
-
${bootable.jar.maven.plugin.version}for the Maven plug-in version.
You must set these properties in your project. For example:
<properties>
<bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version>
</properties>
<properties>
<bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version>
</properties>
Procedure
- Identify the supported JBoss EAP Galleon layers that provide the capabilities that you need to run your application.
Reference a JBoss EAP feature pack location in the
<plugin>element of the Maven projectpom.xmlfile. The following example displays the inclusion of a single feature-pack, which includes thejaxrs-serverbase layer and thejpa-distributedlayer . Thejaxrs-serverbase layer provides additional support for the server.Copy to Clipboard Copied! Toggle word wrap Toggle overflow This example also shows the exclusion of the
jpalayer from the project.NoteIf you include the
jpa-distributedlayer in your project, you must exclude thejpalayer from thejaxrs-serverlayer. Thejpalayer configures a local infinispan hibernate cache, while thejpa-distributedlayer configures a remote infinispan hibernate cache.
11.5. Using a bootable JAR on a JBoss EAP bare-metal platform Copiar o linkLink copiado para a área de transferência!
You can package an application as a bootable JAR on a JBoss EAP bare-metal platform.
A bootable JAR contains a server, a packaged application, and the runtime required to launch the server.
This procedure demonstrates packaging the MicroProfile Config microservices application as a bootable JAR with the JBoss EAP JAR Maven plug-in. See MicroProfile Config Quickstart.
You can use CLI scripts to configure the server during the packaging of the bootable JAR.
On building a web application that must be packaged inside a bootable JAR, you must specify war in the <packaging> element of your pom.xml file. For example:
<packaging>war</packaging>
<packaging>war</packaging>
This value is required to package the build application as a WAR file and not as the default JAR file.
In a Maven project that is used solely to build a hollow bootable JAR, set the packaging value to pom. For example:
<packaging>pom</packaging>
<packaging>pom</packaging>
You are not limited to using pom packaging when you build a hollow bootable JAR for a Maven project. You can create one by specifying true in the <hollow-jar> element for any type of packaging, such as war. See Creating a hollow bootable JAR on a JBoss EAP bare-metal platform.
Prerequisites
-
You have checked the latest Maven plug-in version, such as
9.minor.micro.Final-redhat-XXXXX, where 9 is the major version, minor is the minor version, micro micro version and X is the Red Hat build number. For example:9.0.1.Final-redhat-00009. - You have created a Maven project, and added dependencies for creating an MicroProfile application. See MicroProfile Config development.
The examples shown in the procedure specify the following properties:
-
${bootable.jar.maven.plugin.version}for the Maven plug-in version.
You must set these properties in your project. For example:
<properties>
<bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version>
</properties>
<properties>
<bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version>
</properties>
Procedure
Add the following content to the
<build>element of thepom.xmlfile. For example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf you do not specify Galleon layers in your
pom.xmlfile then the bootable JAR server contains a configuration that is identical to astandalone-microprofile.xmlconfiguration.Package the application as a bootable JAR:
mvn package
$ mvn packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the application:
NAME="foo" java -jar target/microprofile-config-bootable.jar
$ NAME="foo" java -jar target/microprofile-config-bootable.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe example uses
NAMEas the environment variable, but you can choose to usejim, which is the default value.NoteTo view a list of supported bootable JAR arguments, append
--helpto the end of thejava -jar target/microprofile-config-bootable.jarcommand.Specify the following URL in your web browser to access the MicroProfile Config application:
http://localhost:8080/config/json
http://localhost:8080/config/jsonCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verification: Test the application behaves properly by issuing the following command in your terminal:
curl http://localhost:8080/config/json
curl http://localhost:8080/config/jsonCopy to Clipboard Copied! Toggle word wrap Toggle overflow The following is the expected output:
{"result":"Hello foo"}{"result":"Hello foo"}Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.6. Creating a hollow bootable JAR on a JBoss EAP bare-metal platform Copiar o linkLink copiado para a área de transferência!
You can package an application as a hollow bootable JAR on a JBoss EAP bare-metal platform.
A hollow bootable JAR contains only the JBoss EAP server. The hollow bootable JAR is packaged by the JBoss EAP JAR Maven plug-in. The application is provided at server runtime. The hollow bootable JAR is useful if you need to re-use the server configuration for a different application.
Prerequisites
- You have created a Maven project for MicroProfile Config development. For example: See MicroProfile Config Quickstarts.
-
You have completed the
pom.xmlfile configuration steps outlined in Using a bootable JAR on a JBoss EAP bare-metal platform. -
You have checked the latest Maven plug-in version, such as
9.minor.micro.Final-redhat-XXXXX, where 9 is the major version, minor is the minor version, micro micro version and X is the Red Hat build number. For example:9.0.1.Final-redhat-00009.
Procedure
-
To build a hollow bootable JAR, you must set the
<hollow-jar>plug-in configuration element to true in the projectpom.xmlfile. For example:
By specifying true in the <hollow-jar> element, the JBoss EAP JAR Maven plug-in does not include an application in the JAR.
Build the hollow bootable JAR:
mvn clean package
$ mvn clean packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow Run the hollow bootable JAR:
java -jar target/microprofile-config-bootable.jar --deployment=target/microprofile-config.war
$ java -jar target/microprofile-config-bootable.jar --deployment=target/microprofile-config.warCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantTo specify the path to the WAR file that you want to deploy on the server, use the following argument, where
<PATH_NAME>is the path to your deployment.--deployment=<PATH_NAME>
--deployment=<PATH_NAME>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Access the application:
curl http://localhost:8080/microprofile-config/config/json
$ curl http://localhost:8080/microprofile-config/config/jsonCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteTo register your web application in the root directory, name the application
ROOT.war.
11.7. CLI scripts executed at build time Copiar o linkLink copiado para a área de transferência!
You can create CLI scripts to configure the server during the packaging of the bootable JAR.
A CLI script is a text file that contains a sequence of CLI commands that you can use to apply additional server configurations. For example, you can create a script to add a new logger to the logging subsystem.
You can also specify more complex operations in a CLI script. For example, you can group security management operations into a single command to enable HTTP authentication for the management HTTP endpoint.
You must define CLI scripts in the <cli-session> element of the plug-in configuration before you package an application as a bootable JAR. This ensures the server configuration settings persist after packaging the bootable JAR.
Although you can combine predefined Galleon layers to configure a server that deploys your application, limitations do exist. For example, you cannot enable the HTTPS undertow listener using Galleon layers when packaging the bootable JAR. Instead, you must use a CLI script.
You must define the CLI scripts in the <cli-session> element of the pom.xml file. The following table shows types of CLI session attributes:
| Argument | Description |
|---|---|
|
| List of paths to script files. |
|
|
Optional attribute that specifies a path to a properties file. This file lists Java properties that scripts can reference by using the |
|
|
Optional attribute that contains a boolean value. Indicates if system properties or expressions are resolved before sending the operation requests to the server. Value is |
-
CLI scripts are started in the order that they are defined in the
<cli-session>element of thepom.xmlfile. - The JBoss EAP JAR Maven plug-in starts the embedded server for each CLI session. Thus, your CLI script does not have to start or stop the embedded server.
11.8. Executing CLI script at runtime Copiar o linkLink copiado para a área de transferência!
You can apply changes to the server configuration during runtime; this gives you the flexibility to adjust the server with respect to the execution context. However, the preferred way to apply changes to the server is during build time.
Procedure
Launch the bootable JAR, and the
--cli-scriptargument.For Example:
java -jar myapp-bootable.jar --cli-scipt=my-scli-scipt.cli
java -jar myapp-bootable.jar --cli-scipt=my-scli-scipt.cliCopy to Clipboard Copied! Toggle word wrap Toggle overflow
-
The CLI script must be a text file (UTF-8), the file extension if present is meaningless although
.cliextension is advised. - Operations that require your server to restart will terminate your bootable JAR instance.
-
CLI commands such as
connect,reload,shutdown, and any command related to embedded server are not operational. -
CLI commands such as
jdbc-driver-infothat cannot be executed in admin-mode are not supported.
If you restart the server without executing the CLI script, your new server instance will not contain the changes from your previous server instance.
11.9. Using a bootable JAR on a JBoss EAP OpenShift platform Copiar o linkLink copiado para a área de transferência!
11.9.1. Using oc command to do binary build Copiar o linkLink copiado para a área de transferência!
After you packaged an application as a bootable JAR, you can run the application on a JBoss EAP OpenShift platform.
On OpenShift, you cannot use the EAP Operator automated transaction recovery feature with your bootable JAR.
Prerequisites
- You have created a Maven project for MicroProfile Config development. For example: See MicroProfile Config Quickstarts.
-
You have checked the latest Maven plug-in version, such as
9.minor.micro.Final-redhat-XXXXX, where 9 is the major version, minor is the minor version, micro micro version and X is the Red Hat build number. For example:9.0.1.Final-redhat-00009.
The examples shown in the procedure specify the following properties:
-
${bootable.jar.maven.plugin.version}for the Maven plug-in version.
You must set these properties in your project. For example:
<properties>
<bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version>
</properties>
<properties>
<bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version>
</properties>
Procedure
Add the following content to the
<build>element of thepom.xmlfile. For example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteYou must include the
<cloud/>element in the<configuration>element of the plug-in configuration, so the JBoss EAP Maven JAR plug-in can identify that you choose the OpenShift platform.Package the application:
mvn package
$ mvn packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Log in to your OpenShift instance using the
oc logincommand. Create a new project in OpenShift. For example:
oc new-project bootable-jar-project
$ oc new-project bootable-jar-projectCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following
occommands to create an application image:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Creates an openshift sub-directory in the target directory. The packaged application is copied into the created sub-directory.
- 2
- Imports the latest OpenJDK 17 imagestream tag and image information into the OpenShift project.
- 3
- Creates a build configuration based on the microprofile-config-app directory and the OpenJDK 17 imagestream.
- 4
- Uses the
target/openshiftsub-directory as the binary input to build the application.
NoteOpenShift applies a set of CLI script commands to the bootable JAR configuration file to adjust it to the cloud environment. You can access this script by opening the
bootable-jar-build-artifacts/generated-cli-script.txtfile in the Maven project/target directory.Verification:
View a list of OpenShift pods available and check the pods build statuses by issuing the following command:
oc get pods
$ oc get podsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify the built application image:
oc get is microprofile-config-app
$ oc get is microprofile-config-appCopy to Clipboard Copied! Toggle word wrap Toggle overflow The output shows the built application image details, such as name and image repository, tag, and so on. For the example in this procedure, the imagestream name and tag output displays
microprofile-config-app:latest.Deploy the application:
oc new-app microprofile-config-app oc expose svc/microprofile-config-app
$ oc new-app microprofile-config-app $ oc expose svc/microprofile-config-appCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantTo provide system properties to the bootable JAR, you must use the
JAVA_OPTS_APPENDenvironment variable. The following example demonstrates usage of theJAVA_OPTS_APPENDenvironment variable:oc new-app <_IMAGESTREAM_> -e JAVA_OPTS_APPEND="-Xlog:gc*:file=/tmp/gc.log:time -Dwildfly.statistics-enabled=true"
$ oc new-app <_IMAGESTREAM_> -e JAVA_OPTS_APPEND="-Xlog:gc*:file=/tmp/gc.log:time -Dwildfly.statistics-enabled=true"Copy to Clipboard Copied! Toggle word wrap Toggle overflow A new application is created and started. The application configuration is exposed as a new service.
Verification: Test the application behaves properly by issuing the following command in your terminal:
curl http://$(oc get route microprofile-config-app --template='{{ .spec.host }}')/config/json$ curl http://$(oc get route microprofile-config-app --template='{{ .spec.host }}')/config/jsonCopy to Clipboard Copied! Toggle word wrap Toggle overflow Expected output:
{"result":"Hello jim"}{"result":"Hello jim"}Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.10. Configure the bootable JAR for OpenShift Copiar o linkLink copiado para a área de transferência!
Before using your bootable JAR, you can configure JVM settings to ensure that your standalone server operates correctly on JBoss EAP for OpenShift.
Use the JAVA_OPTS_APPEND environment variable to configure JVM settings. Use the JAVA_ARGS command to provide arguments to the bootable JAR.
You can use environment variables to set values for properties. For example, you can use the JAVA_OPTS_APPEND environment variable to set the -Dwildfly.statistics-enabled property to true:
JAVA_OPTS_APPEND="-Xlog:gc*:file=/tmp/gc.log:time -Dwildfly.statistics-enabled=true"
JAVA_OPTS_APPEND="-Xlog:gc*:file=/tmp/gc.log:time -Dwildfly.statistics-enabled=true"
Statistics are now enabled for your server.
Use the JAVA_ARGS environment variable, if you need to provide arguments to the bootable JAR.
JBoss EAP for OpenShift provides a JDK 17 image. To run the application associated with your bootable JAR, you must first import the latest OpenJDK 17 imagestream tag and image information into your OpenShift project. You can then use environment variables to configure the JVM in the imported image.
You can apply the same configuration options for configuring the JVM used for JBoss EAP for OpenShift S2I image, but with the following differences:
-
Optional: The
-Xlogcapability is not available, but you can set garbage collection logging by enabling-Xlog:gc. For example:JAVA_OPTS_APPEND="-Xlog:gc*:file=/tmp/gc.log:time". -
To increase initial metaspace size, you can set the
GC_METASPACE_SIZEenvironment variable. For best metadata capacity performance, set the value to96. -
For better random file generation, use the
JAVA_OPTS_APPENDenvironment variable to setjava.security.egdproperty as-Djava.security.egd=file:/dev/urandom.
These configurations improve the memory settings and garbage collection capability of JVM when running on your imported OpenJDK 17 image.
11.11. Using a ConfigMap in your application on OpenShift Copiar o linkLink copiado para a área de transferência!
For OpenShift, you can use a deployment controller (dc) to mount the configmap into the pods used to run the application.
A ConfigMap is an OpenShift resource that is used to store non-confidential data in key-value pairs.
After you specify the microprofile-platform Galleon layer to add microprofile-config-smallrye subsystem and any extensions to the server configuration file, you can use a CLI script to add a new ConfigSource to the server configuration. You can save CLI scripts in an accessible directory, such as the /scripts directory, in the root directory of your Maven project.
MicroProfile Config functionality is implemented in JBoss EAP using the SmallRye Config component and is provided by the microprofile-config-smallrye subsystem. This subsystem is included in the microprofile-platform Galleon layer.
Prerequisites
- You have installed Maven.
- You have configured the JBoss EAP Maven repository.
- You have packaged an application as a bootable JAR and you can run the application on a JBoss EAP OpenShift platform. For information about building an application as a bootable JAR on an OpenShift platform, see Using a bootable JAR on a JBoss EAP OpenShift platform.
Procedure
Create a directory named
scriptsat the root directory of your project. For example:mkdir scripts
$ mkdir scriptsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
cli.propertiesfile and save the file in the/scriptsdirectory. Define theconfig.pathand theconfig.ordinalsystem properties in this file. For example:config.path=/etc/config config.ordinal=200
config.path=/etc/config config.ordinal=200Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a CLI script, such as
mp-config.cli, and save it in an accessible directory in the bootable JAR, such as the/scriptsdirectory. The following example shows the contents of themp-config.cliscript:config map
# config map /subsystem=microprofile-config-smallrye/config-source=os-map:add(dir={path=${config.path}}, ordinal=${config.ordinal})Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
mp-config.cliCLI script creates a newConfigSource, to which ordinal and path values are retrieved from a properties file.-
Save the script in the
/scriptsdirectory, which is located at the root directory of the project. Add the following configuration extract to the existing plug-in
<configuration>element:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Package the application:
mvn package
$ mvn packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Log in to your OpenShift instance using the
oc logincommand. Optional: If you have not previously created a
target/openshiftsubdirectory, you must create the suddirectory by issuing the following command:mkdir target/openshift
$ mkdir target/openshiftCopy to Clipboard Copied! Toggle word wrap Toggle overflow Copy the packaged application into the created subdirectory.
cp target/microprofile-config-bootable.jar target/openshift
$ cp target/microprofile-config-bootable.jar target/openshiftCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
target/openshiftsubdirectory as the binary input to build the application:oc start-build microprofile-config-app --from-dir target/openshift
$ oc start-build microprofile-config-app --from-dir target/openshiftCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteOpenShift applies a set of CLI script commands to the bootable JAR configuration file to enable it for the cloud environment. You can access this script by opening the
bootable-jar-build-artifacts/generated-cli-script.txtfile in the Maven project/targetdirectory.Create a
ConfigMap. For example:oc create configmap microprofile-config-map --from-literal=name="Name comes from Openshift ConfigMap"
$ oc create configmap microprofile-config-map --from-literal=name="Name comes from Openshift ConfigMap"Copy to Clipboard Copied! Toggle word wrap Toggle overflow Mount the
ConfigMapinto the application with the dc. For example:oc set volume deployments/microprofile-config-app --add --name=config-volume \ --mount-path=/etc/config \ --type=configmap \ --configmap-name=microprofile-config-map
$ oc set volume deployments/microprofile-config-app --add --name=config-volume \ --mount-path=/etc/config \ --type=configmap \ --configmap-name=microprofile-config-mapCopy to Clipboard Copied! Toggle word wrap Toggle overflow After executing the
oc set volumecommand, the application is re-deployed with the new configuration settings.Test the output:
curl http://$(oc get route microprofile-config-app --template='{{ .spec.host }}')/config/json$ curl http://$(oc get route microprofile-config-app --template='{{ .spec.host }}')/config/jsonCopy to Clipboard Copied! Toggle word wrap Toggle overflow The following is the expected output:
{"result":"Hello Name comes from Openshift ConfigMap"}{"result":"Hello Name comes from Openshift ConfigMap"}Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.12. Creating a bootable JAR Maven project Copiar o linkLink copiado para a área de transferência!
Follow the steps in the procedure to create an example Maven project. You must create a Maven project before you can perform the following procedures:
- Enabling JSON logging for your bootable JAR
- Enabling web session data storage for multiple bootable JAR instances
- Enabling HTTP authentication for bootable JAR with a CLI script
- Securing your JBoss EAP bootable JAR application with Red Hat build of Keycloak
In the project pom.xml file, you can configure Maven to retrieve the project artifacts required to build your bootable JAR.
Procedure
Set up the Maven project:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where GROUP_ID is the
groupIdof your project and ARTIFACT_ID is theartifactIdof your project.In the
pom.xmlfile, configure Maven to retrieve the JBoss EAP BOM file from a remote repository.Copy to Clipboard Copied! Toggle word wrap Toggle overflow To configure Maven to automatically manage versions for the Jakarta EE artifacts in the
jboss-eap-eeBOM, add the BOM to the<dependencyManagement>section of the projectpom.xmlfile. For example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the servlet API artifact, which is managed by the BOM, to the
<dependency>section of the projectpom.xmlfile, as shown in the following example:<dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <scope>provided</scope> </dependency><dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <scope>provided</scope> </dependency>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.13. Enabling JSON logging for your bootable JAR Copiar o linkLink copiado para a área de transferência!
You can enable JSON logging for your bootable JAR by configuring the server logging configuration with a CLI script. When you enable JSON logging, you can use the JSON formatter to view log messages in JSON format.
The example in this procedure shows you how to enable JSON logging for your bootable JAR on a bare-metal platform and an OpenShift platform.
Prerequisites
-
You have checked the latest Maven plug-in version, such as
9.minor.micro.Final-redhat-XXXXX, where 9 is the major version, minor is the minor version, micro micro version and X is the Red Hat build number. For example:9.0.1.Final-redhat-00009. You have created a Maven project, and added dependencies for creating an application. See Creating a bootable JAR Maven project.
ImportantIn the Maven archetype of your Maven project, you must specify the groupID and artifactID that are specific to your project. For example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe examples shown in the procedure specify the following properties:
-
${bootable.jar.maven.plugin.version}for the Maven plug-in version.
You must set these properties in your project. For example:
<properties> <bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version> </properties><properties> <bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version> </properties>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Procedure
Add the JBoss Logging and Jakarta RESTful Web Services dependencies, which are managed by the BOM, to the
<dependencies>section of the projectpom.xmlfile. For example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following content to the
<build>element of thepom.xmlfile. For example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the directory to store Java files:
mkdir -p APPLICATION_ROOT/src/main/java/com/example/logging/
$ mkdir -p APPLICATION_ROOT/src/main/java/com/example/logging/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where
APPLICATION_ROOTis the directory containing thepom.xmlconfiguration file for the application.Create a Java file
RestApplication.javawith the following content and save the file in theAPPLICATION_ROOT/src/main/java/com/example/logging/directory:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a Java file
HelloWorldEndpoint.javawith the following content and save the file in theAPPLICATION_ROOT/src/main/java/com/example/logging/directory:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a CLI script, such as
logging.cli, and save it in an accessible directory in the bootable JAR, such as theAPPLICATION_ROOT/scriptsdirectory, whereAPPLICATION_ROOTis the root directory of your Maven project. The script must contain the following commands:/subsystem=logging/logger=com.example.logging:add(level=ALL) /subsystem=logging/json-formatter=json-formatter:add(exception-output-type=formatted, pretty-print=false, meta-data={version="1"}, key-overrides={timestamp="@timestamp"}) /subsystem=logging/console-handler=CONSOLE:write-attribute(name=level,value=ALL) /subsystem=logging/console-handler=CONSOLE:write-attribute(name=named-formatter, value=json-formatter)/subsystem=logging/logger=com.example.logging:add(level=ALL) /subsystem=logging/json-formatter=json-formatter:add(exception-output-type=formatted, pretty-print=false, meta-data={version="1"}, key-overrides={timestamp="@timestamp"}) /subsystem=logging/console-handler=CONSOLE:write-attribute(name=level,value=ALL) /subsystem=logging/console-handler=CONSOLE:write-attribute(name=named-formatter, value=json-formatter)Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following configuration extract to the plug-in
<configuration>element:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This example shows the
logging.cliCLI script, which modifies the server logging configuration file to enable JSON logging for your application.Package the application as a bootable JAR.
mvn package
$ mvn packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: To run the application on a JBoss EAP bare-metal platform, follow the steps outlined in Using a bootable JAR on a JBoss EAP bare-metal platform, but with the following difference:
Start the application:
mvn wildfly-jar:run
mvn wildfly-jar:runCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verification: You can access the application by specifying the following URL in your browser: http://127.0.0.1:8080/hello.
Expected output: You can view the JSON-formatted logs, including the
com.example.logging.HelloWorldEndpointdebug trace, in the application console.
Optional: To run the application on a JBoss EAP OpenShift platform, complete the following steps:
Add the
<cloud/>element to the plug-in configuration. For example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Rebuild the application:
mvn clean package
$ mvn clean packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Log in to your OpenShift instance using the
oc logincommand. Create a new project in OpenShift. For example:
oc new-project bootable-jar-project
$ oc new-project bootable-jar-projectCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following
occommands to create an application image:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Creates the
target/openshiftsubdirectory. The packaged application is copied into theopenshiftsubdirectory. - 2
- Imports the latest OpenJDK 17 imagestream tag and image information into the OpenShift project.
- 3
- Creates a build configuration based on the logging directory and the OpenJDK 17 imagestream.
- 4
- Uses the
target/openshiftsubdirectory as the binary input to build the application.
Deploy the application:
oc new-app logging oc expose svc/logging
$ oc new-app logging $ oc expose svc/loggingCopy to Clipboard Copied! Toggle word wrap Toggle overflow Get the URL of the route.
oc get route logging --template='{{ .spec.host }}'$ oc get route logging --template='{{ .spec.host }}'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Access the application in your web browser using the URL returned from the previous command. For example:
http://ROUTE_NAME/hello
http://ROUTE_NAME/helloCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verification: Issue the following command to view a list of OpenShift pods available, and to check the pods build statuses:
oc get pods
$ oc get podsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Access a running pod log of your application. Where
APP_POD_NAMEis the name of the running pod logging application.oc logs APP_POD_NAME
$ oc logs APP_POD_NAMECopy to Clipboard Copied! Toggle word wrap Toggle overflow Expected outcome: The pod log is in JSON format and includes the
com.example.logging.HelloWorldEndpointdebug trace.
11.14. Enabling web session data storage for multiple bootable JAR instances Copiar o linkLink copiado para a área de transferência!
You can build and package a web-clustering application as a bootable JAR.
Prerequisites
-
You have checked the latest Maven plug-in version, such as
9.minor.micro.Final-redhat-XXXXX, where 9 is the major version, minor is the minor version, micro micro version and X is the Red Hat build number. For example:9.0.1.Final-redhat-00009. You have created a Maven project, and added dependencies for creating a web-clustering application. See Creating a bootable JAR Maven project.
ImportantWhen setting up the Maven project, you must specify values in the Maven archetype configuration. For example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe examples shown in the procedure specify the following properties:
-
${bootable.jar.maven.plugin.version}for the Maven plug-in version.
You must set these properties in your project. For example:
<properties> <bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version> </properties><properties> <bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version> </properties>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Procedure
Add the following content to the
<build>element of thepom.xmlfile. For example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThis example makes use of the
web-clusteringGalleon layer to enable web session sharing.Update the
web.xmlfile in thesrc/main/webapp/WEB-INFdirectory with the following configuration:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
<distributable/>tag indicates that this servlet can be distributed across multiple servers.Create the directory to store Java files:
mkdir -p APPLICATION_ROOT
$ mkdir -p APPLICATION_ROOT /src/main/java/com/example/webclustering/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where
APPLICATION_ROOTis the directory containing thepom.xmlconfiguration file for the application.Create a Java file
MyServlet.javawith the following content and save the file in theAPPLICATION_ROOT/src/main/java/com/example/webclustering/directory.Copy to Clipboard Copied! Toggle word wrap Toggle overflow The content in
MyServlet.javadefines the endpoint to which a client sends an HTTP request.Create a Java file
User.javawith the following content and save the file in theAPPLICATION_ROOT/src/main/java/com/example/webclustering/directory.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Package the application:
mvn package
$ mvn packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: To run the application on a JBoss EAP bare-metal platform, follow the steps outlined in Using a bootable JAR on a JBoss EAP bare-metal platform, but with the following difference:
On a JBoss EAP bare-metal platform, you can use the
java -jarcommand to run multiple bootable JAR instances, as demonstrated in the following examples:java -jar target/web-clustering-bootable.jar -Djboss.node.name=node1 java -jar target/web-clustering-bootable.jar -Djboss.node.name=node2 -Djboss.socket.binding.port-offset=10
$ java -jar target/web-clustering-bootable.jar -Djboss.node.name=node1 $ java -jar target/web-clustering-bootable.jar -Djboss.node.name=node2 -Djboss.socket.binding.port-offset=10Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verification: You can access the application on the node 1 instance: http://127.0.0.1:8080/clustering. Note the user session ID and the user-creation time.
After you kill this instance, you can access the node 2 instance: http://127.0.0.1:8090/clustering. The user must match the session ID and the user-creation time of the node 1 instance.
Optional: To run the application on a JBoss EAP OpenShift platform, follow the steps outlined in Using a bootable JAR on a JBoss EAP OpenShift platform, but complete the following steps:
Add the
<cloud/>element to the plug-in configuration. For example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Rebuild the application:
mvn clean package
$ mvn clean packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Log in to your OpenShift instance using the
oc logincommand. Create a new project in OpenShift. For example:
oc new-project bootable-jar-project
$ oc new-project bootable-jar-projectCopy to Clipboard Copied! Toggle word wrap Toggle overflow To run a web-clustering application on a JBoss EAP OpenShift platform, authorization access must be granted for the service account that the pod is running in. The service account can then access the Kubernetes REST API. The following example shows authorization access being granted to a service account:
oc policy add-role-to-user view system:serviceaccount:$(oc project -q):default
$ oc policy add-role-to-user view system:serviceaccount:$(oc project -q):defaultCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following
occommands to create an application image:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Creates the
target/openshiftsub-directory. The packaged application is copied into theopenshiftsub-directory. - 2
- Imports the latest OpenJDK 17 imagestream tag and image information into the OpenShift project.
- 3
- Creates a build configuration based on the web-clustering directory and the OpenJDK 17 imagestream.
- 4
- Uses the
target/openshiftsub-directory as the binary input to build the application.
Deploy the application:
oc new-app web-clustering -e KUBERNETES_NAMESPACE=$(oc project -q) oc expose svc/web-clustering
$ oc new-app web-clustering -e KUBERNETES_NAMESPACE=$(oc project -q) $ oc expose svc/web-clusteringCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantYou must use the
KUBERNETES_NAMESPACEenvironment variable to view other pods in the current OpenShift namespace; otherwise, the server attempts to retrieve the pods from thedefaultnamespace.Get the URL of the route.
oc get route web-clustering --template='{{ .spec.host }}'$ oc get route web-clustering --template='{{ .spec.host }}'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Access the application in your web browser using the URL returned from the previous command. For example:
http://ROUTE_NAME/clustering
http://ROUTE_NAME/clusteringCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note the user session ID and user creation time.
Scale the application to two pods:
oc scale --replicas=2 deployments web-clustering
$ oc scale --replicas=2 deployments web-clusteringCopy to Clipboard Copied! Toggle word wrap Toggle overflow Issue the following command to view a list of OpenShift pods available, and to check the pods build statuses:
oc get pods
$ oc get podsCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Kill the oldest pod using the
oc delete pod web-clustering-POD_NAMEcommand, where POD_NAME is the name of your oldest pod. Access the application again:
http://ROUTE_NAME/clustering
http://ROUTE_NAME/clusteringCopy to Clipboard Copied! Toggle word wrap Toggle overflow Expected outcome: The session ID and the creation time generated by the new pod match those of the of the terminated pod. This indicates that web session data storage is enabled.
11.15. Enabling HTTP authentication for bootable JAR with a CLI script Copiar o linkLink copiado para a área de transferência!
You can enable HTTP authentication for the bootable JAR with a CLI script. This script adds a security realm and a security domain to your server.
Prerequisites
-
You have checked the latest Maven plug-in version, such as
9.minor.micro.Final-redhat-XXXXX, where 9 is the major version, minor is the minor version, micro micro version and X is the Red Hat build number. For example:9.0.1.Final-redhat-00009. You have created a Maven project, and added dependencies for creating an application that requires HTTP authentication. See Creating a bootable JAR Maven project.
ImportantWhen setting up the Maven project, you must specify HTTP authentication values in the Maven archetype configuration. For example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe examples shown in the procedure specify the following properties:
-
${bootable.jar.maven.plugin.version}for the Maven plug-in version.
You must set these properties in your project. For example:
<properties> <bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version> </properties><properties> <bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version> </properties>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Procedure
Add the following content to the
<build>element of thepom.xmlfile. For example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The example shows the inclusion of the
datasources-web-serverGalleon layer that contains theelytronsubsystem.Update the
web.xmlfile in thesrc/main/webapp/WEB-INFdirectory. For example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the directory to store Java files:
mkdir -p APPLICATION_ROOT/src/main/java/com/example/authentication/
$ mkdir -p APPLICATION_ROOT/src/main/java/com/example/authentication/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where
APPLICATION_ROOTis the root directory of your Maven project.Create a Java file
TestServlet.javawith the following content and save the file in theAPPLICATION_ROOT/src/main/java/com/example/authentication/directory.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a CLI script, such as
authentication.cli, and save it in an accessible directory in the bootable JAR, such as theAPPLICATION_ROOT/scriptsdirectory. The script must contain the following commands:/subsystem=elytron/properties-realm=bootable-realm:add(users-properties={relative-to=jboss.server.config.dir, path=bootable-users.properties, plain-text=true}, groups-properties={relative-to=jboss.server.config.dir, path=bootable-groups.properties}) /subsystem=elytron/security-domain=BootableDomain:add(default-realm=bootable-realm, permission-mapper=default-permission-mapper, realms=[{realm=bootable-realm, role-decoder=groups-to-roles}]) /subsystem=undertow/application-security-domain=other:write-attribute(name=security-domain, value=BootableDomain)/subsystem=elytron/properties-realm=bootable-realm:add(users-properties={relative-to=jboss.server.config.dir, path=bootable-users.properties, plain-text=true}, groups-properties={relative-to=jboss.server.config.dir, path=bootable-groups.properties}) /subsystem=elytron/security-domain=BootableDomain:add(default-realm=bootable-realm, permission-mapper=default-permission-mapper, realms=[{realm=bootable-realm, role-decoder=groups-to-roles}]) /subsystem=undertow/application-security-domain=other:write-attribute(name=security-domain, value=BootableDomain)Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following configuration extract to the plug-in
<configuration>element:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This example shows the
authentication.cliCLI script, which configures the defaultundertowsecurity domain to the security domain defined for your server.NoteYou have the option to execute the CLI script at runtime instead of packaging time. To do so, skip this step and proceed to step 10.
In the root directory of your Maven project create a directory to store the properties files that the JBoss EAP JAR Maven plug-in adds to the bootable JAR:
mkdir -p APPLICATION_ROOT/extra-content/standalone/configuration/
$ mkdir -p APPLICATION_ROOT/extra-content/standalone/configuration/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where
APPLICATION_ROOTis the directory containing thepom.xmlconfiguration file for the application.This directory stores files such as
bootable-users.propertiesandbootable-groups.propertiesfiles.The
bootable-users.propertiesfile contains the following content:testuser=bootable_password
testuser=bootable_passwordCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
bootable-groups.propertiesfile contains the following content:testuser=Users
testuser=UsersCopy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following
extra-content-content-dirselement to the existing<configuration>element:<extra-server-content-dirs> <extra-content>extra-content</extra-content> </extra-server-content-dirs><extra-server-content-dirs> <extra-content>extra-content</extra-content> </extra-server-content-dirs>Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
extra-contentdirectory contains the properties files.Package the application as a bootable JAR.
mvn package
$ mvn packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the application:
mvn wildfly-jar:run
mvn wildfly-jar:runCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you have chosen to skip step 6 and not execute the CLI script during build, launch the application with the following command:
mvn wildfly-jar:run -Dwildfly.bootable.arguments=--cli-script=scripts/authentication.cli
mvn wildfly-jar:run -Dwildfly.bootable.arguments=--cli-script=scripts/authentication.cliCopy to Clipboard Copied! Toggle word wrap Toggle overflow Call the servlet, but do not specify credentials:
curl -v http://localhost:8080/hello
curl -v http://localhost:8080/helloCopy to Clipboard Copied! Toggle word wrap Toggle overflow Expected output:
HTTP/1.1 401 Unauthorized ... WWW-Authenticate: Basic realm="Example Realm"
HTTP/1.1 401 Unauthorized ... WWW-Authenticate: Basic realm="Example Realm"Copy to Clipboard Copied! Toggle word wrap Toggle overflow Call the server and specify your credentials. For example:
curl -v -u testuser:bootable_password http://localhost:8080/hello
$ curl -v -u testuser:bootable_password http://localhost:8080/helloCopy to Clipboard Copied! Toggle word wrap Toggle overflow A HTTP 200 status is returned that indicates HTTP authentication is enabled for your bootable JAR. For example:
HTTP/1.1 200 OK .... Hello testuser
HTTP/1.1 200 OK .... Hello testuserCopy to Clipboard Copied! Toggle word wrap Toggle overflow