Deploy Spring Boot application on Tomcat

Deploy Spring Boot application on Tomcat

Step #1

Add the following dependency to pom.xml in order to tell Spring Boot not to use its embedded Tomcat.
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

Step #2

Change the packaging property to war in pom.xml.

<properties>
      <packaging>war</packaging>
</properties>

Step #3

Replace your initializer class with the following:

package com.oms;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

/**
 *
 * @author tariqul
 *
 */

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}
And then define this pom.xml:
<properties>
        <start-class>com.oms.Application</start-class>
</properties>

The Tomcat Maven Plugin (homepage at http://mojo.codehaus.org/tomcat-maven-plugin/introduction.html  or http://tomcat.apache.org/maven-plugin-2.2 ), can be used to perform tasks such as deploying, reploying, and undeploying a war file to Tomcat using the Tomcat Manager application. 
For this first need to configure maven and tomcat as below:

Step #4

Configure tomcat Roles:

Let’s make these changes in $CATALINA_HOME\conf\tomcat-users:

    <role rolename="manager"/>
    <role rolename="admin-gui"/>
    <role rolename="admin-script"/>
    <role rolename="manager-gui"/>
     <role rolename="tomcat"/>
    <role rolename="manager-script"/>
    <role rolename="manager-jmx"/>
    <role rolename="manager-status"/>
      <user username="omsmanager" password="password" roles="manager,admin-gui,admin-script,manager-gui,tomcat,manager-script,manager-jmx,manager-status"/>

Step #5

Set Directory Permissions:
Finally, ensure that there is read/write permission on the Tomcat installation directory.

Step #6

Configure maven:

we must configure Tomcat as a server in Maven’s settings.xml file.
There are two locations where the settings.xml file may be found:
  • The Maven install: ${maven.home}/conf/settings.xml
  • A user’s install: ${user.home}/.m2/settings.xml
Once you have found it add server as follows:
<server>
        <id>omsserver</id>
        <username>omsmanager</username>
        <password>
password</password>
</server>

Step #7

Now add plug in in pom.xml:

        <plugin>
                <!-- <groupId>org.codehaus.mojo</groupId> -->
                <!-- <artifactId>tomcat-maven-plugin</artifactId> -->
                <!-- <version>1.1</version> -->
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <server>omsserver</server>
                    <update>true</update>
                    <path>/OMSserver</path>
                    <username>omsmanager</username>
                    <password>
password</password>
                </configuration>
            </plugin>

Step #8

To deploy the web app:

 mvn tomcat7:deploy (if use http://tomcat.apache.org/maven-plugin-2.2)
 or 
 mvn tomcat:deploy (if use http://mojo.codehaus.org/tomcat-maven-plugin)
To undeploy it:

 mvn tomcat7:undeploy (if use http://tomcat.apache.org/maven-plugin-2.2)
 or 
mvn tomcat:undeploy (if use http://mojo.codehaus.org/tomcat-maven-plugin)
To redeploy after making changes:
mvn tomcat7:redeploy (if use http://tomcat.apache.org/maven-plugin-2.2)
or 
mvn tomcat:redeploy (if use http://mojo.codehaus.org/tomcat-maven-plugin)

You can download sample project from: https://github.com/tariqliferay/spring-boot-web-deploy-tomcat

 Thanks Enjoy!!!!!!!!!!!!!!!!!!


 


 

Comments

Popular posts from this blog

How to run ofbiz as ubuntu Service.

JPA vs Spring JPA vs Spring Data JPA vs Hibernate

Java Array Interview Questions for Entry-Level Developers Part 01