교재 실습/자바 웹 개발 워크북

126. 스프링 IoC 컨테이너 사용 준비 (3)

Jint 2022. 10. 4. 22:17

4. 스프링 프레임워크 관련 라이브러리 가져오기

프로젝트에서 스프링 프레임워크의 기능을 활용하려면 관련 라이브러리를 가져와야 한다. Gradle을 사용하면 라이브러리를 제공하는 홈페이지에 접속하여 번거롭게 파일을 내려받기할 필요가 없다. Gradle 설정 파일에 필요한 라이브러리 정보를 등록만 하면 된다.

(교재에서 등록한 스프링 라이브러리 정보는 예전 방식이라 그래들 빌드시 오류가 발생한다.)

스프링 홈페이지 https://spring.io/에 접속하여 Learn - Guides - Build Java Projects with Gradle와 Learn - Quickstart에서 https://start.spring.io/를 실행하여 만들어지는 프로젝트의 build.gradle을 참고하여 그래들 빌드를 진행했다.

 

- Build Java Projects with Gradle의 build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'

mainClassName = 'hello.HelloWorld'

// tag::repositories[]
repositories { 
    mavenCentral() 
}
// end::repositories[]

// tag::jar[]
jar {
    archiveBaseName = 'gs-gradle'
    archiveVersion =  '0.1.0'
}
// end::jar[]

// tag::dependencies[]
sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    implementation "joda-time:joda-time:2.2"
    testImplementation "junit:junit:4.12"
}
// end::dependencies[]

// tag::wrapper[]
// end::wrapper[]

 

- Spring Initializr의 build.gradle

plugins {
	id 'org.springframework.boot' version '2.7.4'
	id 'io.spring.dependency-management' version '1.0.14.RELEASE'
	id 'java'
	id 'war'
	id 'org.springframework.experimental.aot' version '0.12.1'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
	maven { url 'https://repo.spring.io/release' }
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
	useJUnitPlatform()
}

tasks.named('bootBuildImage') {
	builder = 'paketobuildpacks/builder:tiny'
	environment = ['BP_NATIVE_IMAGE': 'true']
}

그림 1 (Spring Initializr)

참고링크 : https://start.spring.io/#!type=gradle-project&language=java&platformVersion=2.7.4&packaging=war&jvmVersion=1.8&groupId=com.example&artifactId=demo&name=demo&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.demo&dependencies=native

 

- 위 두 소스를 참고하여 만든 build.gradle

//https://spring.io/guides/gs/gradle/ 참고하여 그래들 빌드

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'war'

compileJava.options.encoding = 'UTF-8'

eclipse {
	wtp {
		facet {
			facet name: 'jst.web', version: '3.0' // Servlet Spec Version 지정
			facet name: 'jst.java', version: '1.8' // Java Version 지정, 1.8 ...
		}
	}
}

// tag::repositories[]
repositories { 
    mavenCentral() 
}
// end::repositories[]

// tag::jar[]
jar {
    archiveBaseName = 'gs-gradle'
    archiveVersion = '0.1.0'
}
// end::jar[]

// tag::dependencies[]
sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    implementation "joda-time:joda-time:2.2"
    testImplementation "junit:junit:4.12"
    implementation 'org.springframework.boot:spring-boot-starter-web'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
// end::dependencies[]

// tag::wrapper[]
// end::wrapper[]

tasks.named('test') {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}

앞으로 실습을 진행하며 차차 수정해나갈 예정이다.

 

참고도서 : https://freelec.co.kr/book/1674/

 

[열혈강의] 자바 웹 개발 워크북

[열혈강의] 자바 웹 개발 워크북

freelec.co.kr