Creating the project with Maven

Spring Boot is a Java-based technology, therefore creating a Java project is required. Maven is going to be used as a project management tool. The process of creating a basic Java project with Maven is explained at the beginning of this chapter, under the Setting up the development environment section, since the process is the same for both Java EE and Spring applications. As a result, a project with an almost empty pom.xml file should be created:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>

<groupId>com.packpub.microservices</groupId>
<artifactId>weather-service-spring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>02-Smart City Weather Microservice with Spring Boot</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</
project>

Having created a basic project with Maven, Spring Framework support can be integrated by making several changes to pom.xml. As a bare minimum, it is required to do the following:

  • Add a Spring Boot Starter parent, that is, a parent tag with group and version details
  • Add Spring Boot dependencies
  • Add the Spring Boot Maven plugin

Spring Boot dependencies provide a curated set of Spring Framework parts with the functionality required. The Spring Boot Maven plugin is required to package the Spring application into a Java Archive (JAR) and make it runnable. In order for the Spring Boot Maven plugin to work, additional Maven configurations and dependencies are required. However, developers are not forced to configure Maven builds to provide a suitable environment for Spring Boot to work. All the required functionality and settings are hidden in a single Maven parent, named the Spring Boot Starter parent.

Adding the Spring Boot Starter parent is the first logical step, since the rest of the steps are dependent on its presence:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>

<groupId>com.packpub.microservices</groupId>
<artifactId>weather-service-spring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>02-Smart City Weather Microservice with Spring Boot</name>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>

Spring Boot applications are subject to a unique packaging system, where all the dependencies, including the optional Java EE parts that Spring is dependent on, are included and compressed into a single Java Archive. Such a JAR is then easily runnable. Maven on its own does not contain the functionality to create such a package. To create a Spring-Boot-runnable JAR, a Maven plugin is required. It is therefore necessary, after the Spring Boot Starter parent is in place, to input the Spring Boot Maven plugin, which provides the functionalities required. This plugin is added to the <plugins> section of the <build> settings section:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>

<groupId>com.packpub.microservices</groupId>
<artifactId>weather-service-spring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>02-Smart City Weather Microservice with Spring Boot</name>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.6.RELEASE</version>
</plugin>
</plugins>
</build>

</project>

In the current state, the Spring-Boot-runnable JAR can be created, but there are no Spring Framework libraries to provide any functionality whatsoever. With Spring Boot, adding Spring Framework functionality is much easier than ever before. Spring itself has many modules with lots of useful functionality, but resolving mutual dependencies could be quite difficult at times. Spring Boot introduces a new system of dependencies, representing a more curated set of Spring Framework functionality. The goal is to create a very basic Microservice that communicates with other participants by means of a RESTful interface. With Spring Boot, this goal requires only one dependency:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>

<groupId>com.packpub.microservices</groupId>
<artifactId>weather-service-spring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>02-Smart City Weather Microservice with Spring Boot</name>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.6.RELEASE</version>
</plugin>
</plugins>
</build>

</project>

The created .pom file describes a basic Spring Boot application completely. Nothing more is required.

It is also possible to use a dedicated web tool named Spring Initializr, available from http://start.spring.io. Spring Initializr is an interactive tool to generate Maven- or Gradle-based Spring Boot projects. For more complex projects with non-trivial functionality, project outline creation is greatly simplified. It also provides an overview of available functionality, which can be added to the project by simply clicking on a checkbox. If you would like a similar experience for your JavaEE projects, WildFly Swarm provides the same capabilities. With WildFly Swarm, you don't need to specify an exhaustive set of required dependencies, as there is automatic detection available. For more information, visit http://wildfly-swarm.io/generator/.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.245.140