[spring boot] dependencies (의존성) 설정 

 spring boot 3.4.2 version에서의 설정 예제입니다. 

 

  build.gradle 파일에서 dependencies 블록은 프로젝트에서 필요한 라이브러리를 정의하는 영역입니다. Gradle은 이 블록에서 선언된 의존성을 바탕으로 빌드에 필요한 모든 파일(라이브러리 및 그 하위 의존성 포함)을 다운로드하고 관리합니다.

1. 의존성 정의의 기본 형식

의존성을 선언하는 기본 형식은 다음과 같습니다.

dependencies {
    configuration "group:artifact:version"
}

 

  • configuration: 의존성의 역할이나 범위를 정의합니다. (예: implementation, testImplementation, runtimeOnly 등)
  • group: 라이브러리의 그룹 ID(Maven에서의 Group ID와 동일).
  • artifact: 라이브러리 이름(Maven에서의 Artifact ID와 동일).
  • version: 라이브러리의 버전.

예제: Spring Boot Starter Web

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web:2.7.5'
}

위의 의존성은 다음과 같은 Maven 좌표를 나타냅니다:

  • 그룹 ID: org.springframework.boot
  • 아티팩트 ID: spring-boot-starter-web
  • 버전: 2.7.5

2. 주요 의존성 범위 (Configuration)

1) implementation

  • 설명: 애플리케이션 코드에서 사용하는 기본 의존성.
  • 특징:
    • 의존성이 컴파일 및 런타임에 포함됩니다.
    • 하위 모듈에 전파되지 않습니다.
  • 예제
implementation 'org.springframework.boot:spring-boot-starter-web'

2) api

  • 설명: 라이브러리를 사용하는 외부 프로젝트에서 의존성을 노출해야 할 때 사용.
  • 특징:
    • implementation과 달리 하위 모듈에서도 사용할 수 있도록 전파됩니다.
  • 예제
api 'org.apache.commons:commons-lang3:3.12.0'

3) compileOnly

  • 설명: 컴파일 시에만 필요한 의존성.
  • 특징:
    • 런타임에는 포함되지 않습니다.
    • Lombok 같은 어노테이션 처리 라이브러리에서 주로 사용.
  • 예제
compileOnly 'org.projectlombok:lombok:1.18.26'
annotationProcessor 'org.projectlombok:lombok:1.18.26'

4) runtimeOnly

  • 설명: 런타임 시에만 필요한 의존성.
  • 특징:
    • 컴파일 시에는 필요 없지만, 실행 중에만 사용됩니다.
  • 예제
runtimeOnly 'mysql:mysql-connector-java:8.0.33'

5) testImplementation

  • 설명: 테스트 코드에서만 사용하는 의존성.
  • 특징:
    • JUnit, Mockito 등의 테스트 라이브러리에서 주로 사용.
  • 예제
testImplementation 'org.springframework.boot:spring-boot-starter-test'

3. 다양한 의존성 선언 방식

1) 단일 문자열 형식

가장 일반적인 방식으로, Maven 좌표(group:artifact:version)를 명시합니다.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web:2.7.5'
}

2) version 생략

Spring Boot 프로젝트는 io.spring.dependency-management 플러그인을 통해 버전을 관리하므로, 일반적으로 버전을 생략합니다.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

3) 별도의 변수 사용

의존성 버전을 변수로 정의하면 관리가 용이합니다.

ext {
    springBootVersion = '2.7.5'
}
dependencies {
    implementation "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
}

4) Maven BOM 사용

Maven BOM(Bill of Materials)을 사용해 여러 의존성의 버전을 일관되게 관리할 수 있습니다.

dependencyManagement {
    imports {
        mavenBom "org.springframework.boot:spring-boot-dependencies:2.7.5"
    }
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

5) 로컬 프로젝트 의존성

같은 프로젝트 내의 다른 모듈을 참조할 때 사용합니다.

dependencies {
    implementation project(':moduleA')
}

6) 로컬 파일 의존성

JAR 파일을 직접 참조해야 할 경우 사용합니다.

dependencies {
    implementation files('libs/my-library-1.0.0.jar')
}

4. 실무에서 유용한 팁

1) 의존성 트리 확인

gradle dependencies 명령어를 사용하면 프로젝트의 의존성 트리를 확인할 수 있습니다.

./gradlew dependencies

2) 의존성 충돌 해결

의존성 충돌이 발생할 경우, dependencyManagement 블록에서 우선순위를 설정하거나 특정 버전을 강제로 지정합니다.

dependencies {
    implementation 'com.google.guava:guava:31.1-jre'
    implementation 'org.example:conflicting-lib:1.2.3'
}

3) 선택적 의존성 추가

환경에 따라 특정 라이브러리를 추가하려면 Gradle의 조건문을 활용합니다.

dependencies {
    if (project.hasProperty('useMysql')) {
        runtimeOnly 'mysql:mysql-connector-java:8.0.33'
    } else {
        runtimeOnly 'org.postgresql:postgresql:42.6.0'
    }
}

5. 대표적인 의존성 예시

dependencies {
    // Spring Boot 의존성
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'

    // 데이터베이스
    runtimeOnly 'mysql:mysql-connector-java'
    runtimeOnly 'org.postgresql:postgresql'

    // 테스트 라이브러리
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.mockito:mockito-core:5.5.0'
}

 

+ Recent posts