Web Java Spring-boot와 React를 war로 묶어서 배포하는 방법.
페이지 정보
본문
안녕하세요. 엔지엠소프트웨어입니다. Backend는 Java Spring-boot로 개발하고, Frontend는 React로 개발된 프로젝트인데요. 리눅스 서버에 톰캣을 설치하고, 이곳에 war 파일을 배포할겁니다. 그런데, React를 따로 배포하기는 좀 번거로워서 자바 스프링의 resources/static에 React를 빌드하고, 파일을 복사하도록 했습니다. 자바 프로젝트가 gradle로 되어 있어서, build.gradle에 아래 내용을 추가하면 됩니다. 경로는 자신의 프로젝트에 맞게 변경하셔야 합니다^^
def webappDir = "$projectDir//.."
sourceSets {
main {
resources {
srcDirs = ["$webappDir/frontend/dist/apps/rpa_macro", "$projectDir/src/main/resources"]
}
}
}
processResources {
dependsOn "copyWebApp"
}
task copyWebApp(type: Copy) {
dependsOn "buildReact"
from "$webappDir/frontend/dist/apps/rpa_macro"
into "$projectDir/src/main/resources/static"
}
task buildReact(type: Exec) {
dependsOn "installReact"
workingDir "$webappDir/frontend"
inputs.dir "$webappDir/frontend"
group = BasePlugin.BUILD_GROUP
if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')) {
commandLine "npm.cmd", "run-script", "build"
} else {
commandLine "npm", "run-script", "build"
}
}
task installReact(type: Exec) {
workingDir "$webappDir/frontend"
inputs.dir "$webappDir/frontend"
group = BasePlugin.BUILD_GROUP
if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')) {
commandLine "npm.cmd", "audit", "fix"
commandLine 'npm.cmd', 'install'
} else {
commandLine "npm", "audit", "fix"
commandLine 'npm', 'install'
}
}
터미널에서 ./gradlew build를 실행하면, 자바 소스의 resources/static에 react 빌드 실행 파일들이 만들어집니다. 물론, 서버의 포트나 기타 설정들은 미리 맞춰놔야 합니다. 좀 더 편리하게 웹프로젝트를 배포하려면 yml이나 log, properties와 같은 설정 파일들은 외부로 빼놔야 합니다. 그리고, java -jar <war 파일명> <main args>와 같이 실행하면 됩니다.
개발자에게 후원하기
추천, 구독, 홍보 꼭~ 부탁드립니다.
여러분의 후원이 빠른 귀농을 가능하게 해줍니다~ 답답한 도시를 벗어나 귀농하고 싶은 개발자~
감사합니다~
추천0 비추천0
- 이전글Azure DevOps Service에서 소스 저장소(Repository) 삭제하는 방법. 23.04.05
- 다음글Linux CentOS - Java 백그라운드 실행 방법. (nohup) 23.04.04
댓글목록
등록된 댓글이 없습니다.