[springboot] 2.0 을 사용하면서 패키징과 관련된 문제가 발생한다면

스프링 부트 2.0 을 사용하면서 패키징과 관련된 찝찝한 느낌을 받고 있는 요즘이다.

그 주요 원인은 스프링 부트 그레이들 플러그인 2.0 때문이다. 스프링 부트 그레이들 플러그인 2.0이 되면서 기존에 있던 bootRepackagebootJarbootWar 으로 변경(Spring Boot’s new Gradle plugin)되면서 부터다.

실행가능한 jar와 war를 만드는데 사용했던 bootRepackage를 배포파일에 따라서 각각 JarbootJarWarbootWar로 확장하는 과정에서 이런 상황이 발생했다.

핵심요점만 이야기하자면 bootJarbootWar를 비활성화하면 패키징 태스크가 실행되지 않는다.

//bootJar 비활성화시
bootJar.enabled = false
jar.enabled = true

//bootWar 비활성화시
bootWar.enabled = false
war.enabled = true

위와 같은 형태로 상위 태스크를 활성화시켜줘야지만 패키징 태스크가 실행된다. 이러한 사실을 알게된 것은

가 있었고, 오늘은 EBS(AWS Elastic Beanstalk)에서 "구성 파일(.ebextensions)을 사용하여 고급 환경 사용자 지정" 기능을 사용하기 위해서 프로젝트 내에 .ebextensions 디렉터리를 war 파일 패키징시 이동하기 위해 그레이들내에서 다음과 같이 선언했는데 .ebextensions 디렉터리가 패키징에서 누락된 것을 발견했다.

build.gradle
project(":api-module") {
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'war'

    war {
        from('./src/main/ebextensions') {
            into('.ebextensions')
        }
    }

    dependencies {
        compile project(":core-module")

        compile('org.springframework.boot:spring-boot-starter-data-rest')
        compile('org.springframework.boot:spring-boot-starter-hateoas')
        compile('org.springframework.boot:spring-boot-starter-web-services')
        compile('org.springframework.boot:spring-boot-starter-webflux')
        compile('org.springframework.data:spring-data-rest-hal-browser')

        compileOnly "org.springframework.boot:spring-boot-configuration-processor"

        testCompile('org.springframework.boot:spring-boot-starter-test')
        testCompile('io.projectreactor:reactor-test')
    }
}

위와 같이 모듈을 정의했는데, war 태스크를 정의한 부분이 무시됐다. (이런!!)

하지만 그렇다고 분노하거나 노여워하지 말자(그건 내가 다 했으니…​).

여기서 대응할 수 있는 방법은 크게 2가지가 있다.

  • 새로추가된 booWar 태스크를 사용하는 경우

  • 기존 war 태스크를 유지하는 경우

Note

스프링 부트를 기반으로 할 때는 실행가능하게 패키징(Repackaging)을 하는 bootJarbootWar를 사용한다고 생각하면 마음의 평화가 찾아올 듯 하다.

추천방법: 새로 추가된 booWar 태스크를 사용하는 경우

project(":api-module") {
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'war'

    bootWar {
        from('./src/main/ebextensions') {
            into('.ebextensions')
        }
    }

    dependencies {
        compile project(":core-module")

        compile('org.springframework.boot:spring-boot-starter-data-rest')
        compile('org.springframework.boot:spring-boot-starter-hateoas')
        compile('org.springframework.boot:spring-boot-starter-web-services')
        compile('org.springframework.boot:spring-boot-starter-webflux')
        compile('org.springframework.data:spring-data-rest-hal-browser')

        compileOnly "org.springframework.boot:spring-boot-configuration-processor"

        testCompile('org.springframework.boot:spring-boot-starter-test')
        testCompile('io.projectreactor:reactor-test')
    }
}

기존 war 태스크를 유지하는 경우

Note

톰캣과 같은 WAS에 war 파일을 배포한다면 실행가능하게 패키징을 필요로 하지 않으니 bootWar를 비활성화 하자.

project(":api-module") {
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'war'

    bootWar.enabled = false // (1)
    war.enabled = true  // (2)

    war {
        from('./src/main/ebextensions') {
            into('.ebextensions')
        }
    }

    dependencies {
        compile project(":core-module")

        compile('org.springframework.boot:spring-boot-starter-data-rest')
        compile('org.springframework.boot:spring-boot-starter-hateoas')
        compile('org.springframework.boot:spring-boot-starter-web-services')
        compile('org.springframework.boot:spring-boot-starter-webflux')
        compile('org.springframework.data:spring-data-rest-hal-browser')

        compileOnly "org.springframework.boot:spring-boot-configuration-processor"

        testCompile('org.springframework.boot:spring-boot-starter-test')
        testCompile('io.projectreactor:reactor-test')
    }
}
  1. bootWar를 비활성화한다.

  2. war를 활성화한다.

정리

  • 기술을 사용할 때는 참고문서를 보자.

참고문헌

[springboot] The value of a manifest attribute must not be null (Key=Start-Class)

그레이들을 기반으로 스프링 부트 프로젝트 스크립트를 하나하나 작성하다보니 장애하나를 접했다.

$ ./gradlew clean build
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':c-app:bootJar'.
> The value of a manifest attribute must not be null (Key=Start-Class). // (1)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
10 actionable tasks: 10 executed
  1. Start-Class

눈치가 있는 분이라면 (Key=Start-Class) 항목이 눈에 띌 것이다. 스프링 부트 그레이들 플러그인에서 bootJar 태스크를 실행하는 과정에서 시작 클래스를 찾지 못했다. 이 문제가 발생할 당시에 메인클래스를 작성하지 않았다(혹은 public static void main(String[] args) 메서드가 작성되지 않았을 때 발생).

public class Application {
    public static void main(String[] args) {}
}

다음과 같은 형태로 간단한 메인클래스를 작성하면 빌드는 정상적으로 진행된다.


+ Recent posts