Fabric8 Maven Plugin

repository·master·Indexed 43 days ago

https://github.com/fabric8io/fabric8-maven-plugin

A tool for automating Kubernetes and OpenShift deployments from Maven, providing goals for building Docker images, pushing to registries, and deploying resource objects. It features a customization model using Generators and Enrichers and supports zero, inline, and external configuration styles. Note: This plugin has been deprecated and rebranded as Eclipse JKube.

Tokens
22.8K
Snippets
60
Records
101
Agent score
55%

What's inside Fabric8 Maven Plugin

  1. Overview of fabric8-maven-plugin capabilities

    master

    The fabric8-maven-plugin (f8-m-p) integrates Maven with Kubernetes and OpenShift. It focuses on two primary tasks:

    1. Building Docker images: Creating container images containing your application (using fabric8:build).
    2. Creating resource descriptors: Generating Kubernetes and OpenShift YAML descriptors (using fabric8:resource).

    It supports three configuration models:

    • Zero-Config: Uses opinionated defaults based on your pom.xml (e.g., detecting Spring Boot).
    • XML Configuration: Provides type-safe configuration within the pom.xml (partially implemented).
    • Resource Fragments: Uses external YAML files in src/main/fabric8 that are enriched with build metadata.
  2. Use the Vert.x generator for fat jar applications

    master

    The Vert.x generator is used to detect Eclipse Vert.x applications and generate the necessary metadata to start them as a fat jar.

    This generator is automatically enabled if:

    • You are using the Vert.x Maven Plugin.
    • You depend on io.vertx:vertx-core and use the Maven Shader plugin.

    If neither condition is met, the generator behaves identically to the java-exec generator, supporting both common generator options and java-exec options.

  3. How profiles are looked up and merged

    master

    The plugin searches for profiles.yml files in the following order:

    1. META-INF/fabric8/profiles-default.yml from the compile/plugin classpath (reserved for plugin defaults).
    2. META-INF/fabric8/profiles.yml from the compile/plugin classpath (used for custom profiles in dependencies).
    3. src/main/fabric8/profiles.yml in your project (can be customized via the resourceDir property or fabric8.resourceDir system property).

    Merging Rules

    When multiple profiles share the same name:

    • Order: Profiles with a higher order value take precedence. If no order is provided, it defaults to 0.
    • Includes: If multiple profiles include the same processor, that processor is moved to the earliest position in the merged list.
    • Configuration: Configuration maps are merged; higher-order profiles override configuration values of lower-order profiles for the same keys.
  4. Automatic Vert.x generator behaviors

    master

    The Vert.x generator performs several automatic configurations based on your project's classpath and dependencies:

    • Metrics & JMX: Enables metrics and JMX publishing if io.vertx:vertx-dropwizard-metrics is present.
    • Clustering: Appends the -cluster flag to the command line if a Vert.x cluster manager is detected in the classpath.
    • Infinispan: Forces the IPv4 stack if vertx-infinispan is used.
    • DNS: Disables the async DNS resolver to fallback to the regular JVM DNS resolver.
  5. Use default generators to create Docker build configurations

    master

    The fabric8-maven-plugin includes several default generators that examine your build information to automatically generate Docker build configurations on the fly. Each generator is designed for specific application types and can be configured using generator-specific options.

    Available default generators include:

    • Java Applications (java-exec): Generic generator for flat classpath and fat-jar Java applications.
    • Spring Boot (spring-boot): Specific generator for Spring Boot applications.
    • Wildfly Swarm (wildfly-swarm): Generator for Wildfly Swarm apps.
    • Thorntail v2 (thorntail-v2): Generator for Thorntail v2 apps.
    • Vert.x (vertx): Generator for Vert.x applications.
    • Karaf (karaf): Generator for Karaf based apps.
    • Web applications (webapps): Generator for WAR based applications supporting Tomcat, Jetty, and Wildfly base images.
    • Open Liberty (webapps): Generator for Open Liberty applications.
  6. Use Resource Fragments for Kubernetes/OpenShift YAML

    master

    Resource Fragments allow you to use standard Kubernetes/OpenShift YAML files located in src/main/fabric8. The plugin enriches these files with build metadata (like image names and labels) and combines them into a single deployment manifest.

    Key behaviors:

    • Automatic Kind derivation: The Kind of the resource is derived from the filename (e.g., deployment.yml becomes a Deployment).
    • Automatic Metadata: You do not need to provide a metadata section; the plugin adds it automatically.
    • Naming: If you name a file myapp-deployment.yml, the resource name will be myapp. Otherwise, it defaults to the Maven artifactId.
    • Sidecars: To ensure a container is treated as a sidecar rather than being merged into the main application container, ensure it has a unique name or override the default main container name using the fabric8.generator.alias property.
    # src/main/fabric8/deployment.yml
    spec:
      replicas: 1
      template:
        spec:
          volumes:
            - name: config
              gitRepo:
                repository: 'https://github.com/jstrachan/sample-springboot-config.git'
                revision: 667ee4db6bc842b127825351e5c9bae5a4fb2147
                directory: .
          containers:
            - volumeMounts:
                - name: config
                  mountPath: /app/config
              env:
                - name: KUBERNETES_NAMESPACE
                  valueFrom:
                    fieldRef:
                      apiVersion: v1
                      fieldPath: metadata.namespace
            serviceAccount: ribbon
  7. How the webapp generator detects servlet containers

    master

    The webapp generator automatically detects the type of WAR build by inspecting your pom.xml and project structure. It selects a base servlet container image based on the following rules:

    • Tomcat: Selected if tomcat6-maven-plugin or tomcat7-maven-plugin is present, or if META-INF/context.xml is found in the classes directory.
    • Jetty: Selected if jetty-maven-plugin is present, or if WEB-INF/jetty-web.xml or WEB-INF/jetty-logging.properties is found.
    • Wildfly: Selected if jboss-as-maven-plugin or wildfly-maven-plugin is present, or if a Wildfly-specific deployment descriptor like jboss-web.xml is found.

    Note: S2I (Source-to-Image) builds are currently not supported for the webapp generator.

  8. How debugging is enabled in containers

    master

    The fabric8:debug goal enables debugging by setting the JAVA_ENABLE_DEBUG environment variable to true.

    This variable is automatically respected by standard Docker images used by:

    • Spring Boot
    • Flat classpath and executable JAR projects
    • Wildfly Swarm

    If you are using a custom Docker base image, you must ensure your startup script respects the JAVA_ENABLE_DEBUG environment variable to enable debugging.

  9. What are Profiles in fabric8-maven-plugin?

    master

    Profiles are named combinations of generators and enrichers defined in YAML. They allow you to bundle specific resource generation and enrichment logic under a single referable name, making it easy to apply consistent configurations to your Kubernetes/OpenShift resources.

    Key components of a profile include:

    • name: The unique identifier for the profile. You can extend predefined profiles by using the same name.
    • generator: Defines which generators to use to create resources.
    • enricher: Defines which enrichers to use to modify or add to existing resources.
    • order: An integer used to resolve conflicts when multiple profiles with the same name are merged. Higher order takes precedence.
    • extends: (Optional) Allows a profile to inherit generators and enrichers from another profile to avoid repetition.
    - name: my-spring-boot-apps
      generator:
        includes:
          - spring-boot
      enricher:
        includes:
          - fmp-controller
          - fmp-service
        excludes:
          - f8-icon
        config:
          fmp-service:
            type: NodePort
      order: 10
  10. How Generators and Enrichers work in fabric8-maven-plugin

    master

    The fabric8-maven-plugin provides two extension mechanisms to customize the creation of Docker images and Kubernetes resource descriptors:

    Generators

    Generators are used to automatically create or customize image configurations during Docker image creation. They function similarly to Spring Boot Generator POMs: they can be enabled or disabled by declaring a Maven dependency.

    Generators can inspect the build environment to detect specific features (for example, determining if a build is a Spring Boot application or a plain WAR file). Based on this detection, they can automatically select base images or configure exposed ports for the image build configuration. Generators are primarily intended for Zero-Config use cases.

    Enrichers

    Enrichers are used to customize Kubernetes resource descriptors. They can add build metadata as labels or automatically create resources like ReplicaSet or Service based on the image configuration.

    Like Generators, Enrichers can be toggled via Maven dependencies or XML configuration. Unlike Generators, Enrichers are useful across all configuration variants, not just Zero-Config setups.

  11. How the Spring Boot watcher works

    master

    The Spring Boot watcher is enabled by default for Spring Boot projects. It automates the following workflow:

    1. Deploys your application with Spring Boot DevTools enabled.
    2. Tails the logs of the latest running pod for your application.
    3. Watches the local development build and triggers a reload of the application when changes are detected.

    To trigger a reload, simply build your project in a separate shell (e.g., mvn package), and the running fabric8:watch goal will detect the change and reload the app on the fly.

  12. Use the java-exec generator to run Java applications

    master

    The java-exec generator is a generic generator used to start arbitrary Java applications within a container. It supports two primary deployment patterns:

    1. Fat-jar applications: A single JAR containing the application and all dependencies, with a Main-Class entry in META-INF/MANIFEST.MF.
    2. Flat classpath applications: A main class is explicitly provided, and the image includes the application JAR plus all separate dependency JARs.

    Main Class Detection Logic

    If you do not explicitly configure the mainClass option, the generator follows this priority:

    1. Fat-jar detection: It looks for a JAR file with a Main-Class in its manifest. If multiple exist, the largest one is selected.
    2. Automatic scanning: If no fat-jar is found, it scans target/classes for exactly one class containing public static void main(String args[]). If zero or multiple classes are found, the generator does nothing.
    3. Explicit configuration: If you provide the mainClass option, the generator skips detection and builds a flat classpath image containing the application and its dependencies.

    Including Additional Files

    You can add custom files to the generated image by placing them in the following directory structure:

    • src/main/fabric8-includes: Files placed here are added with mode 0644.
    • src/main/fabric8-includes/bin: Files placed here are added with mode 0755 (executable).