๋šœ๋ถ€๋‹ˆ์˜ Devlog

Spring ํ”„๋กœ์ ํŠธ ๋‚ด๋ถ€์— React ์„ค์ • ๋ฐ Build (Gradle)

by ๋šœ๋ถ€๋‹ˆ
ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€์„ Spring ํ”„๋กœ์ ํŠธ์™€ React์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์„ ์ œ๊ณตํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.

 

React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

React ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์€ ์œ„์น˜์— ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

npx create-react-app {ํ”„๋กœ์ ํŠธ ์ด๋ฆ„}

๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ”„๋กœ์ ํŠธ๊ฐ€ ์›ํ•˜๋Š” ์œ„์น˜์— ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

CORS ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก Package.json์— proxy ์„ค์ •์„ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค.

{
	// ...
	"proxy": "http://localhost:8080"
}

 

ํ†ตํ•ฉ Build ์„ค์ •ํ•˜๊ธฐ

Spring์—์„œ React๋ฅผ Build ํ•  ์ˆ˜ ์žˆ๋„๋ก ์•„๋ž˜ ์„ค์ •์„ Gradle์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์ด๋•Œ, frontendDir์€ React ํ”„๋กœ์ ํŠธ Path์ž…๋‹ˆ๋‹ค.

// React ์„ค์ • ์‹œ์ž‘
def frontendDir = "$projectDir/daangn-react"

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"
}
// React ์„ค์ • ๋

ํ•ด๋‹น ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

  1. React ํ”„๋กœ์ ํŠธ ๋‚ด๋ถ€์—์„œ npm install์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  2. React ํ”„๋กœ์ ํŠธ ๋‚ด๋ถ€์—์„œ npm build๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ build ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‚ด๋ถ€์— ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.
  3. React ํ”„๋กœ์ ํŠธ build ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ƒ์„ฑ๋œ ์ •์  ํŒŒ์ผ์„ spring ํ”„๋กœ์ ํŠธ์˜ resources/static์œผ๋กœ ๋ณต์‚ฌํ•œ๋‹ค.

 

์ฐธ๊ณ 

'Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Spring DI  (0) 2022.11.27
properties๋กœ Spring Boot log๋ฅผ ์„ค์ •ํ•ด๋ณด์ž!  (0) 2022.04.23

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

๋šœ๋ถ€๋‹ˆ์˜ Devlog

๋šœ๋ถ€๋‹ˆ

ํ™œ๋™ํ•˜๊ธฐ