The Payara Micro Maven plugin

The Maven plugin is capable of automatically downloading, starting, and stopping a managed instance of Payara Micro. Besides that, it is also able to automatically deploy Microservices built with Maven. The creation of Payara Micro Uberjar is also one of the capabilities of the Maven plugin.


As a good practice, before the plugin declaration itself, create a build-wide property that contains the plugin's version.

The following code shows properties tab for Maven:

<properties>
<payaramicro.maven.plugin.version>1.0.0</payaramicro.maven.plugin.version>
<!-- Other properties omitted -->
</properties>

As a next step, the declaration of the plugin itself in pom.xml is necessary. This is also the final step. Standard Maven requirements for a plugin, such as a group identifier and artifact identifier, distinguished by a plugin version, are necessary. Everything happens automatically. The only configuration required is to tell Payara to deploy WAR files on startup. The execution phase is set to package in order to automatically generate an UberJar with both the Microservice and Payara inside every time the Maven package or install goals are executed:

<build>
<plugins>
<plugin>
<groupId>fish.payara.maven.plugins</groupId>
<artifactId>payara-micro-maven-plugin</artifactId>
<version>${payaramicro.maven.plugin.version}</version>
<configuration>
<deployWar>true</deployWar>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

All WAR are bundled and deployed automatically with such settings. If a WAR is required, instructing Maven to use the WAR packaging is necessary:

<packaging>war</packaging>

Dependencies are standard. The Java EE 8 API as a provided dependency is required for the sample Weather Microservice used throughout this book, as well as the common POJOs used for this project specifically. The final POM with the preceding dependencies is demonstrated as follows:

<?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">
<parent>
<artifactId>javaee8-Microservices-book</artifactId>
<groupId>com.packtpub.Microservices</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>10-payara-micro</artifactId>
<packaging>war</packaging>

<properties>
<payaramicro.maven.plugin.version>1.0.0</payaramicro.maven.plugin.version>
<!-- Other properties omitted -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>fish.payara.maven.plugins</groupId>
<artifactId>payara-micro-maven-plugin</artifactId>
<version>${payaramicro.maven.plugin.version}</version>
<configuration>
<deployWar>true</deployWar>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
</dependency>
</dependencies>


</project>

In the target/ folder created by Maven, there is the WAR file with the Microservice. Additionally, with the plugin settings demonstrated, there is an UberJar with the -microbundle.jar suffix. In the case of the sample project, its name is 10-payara-micro-1.0-microbundle.jar. The sample project with Payara Micro is, of course, available in the package bundled to this book.

The UberJar may be executed manually by using the CLI. To do this, simply run the UberJar as any other Java application: java -jar application-name-microbundle.jar. The sample application is run by issuing the java -jar 10-payara-micro-1.0-microbundle.jar command. Alternatively, the payara-micro:start Maven goal can be used.

The started Microservice binds itself to port 8080, using the name of WARS as application context roots. During startup, Payara Micro informs what all internal Microservices are deployed. The simple Weather Microservice that provides an arbitrary temperature is available at /weather/temperature, as in other projects in this book. Therefore, the resulting URL for the Weather Microservice is http://localhost:8080/10-payara-micro-1.0/weather/temperature. Sending an HTTP GET request to this URL returns an arbitrary temperature:

Deployed: 10-payara-micro-1.0.war ( 10-payara-micro-1.0.war war /10-payara-micro-1.0 )
Payara Micro URLs
http://localhost:8080/10-payara-micro-1.0
]]

Payara Micro will download the latest stable release by default. Alternatively, the plugin can be pointed to a local instance of Payara Micro, or instructed to download a different version, if required. To instruct Payara Micro to download and manage a specific version, use Maven's standard plugin configuration block:

        <artifactItem>
            <groupId>fish.payara.extras</groupId>
            <artifactId>payara-micro</artifactId>
            <version>4.1.1.171</version>
        </artifactItem>

In the rare case that there is a Payara Micro instance available on the filesystem and intended to be used, an absolute path to the Payara MicroJAR can be specified as well:

<payaraMicroAbsolutePath>/path/to/payara-micro.jar</payaraMicroAbsolutePath>
..................Content has been hidden....................

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