의존성 충돌?
소프트웨어 프로젝트에서 특정 라이브러리나 패키지가 서로 다른 버전의 동일한 라이브러리를 요구할 때 발생하는 문제.
이로 인해 빌드 실패, 런타임 오류 등 예기치 않은 동작이 발생할 수 있음.
ex) A라이브러리는 v1.0의 Library X를 요구하고 B라이브러리는 v2.0의 Library X를 요구하는 경우
ex) Spring Boot 프로젝트에서 spring-boot-starter-web을 추가하면 내부적으로 spring-web과 spring-core 같은 라이브러리가 포함됨.
spring-boot-starter-web을 추가했더니 spring-web 5.3.0을 필요로 하고 spring-core 5.3.0도 함께 필요함.
그런데 추가적으로 다른 라이브러리를 사용했는데 spring-core 5.2.0을 필요로 한다면 어떤 버전을 쓸 것인지 충돌 발생
"Dependency conflict" 또는 "Duplicate class" 같은 메시지
ClassNotFoundException, NoSuchMethodError 등
잘못된 버전의 라이브러리가 로드되어 기능이 올바르게 동작하지 않음
./gradlew dependencies
명령을 통해 의존성 트리를 시각적으로 보여줌configurations.all {
resolutionStrategy {
force 'com.example:library:1.2.3'
}
}
implementation('com.example:library') {
exclude group: 'com.other', module: 'conflicting-library'
}
implementation platform('org.springframework.boot:spring-boot-dependencies:2.5.0')
mvn dependency:tree
명령으로 트리를 확인하고 충돌 원인을 파악<dependencyManagement>
태그를 사용하여 버전을 명시적으로 고정<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>library</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>com.other</groupId>
<artifactId>conflicting-library</artifactId>
</exclusion>
</exclusions>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>enforce-dependency-convergence</id>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>