Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

How to use Eclipse JKube Maven plugins in 3 steps

July 27, 2020
Marc Nuri
Related topics:
Developer ToolsJavaKubernetes
Related products:
Red Hat OpenShift

Share:

    Before we had Spring Boot and similar frameworks, a web app container was the main requirement for deploying Java web applications. We now live in the age of microservices, and many Java applications are developed on top of Quarkus, Thorntail, or Spring Boot. But some use cases still require an old-school web application.

    In this article, you will learn how to deploy a Java web application (WAR) into a Kubernetes or Red Hat OpenShift cluster using Eclipse JKube. I'll show you how easy it is to make a monolithic Java web application cloud-native, just by adding Eclipse JKube Maven plugins.

    The example web application

    For the purpose of this article, we'll work with a very simple Spring Web MVC application. The following sample shows the most relevant parts of the Maven project's pom.xml:

    <!-- ... -->
    <packaging>war</packaging>
    <!-- ... -->
    <properties>
      <maven.compiler.source>11</maven.compiler.source>
      <maven.compiler.target>11</maven.compiler.target>
      <failOnMissingWebXml>false</failOnMissingWebXml>
      <!-- ... -->
      <jkube.enricher.jkube-service.type>NodePort</jkube.enricher.jkube-service.type>
    </properties>
    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${version.spring}</version>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
      </dependency>
    </dependencies>
    <build>
      <plugins>
        <plugin>
          <groupId>org.eclipse.jkube</groupId>
          <artifactId>kubernetes-maven-plugin</artifactId>
          <version>${version.jkube}</version>
        </plugin>
        <!-- ... -->
      </plugins>
    </build>
    

    First, notice the packaging element, which indicates that the resulting artifact should be packaged as a war file, which is the usual format for Java web applications.

    In the properties section, notice that the project is configured for Java 11. We'll need a compatible JDK to build the project. We use the failOnMissingWebXml option to configure the maven-war-plugin so that it won't fail due to a missing web.xml file. (You will see soon what we're using instead.) Finally, there's a JKube-specific property, jkube.enricher.jkube-service.type. This property configures JKube to create a service-resource manifest using NodePort as the spec.type.

    In the dependencies section, we find just two dependencies. The spring-webmvc dependency lets us use the Spring Web MVC framework. The javax.servlet-api dependency provides compile-time support for the Java Servlet API, which is provided by the web application container at runtime.

    Finally, in the plugins section, we've configured the Eclipse JKube dependency. Note that we can use either the kubernetes-maven-plugin or the openshift-maven-plugin. The choice of plugin depends on the cluster we want to target, but we only need one of them.

    The configuration is simple and straightforward. The only thing different from a typical, old-school Java web application is the Eclipse JKube plugin dependency.

    Java classes in the example project

    The example project contains three Java classes: ExampleInitializer, ExampleConfiguration, and ExampleResource. First, ExampleInitializer is a WebApplicationInitializer implementation. We're using it instead of the standard WEB-INF/web.xml deployment descriptor to configure the ServletContext programmatically.

    The following code shows how ExampleInitializer lets us register Spring's DispatcherServlet without any additional XML configuration:

    final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(ExampleConfiguration.class);
    context.setServletContext(servletContext);
    final ServletRegistration.Dynamic dsr = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
    dsr.setLoadOnStartup(1);
    dsr.addMapping("/");
    

    The ExampleConfiguration class is a Spring-specific configuration enabling Spring MVC.

    Finally, ExampleResource is a standard Spring @RestController. It has a single request mapping that responds Hello!!! to any GET request.

    Deploy the web application into Kubernetes

    We'll start by deploying the example web application into Kubernetes; then, I'll show you how to make a couple of adjustments and deploy it into OpenShift.

    Step 1: Build the application

    The first step is to build the project just as we would build any other Maven web application project. Running mvn clean package generates a new war artifact in the target directory of target/example-0.0.0-SNAPSHOT.war.

    For the purpose of this example, we'll use Minikube. So that we can pull the image from the cluster without having to push it to a shared registry, we'll use Minikube's docker daemon, eval $(minikube docker-env).

    We can now issue the mvn k8s:build command to build the docker image for our application, as shown in Figure 1.

    mvn k8s:build
    Figure 1: Build the docker image for the application.

    A new docker image will be tagged as webapp/example:latest in our docker registry.

    Step 2: Create the cluster configuration

    Next, we create the required cluster configuration resource manifests and apply them to the kubectl- configured cluster, as shown in Figure 2.

    mvn k8s:resource k8s:apply
    Figure 2: Create and deploy the cluster configuration.

    The previous commands will generate the Kubernetes configuration manifests in target/classes/META-INF/jkube/kubernetes.yml, and will apply them to the cluster.

    Step 3: Verify that the application is running

    Finally, we verify that everything is running by entering the commands shown in Figure 3.

    kubectl get pod
    Figure 3: Verify that the application is running.

    Deploy the web application into OpenShift

    Following similar steps to those in the previous section, we can seamlessly deploy the example web application to an OpenShift cluster, just by using the openshift-maven-plugin instead of the kubernetes-maven-plugin:

    <build>
      <plugins>
        <plugin>
          <groupId>org.eclipse.jkube</groupId>
          <artifactId>openshift-maven-plugin</artifactId>
          <version>${version.jkube}</version>
        </plugin>
      </plugins>
    </build>
    

    In this case, the plugin prefix is oc instead of k8s, but the goals to be run are the same. Note that in this case, the build step uses S2I instead of docker to perform the build.

    mvn oc:build
    Figure 4: Build the container image using S2I binary build strategy for the application.
    mvn k8s:resource k8s:apply
    Figure 5: Create and deploy the cluster OpenShift specific configuration.

    The developer experience

    You've seen how Eclipse JKube lets us easily deploy a web application to the cloud. It also has other features to ease our lives as developers. Let's look at how Eclipse JKube enhances a typical log retrieval.

    Log retrieval

    If we want to print the logs for the web application that we just deployed, we can simply run mvn k8s:log. The logs will be printed and followed (or tailed) in the current console, as shown in Figure 6.

    mvn k8s:log
    Figure 6: Logs for the Kubernetes application deployment.

    The screenshot in Figure 6 shows the log for the application we've just deployed on Kubernetes. You can see that the Apache Tomcat web application container was started and that the web application was deployed into the ROOT context.

    Note: By default, Eclipse JKube uses Apache Tomcat as its web application container, via the jkube/jkube-tomcat9-binary-s2i base image. In a follow-up article, I will show you how to use different web application containers (such as Jetty), just by adding container-specific files.

    Conclusion

    In this article, you've seen how easy it is to convert an old-school Java web application into a full-fledged cloud-native application, just by adding the Eclipse JKube plugin dependency to your Maven POM. The complete source code for the examples is available on GitHub.

    If you're interested in learning more about Eclipse JKube, you can visit our main GitHub repository, website, or Gitter channel. And don't forget to follow us on Twitter!

    Last updated: September 19, 2023

    Related Posts

    • Introduction to Eclipse JKube: Java tooling for Kubernetes and Red Hat OpenShift

    • Java development on top of Kubernetes using Eclipse JKube

    • Modernizing Enterprise Java: A cloud native guide for developers

    Recent Posts

    • The benefits of auto-merging GitHub and GitLab repositories

    • Supercharging AI isolation: microVMs with RamaLama & libkrun

    • Simplify multi-VPC connectivity with amazon.aws 9.0.0

    • How HaProxy router settings affect middleware applications

    • Fly Eagle(3) fly: Faster inference with vLLM & speculative decoding

    What’s up next?

    This ebook will show you how to modernize traditional, monolithic Java-based applications into cloud-native models with Kubernetes.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue