Red Hat Training

A Red Hat training course is available for Red Hat Fuse

Chapter 3. Building with Maven

The standard approach to developing applications for Spring Boot in JBoss Fuse is to use the Apache Maven build tool and to structure your source code as a Maven project. JBoss Fuse provides Maven quickstarts to get you started quickly and many of the JBoss Fuse buld tools are provided as Maven plug-ins. For this reason, it is highly recommended that you adopt Maven as the build tool for Spring Boot projects in JBoss Fuse.

3.1. Generating a Maven project

JBoss Fuse provides a selection of quickstarts, based on Maven archetypes, which you can use to generate an initial Maven project for a Spring Boot application. To save you having to remember the location information and versions for various Maven archetypes, JBoss Fuse provides tooling to help you generate Maven projects for standalone Spring Boot projects.

3.1.1. Project generator at launch.openshift.io

The quickest way to get started with Spring Boot standalone in JBoss Fuse is to navigate to launch.openshift.io and follow the instructions for the Spring Boot standalone runtime, to generate a new Maven project. After following the on-screen instructions, you will be prompted to download an archive file, which contains a complete Maven project that you can build and run locally.

3.1.2. Fuse tooling wizard in Developer Studio

Alternatively, you can download and install Red Hat JBoss Developer Studio (which includes Fuse Tooling). Using the Fuse New Integration Project wizard, you can generate a new Spring Boot standalone project and continue to develop inside the Eclipse-based IDE.

3.2. BOM file

The Maven Bill of Materials (BOM) file is the first crucial element of a Spring Boot project for JBoss Fuse. The purpose of a BOM file is to provide a curated set of Maven dependency versions that work well together, saving you from having to define versions individually for every Maven artifact.

The JBoss Fuse BOM offers the following advantages:

  • Defines versions for all Maven dependencies related to JBoss Fuse, saving you the trouble of doing this yourself.
  • Defines a curated set of curated dependencies that are fully tested and supported for a specific version of JBoss Fuse.
  • Simplifies upgrades of JBoss Fuse — to upgrade to a later patch of JBoss Fuse, simply change the version of the BOM file.
Important

Only the set of dependencies defined by a JBoss Fuse BOM are supported by Red Hat.

A BOM file is incorporated into your Maven project by specifying a dependencyManagement element in your project’s pom.xml file (or, possibly, in a parent POM file), as shown in the following example:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project ...>
  ...
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- configure the versions you want to use here -->
    <fabric8.version>3.0.8.fuse-000023-redhat-2</fabric8.version>
    <spring-boot.version>1.5.8.RELEASE</spring-boot.version>

    <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
    <maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>io.fabric8</groupId>
        <artifactId>fabric8-project-bom-camel-spring-boot</artifactId>
        <version>${fabric8.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  ...
</project>

After specifying the BOM using the dependency management mechanism, it becomes possible to add Maven dependencies to your POM without specifying the version of the artifact. For example, to add a dependency for the camel-hystrix component, you would add the following XML fragment to the dependencies element in your POM:

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-hystrix-starter</artifactId>
</dependency>

Note how the Camel artifact ID is specified with the -starter suffix — that is, you specify the Camel Hystrix component as camel-hystrix-starter, not as camel-hystrix. The Camel starter components are packaged in a way that is optimized for the Spring Boot environment. For more details, see Chapter 4, Apache Camel in Spring Boot.

3.3. Spring Boot Maven plug-in

The Spring Boot Maven plug-in is the second crucial element of a Spring Boot project for JBoss Fuse. This Maven plug-in is provided by Spring Boot and it is a developer utility for building and running a Spring Boot project:

  • Building — create an executable Jar package for your Spring Boot application by entering mvn package in your project directory. The output of the build is placed in the target/ subdirectory of your Maven project.
  • Running — for convenience, you can run the newly-built application with the command, mvn spring-boot:start.

For more details about the Spring Boot Maven plug-in, see Appendix A, Spring Boot Maven Plug-In.

To incorporate the Spring Boot Maven plug-in into your project POM file, add the plug-in configuration to the project/build/plugins section of your pom.xml file, as shown in the following example:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project ...>
  ...
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- configure the versions you want to use here -->
    <fabric8.version>3.0.8.fuse-000023-redhat-2</fabric8.version>
    <spring-boot.version>1.5.8.RELEASE</spring-boot.version>

    <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
    <maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
  </properties>
  ...
  <build>
    <pluginManagement>
      <plugins>

      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

Note that the version of the spring-boot-maven-plugin artifact is not specified in the preceding example, because the version has already been specified by Maven dependency management (see Section 3.2, “BOM file”).