2023-12-05 작성

Spring Boot 내장 톰캣 제외시키는 방법

Spring Boot로 개발된 애플리케이션을 실행할 때, 내장 톰캣이 아니라 다른 서버로 배포해야 하는 경우가 있다. (예를 들어 내장톰캣이 아니라 Weblogic 서버로 배포해야 할 경우)

이런 경우 내장톰캣 관련된 설정을 모두 제거해야 한다. Spring Boot 애플리케이션에서 내장 톰캣서버를 제외시키는 방법은 간단하다.

내장톰캣 제외하기

<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>

pom.xml에서 <exclusion>으로 내장 톰캣을 제외하면 된다.

그런데 간혹 내장톰캣 제외 설정을 해도 안지워지는 경우가 있다. maven repository에서 내장톰캣을 사용하는 라이브러리들을 찾은 뒤,  마찬가지로 <exclusion>로 제외시키면 된다.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

애플리케이션을 war/jar로 배포해보면 lib 폴더에 톰캣 관련한 jar가 모두 제외된 것을 확인할 수 있다.

(참고) 다른 웹서버 사용하는법

Spring Doc 를 보면, 다양한 Spring Boot starter 패키지로 기본 내장 컨테이너가 포함되어 있다. 이런 경우 웹서버를 쉽게 가져다 쓸 수 있다.

  • spring-boot-starter-web (spring-boot-starter-tomcat를 포함)
  • spring-boot-starter-undertow
  • spring-boot-starter-webflux
  • spring-boot-starter-reactor-netty
  • spring-boot-starter-jetty