// build.gradle
def frontendDir = "$projectDir/src/main/frontend"
sourceSets {
main {
resources { srcDirs = ["$projectDir/src/main/resources"]
}
}
}
processResources { dependsOn "copyReactBuildFiles" }
task installReact(type: Exec) {
workingDir "$frontendDir"
inputs.dir "$frontendDir"
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'
}
}
task buildReact(type: Exec) {
dependsOn "installReact"
workingDir "$frontendDir"
inputs.dir "$frontendDir"
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 copyReactBuildFiles(type: Copy) {
dependsOn "buildReact"
from "$frontendDir/build"
into "$projectDir/src/main/resources/static"
}
./gradlew build
home directory에서 ./gradlew build
를 실행한다.
아래와 같이 BUILD SUCCESSFUL
이 나오면 성공한 것이다.
빌드된 jar 파일은 build/libs
에서 확인가능하다.
1번과 2번을 실행하고 나면 npm start를 통해 따로 프론트엔드를 실행시키지 않고, 빌드하여 실행하면 프론트엔드 코드와 백엔드 코드가 동시에 실행된다.
결과 : 하지만 백엔드 파일과 프론트엔드 파일을 따로 관리하기로 결정하였다. 따라서 빌드를 할 필요는 없지만 첫 시도에 의미를 두기로 하였다.