Sunday, December 17, 2017

SpringBoot application Tomcat deployment

Leave a Comment

I have a SpringBoot application that I am trying to deploy to Tomcat Server. According to references online, I have added some code in the Application class as follows:

public class SkyVetApplication extends SpringBootServletInitializer{ ...    @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {     return application.sources(SkyVetApplication.class); } ... } 

In build.gradle I have added the following:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-web' **providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'** 

After doing a clean build I have copied the war file to Tomcat's webapps folder. But the deployment happens twice and ends with an exception as context is already present. What am I missing?

Help is very much appreciated.

1 Answers

Answers 1

You should add a main method.

Check out these examples: https://github.com/Pytry/bootiful-war-deployment

Here's an example from the "hello" module (it's using Lombok annotation processors).

package com.example.bootifulwar;  import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled;  @SpringBootApplication @EnableScheduling @Slf4j public class HelloServletInitializer extends SpringBootServletInitializer{      @Value("${messageForUser}")     private String message;      @Value("${whatDoesTheFoxSay:'No body knows.'}")     private String whatDoesTheFoxSay;      public static void main(String[] args){          SpringApplication.run(HelloServletInitializer.class, args);     }      @Scheduled(fixedRate = 2000)     public void sayHelloTo(){          log.info("Hello! " + message);     }      @Override     public SpringApplicationBuilder configure(SpringApplicationBuilder application){          log.info(             "\n*********************\n" +                 "What does the fox say?\n" +                 whatDoesTheFoxSay +                 "\n*********************\n");         return application.sources(HelloServletInitializer.class);     } } 

To take advantage of individualized logging and external "application.properties", assuming you deploy more than one war file to the same Tomcat, you will need to place a custom context.xml for each applications context-path inside of "conf/Catalina/localhost.".

Example:

<?xml version='1.0' encoding='utf-8'?> <Context docBase="hello.war" path="hello">   <Resources className="org.apache.catalina.webresources.StandardRoot">     <PreResources base="hello\\config"                   className="org.apache.catalina.webresources.DirResourceSet"                   internalPath="/"                   webAppMount="/WEB-INF/classes"/>   </Resources> </Context> 

I'm not an expert at gradlem but your dependencies look fine.

Hope that helps.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment