1

While trying to deploy a springboot application in an external tomcat, I'm not able to deploy. In the pom.xml ,if i exclude tomcat dependency then im not facing compile time error. Since i have used tomcat jar classes in my project. Please let me know how to deploy the project.

2 Answers2

1

I had the same issue as you and I came with a solution. It's maybe not the best, but it works...

First you need two application :

Application.java

@SpringBootApplication
public class Application {

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

WarApplication

@SpringBootApplication
public class WarApplication extends SpringBootServletInitializer {

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

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

After this you need to modify you pom.xml with this :

<profiles>
    <profile>
        <id>tomcat</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <project.packaging>war</project.packaging>
            <project.start-class>com.whatever.WarApplication</project.start-class>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>

    <profile>
        <id>springboot</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <project.packaging>jar</project.packaging>
            <project.start-class>com.whatever.Application</project.start-class>
        </properties>
    </profile>
</profiles>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <inherited>true</inherited>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <configuration>
                    <profiles>
                        <profile>tomcat</profile>
                        <profile>springboot</profile>
                    </profiles>
                    <mainClass>${project.start-class}</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Basically, you are just making profiles depending on where you have to deploy. But if you only need to deploy on tomcat, you could remove the profiles. What is important in this pom, it's the <scope>provided</scope> in the tomcat profile. Here is the link to Traditional Deployment from Spring doc.

GabLeg
  • 352
  • 4
  • 14
1

As an addition @Gableg's answer,

in pom.xml :

<packaging>war</packaging> set as war not jar.

Basically @Gableg's two condition and my condition i specified above is enough. Tree condition is enough to make an app deployable in external tomcat.

I referenced the video in below.It is really usable.

https://www.youtube.com/watch?v=05EKZ9Xmfws

Cocuthemyth
  • 264
  • 1
  • 2
  • 11