[Gradle] --project-prop 옵션으로 특정 파일 제외하여 빌드하기

웹/Java|2025. 3. 7. 16:44

이번에 Swagger를 활용해 API 문서를 작성하면서,

개발 서버에서만 쓰이는 내부용 컨트롤러에 아예 접근할 수 없도록 처리할 필요가 있었다.

 

@Hidden, @Profile 등 여러 방법을 찾아 보다 gradle로 아예 파일 자체를 빼고 빌드하기로 했다.

 

https://docs.gradle.org/current/userguide/command_line_interface.html#sec:environment_options

 

Command-Line Interface Reference

Gradle provides several built-in tasks which show particular details of your build. This can be useful for understanding your build’s structure and dependencies, as well as debugging problems. Running the projects task gives you a list of the subprojects

docs.gradle.org

 

gradle로 스프링 부트 프로젝트를 빌드할 때, -p 라는 옵션으로 시스템 프로퍼티를 설정할 수 있는데,

./gradlew clean build --no-scan --build-cache -x test -P{property}

 

이렇게 설정하고 나서 gradle.build에서 project.hasProperty('property값') 으로 조건을 만들어 제외할 파일을 지정할 수 있다.

// 제외할 폴더 지정 (빌드시 -Pproperty 사용한 경우 적용됨)
jar {
    if (project.hasProperty('property')) {
        sourceSets {
            main {
                java {
                    exclude "**/제외할 폴더 또는 파일 경로"
                }
            }
        }
    }
}

 

그렇게 빌드하고 나서 jar 파일로 프로젝트를 실행시키면 해당 파일은 깔끔하게 제외된다!

댓글()