[Spring] Spring boot + React 프로젝트 생성 및 세팅

seonjeong·2024년 3월 4일
0

Spring

목록 보기
27/27

Spring Boot 프로젝트 생성

Spring initializr를 이용하여 프로젝트를 생성한다

Gradle Project
Spring Boot 3.2.3
java 17
IDE : IntelliJ


Spring Boot에 React 빌드 환경 구축하기

src/main/frontend 에 React 프로젝트를 추가한다

npx create-react-app frontend


프로젝트 빌드 및 실행하기

build.gradlesrc/main/fronted에 만들었던 React 프로젝트를 빌드하기 위한 내용을 추가한다

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
profile
🦋개발 공부 기록🦋

0개의 댓글

관련 채용 정보