티스토리 뷰

스프링부트 profile과 Maven/Gradle의 profile의 차이

스프링부트의 profile은 runtime시 작동하는 것이고 Maven/Gradle의 profile은 build 시점 동작하는 것입니다. 

maven/gradle에서는 profile에 따라 해당 설정 정보를 선별적으로 포함시키고자할 때 이용이 됩니다. 

Maven 예제 

1. 리소스 디렉토리 생성

 

프로젝트를 생성하고 리소스 디렉토리를 추가합니다. src/main/resources에는 개발/qa/운영과 상관없이 공통적으로 사용되는 것들을 넣습니다. 그외 환경에 따라 달라 지는 것들은 profile로 구분하여 생성합니다. 

src/main/resources-[profile] 

2. pom.xml 수정 

    <!-- profile 추가 : dev, qa, release  -->
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <environment>dev</environment>
                <maven.test.skip>true</maven.test.skip>
            </properties>
        </profile>
        <profile>
            <id>qa</id>
            <properties>
                <environment>qa</environment>
                <maven.test.skip>true</maven.test.skip>
            </properties>
        </profile>
        <profile>
            <id>release</id>
            <properties>
                <environment>release</environment>
                <maven.test.skip>true</maven.test.skip>
            </properties>
        </profile>
    </profiles>
   <!-- build 수정 -->
   <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources-${environment}</directory>
            </resource>
        </resources>
    </build>
 

3. run maven 

clean install -Dmaven.test.skip=true -Pdev

 

 

4. 결과 확인

 

 

Gradle 예제

1. 리소스 디렉토리 생성 : maven과 동일하게 한다. 

2. build.gradle 수정 

sourceSets { 

    if (!project.hasProperty('profile') || !profile) { 

        ext.profile = 'dev' 

    } 

    main { 

        java {

            srcDirs "src/main/java" 

        } 

        resources { 

            srcDirs "src/main/resources", "src/main/resources-${profile}" 

        } 

    } 

}

 

3. run gradle : clean build -x test Pprofile=dev

 

 

 

4. 결과 확인 

 

Spring boot profile 예제 

1. properties 추가 : application-[profile].properties

2. properties 수정 

 

application.properties

spring.profiles.active=qa

profile을 qa로 설정한다. 

 

application-dev.properties

app.title=eble-dev

 

application-qa.properties

app.title=eble-qa

 

application-release.properties

app.title=eble-release

 

TestController

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
public class TestRestController {
 
    @Value("${app.title}")
    private String appTitle;
    
    @GetMapping("/test")
    public String test() {
        
        return appTitle;
    }
}
cs

이렇게 하면 현재 실행하는 profile을 확인 할 수 있다. 

 

실행 결과 

eblo-qa

 

Github 링크

https://github.com/kkaok/examples

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함