28

How to deploy Spring Boot application in IBM Liberty and WAS 8.5

 3 years ago
source link: http://www.adeveloperdiary.com/java/spring-boot/deploy-spring-boot-application-ibm-liberty-8-5/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

April 4, 2016 By Abhisek Jana 8 Comments

How to deploy Spring Boot application in IBM Liberty and WAS 8.5

How to deploy Spring Boot application in IBM Liberty and WAS 8.5

We have developed RESTful services using Spring Boot in our last tutorial. Now we will learn How to deploy Spring Boot application in IBM Liberty and WAS 8.5. Many of us are still using IBM Application Servers and it’s very important to understand how we can deploy our spring boot code there.

Since Spring Boot’s main objective is to support Microservices and Cloud Native Applications,it includes embedded Tomcat\Jetty. IBM is also moving towards a very similar direction with their Liberty Profile Application Server and Bluemix. Now Liberty has Maven plugin to create, build and deploy Spring Boot Application to IBM Liberty Profile Application Server, which is either hosted locally or remotely using Bluemix. They have also provided similar support for Cloud Foundry (Could Native). So it has become very crucial to understand how to deploy Spring Boot application in IBM Liberty and WAS 8.5 (Websphere Application Server 8.5).

Moreover, many of our existing infrastructure is already using Websphere Application Server 8.5 / IBM Liberty profile. If we want to enable Spring Boot with our current infrastructure we need to also find a way to deploy the application there.

I am not discussing in detailed on WebLogic/JBOSS (Wildfly) here, the same mechanics would apply there as well.

There are mainly two ways to configure & deploy Spring Boot application in IBM Liberty Profile Application Server. Here are the approaches we shall experiment with.

  1. Configure Spring Boot using spring-boot-starter-parent
  2. Configure Spring Boot Application using net.wasdev.wlp.starters.springboot

Once our Spring Boot Application is working in IBM Liberty Profile, we will then deploy the application in IBM WebSphere Application Server 8.5.

P.S – The Java code would stay same across each deployment process, however the configuration and pom file would be different.

IBM Liberty Profile

spring-boot-starter-parent

As you have already created some Spring Boot Application by now, you can find that in each pom file has a parent artifact id as spring-boot-starter-parent. We will keep that as is and deploy our app.

Make sure you have the latest version (8.5.5.9, at the time to writing this blog) of IBM Liberty Profile, also its good to have the full JEE server. If you are not sure check the read-me file for the server version and run the following command to install the full JEE7 profile.

./installUtility install javaee-7.0

Changes to Maven Configuration File

Since we were using embedded Tomcat server we were using a jar file to run our app, now we need to create a war file to deploy that to IBM Liberty Server. So at first change packaging to war from jar

Add the Tomcat starter to exclusion list.

pom.xml
XHTML
<exclusions>
   <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
   </exclusion>
</exclusions>

Since we need Servlet 3.1 we need to add the dependency here.

pom.xml
XHTML
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
   <scope>provided</scope>
</dependency>

In case you are using an old application server and want to use Servlet 3.0, use the following dependency instead.

pom.xml
XHTML
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

Here is my pom file.

pom.xml
XHTML
<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.adeveloperdiary</groupId>
   <artifactId>springweb</artifactId>
   <version>1.0</version>
   <packaging>war</packaging>
   <name>spring_web</name>
   <description>springweb</description>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.3.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <properties>
      <start-class>com.adeveloperdiary.ServletInitializer</start-class>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <java.version>1.8</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <version>3.1.0</version>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
         <exclusions>
            <exclusion>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
         </exclusions>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-actuator</artifactId>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
               <execution>
                  <goals>
                     <goal>repackage</goal>
                  </goals>
                  <configuration>
                     <skip>true</skip>
                  </configuration>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-resources-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

Change the Code

Still now we had the main method as the starting point of our Spring Boot Application, however since we are using a WAR file we can’t leverage that. We need to change our main class to following. I also have a REST service to test the app.

@SpringBootApplication(exclude = MessageSourceAutoConfiguration.class)
public class ServletInitializer extends SpringBootServletInitializer {
   /*public static void main(String[] args) {
      SpringApplication.run(ServletInitializer.class, args);
   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(ServletInitializer.class);
@RestController
class RESTController {
   @RequestMapping("/")
   public String hello() {
      return “<H1>Spring Boot Application from adeveloperdiary.com</H1>”;

Liberty Server Configuration

There isn’t anything to update/add in the server.xml of the Liberty Profile. Here is my version.

pom.xml
XHTML
<server description="new server">
<!-- Enable features -->
<featureManager>
<feature>javaee-7.0</feature>
<feature>adminCenter-1.0</feature>
<feature>localConnector-1.0</feature>
</featureManager>
<variable name="defaultHostName" value="localhost" />
<!-- Define an Administrator and non-Administrator -->
<basicRegistry id="basic">
<user name="admin" password="adminpwd" />
<user name="nonadmin" password="nonadminpwd" />
</basicRegistry>
<!-- Assign 'admin' to Administrator -->
<administrator-role>
<user>admin</user>
</administrator-role>
<keyStore id="defaultKeyStore" password="Liberty" />
<httpEndpoint host="*" httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint" />
<!-- Automatically expand WAR files and EAR files -->
<applicationManager autoExpand="true" />
    <applicationMonitor updateTrigger="mbean" />
</server>

Deploy To Liberty Profile

You can manually drop the war file to the dropins folder once liberty has started it will install the app.

In case you are using eclipse, just add the war to the server, the app will be automatically compiled. One quick tip here is, if you don’t create the spring boot app from STS by selecting war packaging option, you may not see the war file listed here.In that case you need to build using Maven and deploy the war manually.

How to deploy Spring Boot application in IBM Liberty and WAS 8.5 adeveloperdiary.com

In case you are using IntelliJ IDEA, you need to add a Liberty server profile and add Maven build then deploy artifact. Every time you start the server or redeploy the app it will automatically compile everything and then deploy the war file.

How to deploy Spring Boot application in IBM Liberty and WAS 8.5 adeveloperdiary.com

Once you access the URL of the deployed app, Spring Boot will initialize and load the related configurations.

net.wasdev.wlp.starters.springboot

IBM has recently started supported Spring Boot using net.wasdev.wlp.starters.springboot. Let me provide you the pom I will be using here.

If you look closely there are many differences of packages here and you can see IBM provided starters has been used here.

pom.xml
XHTML
<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>myParentGroupId</groupId>
   <artifactId>SpringBootLiberty</artifactId>
   <version>1.0</version>
   <packaging>war</packaging>
   <scm>
      <connection>scm:git:[email protected]:WASdev/tool.artisan.core.git</connection>
      <developerConnection>scm:[email protected]:WASdev/tool.artisan.core.git</developerConnection>
      <url>[email protected]:WASdev/tool.artisan.core.git</url>
   </scm>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
   </properties>
   <repositories>
      <repository>
         <id>liberty-starter-maven-repo</id>
         <name>liberty-starter-maven-repo</name>
         <url>http://liberty-starter.wasdev.developer.ibm.com/start/api/v1/repo</url>
      </repository>
   </repositories>
   <dependencies>
      <dependency>
         <groupId>net.wasdev.wlp.starters.springboot</groupId>
         <artifactId>provided-pom</artifactId>
         <version>0.0.1</version>
         <type>pom</type>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>net.wasdev.wlp.starters.springboot</groupId>
         <artifactId>runtime-pom</artifactId>
         <version>0.0.1</version>
         <type>pom</type>
         <scope>runtime</scope>
      </dependency>
      <dependency>
         <groupId>net.wasdev.wlp.starters.springboot</groupId>
         <artifactId>compile-pom</artifactId>
         <version>0.0.1</version>
         <type>pom</type>
         <scope>compile</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-actuator</artifactId>
         <version>1.3.3.RELEASE</version>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
               <failOnMissingWebXml>false</failOnMissingWebXml>
               <packagingExcludes>pom.xml</packagingExcludes>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

The Java code we changed previously will stay same, however IBM has provided support for defining Context root and web.xml. Also you can add any html file. You need to create a folder named webapp at the same level of java and resources and add following files there. Here my directory structure.

How to deploy Spring Boot application in IBM Liberty and WAS 8.5 adeveloperdiary.com

You can now define the context root in the ibm-web-ext.xml file.

ibm-web-ext.xml
XHTML
<web-ext
    xmlns="http://websphere.ibm.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-ext_1_0.xsd"
    version="1.0">
  <context-root uri="/SpringBootLiberty"/>
</web-ext>

The web.xml is a basic one I will be using

web.xml
XHTML
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>Spring Boot Liberty</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

Also added an index.html file to make sure its working as expected. When you build your app using the above pom file, it will add the webapp folder in your war file.

I have created the pom specific for development perspective. You can find more details here (http://liberty-app-accelerator.wasdev.developer.ibm.com/).

Here is my IntelliJ configuration.

How to deploy Spring Boot application in IBM Liberty and WAS 8.5 adeveloperdiary.com

Once you run the server you will get following logs.

How to deploy Spring Boot application in IBM Liberty and WAS 8.5 adeveloperdiary.com

IBM WebSphere Application Server 8.5

In order to deploy Spring Boot Application to IBM WebSphere Application Server 8.5 sever you need to have the latest fix pack installed to get the most compatibility. At the time of writing this post the latest fix pack is 8.5.5.9, please make to download and install it.

I have also tested using JDK 6,7 & 8, all of them are working fine. You need to generate the war file using Maven and deploy that to server. Use the above pom files to create the war file.

When using spring-boot-starter-parent, you need to manually downgrade the Servlet version to 3.0 since WAS 8.5.5.9 supports only till Servlet 3.0 and not 3.1. You need to use the following dependency.

pom.xml
XHTML
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

I was able to make both spring-boot-starter-parent & net.wasdev.wlp.starters.springboot working in IBM WebSphere Application Server 8.5. If you notice we had defined start-class in the properties section, WAS will automatically use it to load the Servlet Initializer during server startup.

You will not be able to use Rational Application Developer with the build, you probably need eclipse or IntelliJ IDEA for your development.

Conclusion

You will probably be using embedded Tomcat during your development and use a Maven build to create the WAR specific for Liberty or WAS 8.5. There might be conflicts with older version of the jars when you use WAS 8.5, however Liberty supports most of the latest versions. We will see more compatibility between Spring Boot and Liberty, however may not be so much for WAS 8.5.

This post is the 4th part of Getting Started with Spring Boot Series. Find all the parts here.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK