본문 바로가기

Programming/Java

[Java] 외부 WAS에 Spring Boot 프로젝트(WAR 파일) 배포를 위한 설정 : 톰캣(tomcat) 제거하기

결론부터 이야기하자면, 외부 WAS에 Spring Boot 프로젝트를 배포하기 위해서는 내장 톰캣(tomcat)을 제거해야한다!

 

spring-boot-starter-web 의존성에는 기본적으로 톰캣(tomcat) 라이브러리가 내장되어 있어, 애플리케이션 실행 시 내장 서블릿 컨테이너인 톰캣이 자동으로 설정되어 동작한다. 이를 외부 WAS에서 동작해야 하는 경우나, 내장 서블릿 컨테이너를 톰캣이 아니나 다른 것으로 사용하고 싶은 경우, 배치 프로세스나 백그라운드 작업처럼 웹 서버가 필요하지 않은 경우 내장 톰캣을 제거해야 하는 경우를 위한 설정 방법이다.

 

  1. 내장된 톰캣 의존성을 'provided' 스코프로 설정
  2. SpringBootServletInitalizer 상속받도록 클래스 수정

 


 

1. 내장된 톰캣 의존성을 'provided' 스코프로 설정

Maven 환경으로 작업했기 때문에 pom.xml 파일을 수정해주었다. 아래는 Gradle 환경의 경우이다.

// pom.xml

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <!-- 추가한 부분 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
  </dependency>
</dependencies>
dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'
  
  // 추가한 부분
  providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}

 

2. SpringBootServletInitalizer 상속받도록 클래스 수정

SpringBootServletInitializer를 상속 후, SpringApplicationBuilder 메소드를 재정의 한다.

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

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

	// SpringApplicationBuilder 재정의한 부분
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(MyApplication.class);
	}
}

 

728x90
728x90