java/Spring Boot

[build.gradle] dev/prod 분리

내가 만드는게 길이 된다 2023. 11. 16. 17:18

[라이센스 파일 default(local)/dev/prod 따로 build]

1. 폴더구조

\src\main\resources-env\dev\temp_server_license.xml
<license version="1.0">
dev_1
</license>



\src\main\resources-env\prod\temp_server_license.xml
<license version="1.0">
prod_1
</license>



2. build.gradle 스크립트

bootWar { 
    archiveFileName = 'ROOT.war'
    ...
}

# profile 값이 없을 경우 default 설정
ext.profile = (!project.hasProperty('profile') || !profile) ? 'default' : profile
 
# profile 값에 따른 소스파일 변경 설정
sourceSets {
    main {
        resources {
        	srcDirs "src/main/resources", "src/main/resources-env/${profile}"
        }
    }
}



3. eclipse에 Run Configuration을  추가 (prod 버전 추가)

프로젝트
 >> 우클릭
  >> Run As
   >> Run Configurations
    >> Gradle Task
      >> New Configurations
          >> Name 에 구분할 이름 입력 : warProd
          >> Gradle Tasks 탭에서
               Gradle Tasks 에 bootWar 입력 
          >> Project Settings 탭에서
                Program Arguments 에 아래 추가
                 -Pprofile=prod
          >> Apply
          >> Run



4. 'ROOT.war' 생성하기

프로젝트
 >> 우클릭
  >> Run As
   >> Run Configurations
    >> Gradle Task
    >> Run



5. 묶인 파일 확인해보기

 >> workspace\프로젝트명\build\libs\ROOT.war
 >> 파일 풀어서 
    temp_server_license.xml 파일 내용이 아래와 같은지 확인한다.
<license version="1.0">
prod_1
</license>



  

참조문서 >>
https://jiurinie.tistory.com/113
https://perfectacle.github.io/2017/09/23/Spring-boot-gradle-profile/#Profile-%EA%B5%AC%EC%84%B1%ED%95%98%EA%B8%B0
https://velog.io/@fivewinds/eclipse-gradle-%EB%B9%8C%EB%93%9C-%EC%8B%9C-profile-%EC%84%A4%EC%A0%95%ED%95%98%EA%B8%B0