Xung đột phụ thuộc Maven và cách giải quyết

Giới thiệu về xung đột phụ thuộc

Xung đột phụ thuộc xảy ra khi một dự án sử dụng nhiều phiên bản khác nhau của cùng một thư viện, dẫn đến xung đột giữa các phiên bản.

Lý do gây ra xung đột phụ thuộc

Xung đột phụ thuộc thường do sự phụ thuộc gián tiếp giữa các thư viện. Mỗi thư viện được khai báo trực tiếp có thể phụ thuộc vào các thư viện khác, và những thư viện này có thể được Maven tự động thêm vào, tạo ra xung đột.

Cách giải quyết xung đột phụ thuộc

Để giải quyết xung đột, trước tiên cần xác định các thư viện gây xung đột, sau đó loại bỏ phiên bản không mong muốn. Dưới đây là các bước chi tiết:

1. Xem xung đột phụ thuộc

a. Sử dụng lệnh dependency:tree để kiểm tra xung đột

mvn -Dverbose dependency:tree

Kết quả hiển thị trên console sẽ tương tự như sau:

[INFO] org.example:hello:jar:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-context:jar:5.2.7.RELEASE:compile
[INFO] |  +- (org.springframework:spring-aop:jar:5.2.7.RELEASE:compile - omitted for conflict with 5.2.0.RELEASE)
[INFO] |  +- org.springframework:spring-beans:jar:5.2.7.RELEASE:compile
[INFO] |  |  \- (org.springframework:spring-core:jar:5.2.7.RELEASE:compile - omitted for duplicate)
[INFO] |  +- org.springframework:spring-core:jar:5.2.7.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.2.7.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:5.2.7.RELEASE:compile
[INFO] |     \- (org.springframework:spring-core:jar:5.2.7.RELEASE:compile - omitted for duplicate)
[INFO] \- org.springframework:spring-aop:jar:5.2.0.RELEASE:compile
[INFO]    +- (org.springframework:spring-beans:jar:5.2.0.RELEASE:compile - omitted for conflict with 5.2.7.RELEASE)
[INFO]    \- (org.springframework:spring-core:jar:5.2.0.RELEASE:compile - omitted for conflict with 5.2.7.RELEASE)

Trong đó, "omitted for duplicate" cho thấy có thư viện bị trùng lặp, và "omitted for conflict with xxx" cho thấy xung đột với phiên bản khác. Ví dụ, dòng cuối cùng cho thấy spring-core 5.2.0.RELEASE bị bỏ qua vì xung đột với 5.2.7.RELEASE.

b. Sử dụng plugin Maven Helper trong IntelliJ IDEA

Sau khi cài đặt plugin Maven Helper, mở file pom.xml, bạn sẽ thấy view Dependency Analyzer. Các nút chức năng bao gồm:

  • Conflicts (Xem xung đột)
  • All Dependencies as List (Danh sách tất cả phụ thuộc)
  • All Dependencies as Tree (Cây phụ thuộc)

Nếu có xung đột, click vào thư viện xung đột để xem chi tiết.

2. Giải quyết xung đột

Ví dụ về cấu trúc file pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>
</dependencies>

a. Nguyên tắc ưu tiên người khai báo đầu tiên

Maven sẽ sử dụng phiên bản của thư viện được khai báo đầu tiên. Để sử dụng spring-core 5.2.0.RELEASE, thay đổi thứ tự khai báo:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.7.RELEASE</version>
    </dependency>
</dependencies>

b. Nguyên tắc ưu tiên đường dẫn gần nhất

Thêm trực tiếp phụ thuộc vào pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>
</dependencies>

c. Loại bỏ phụ thuộc

Loại bỏ phụ thuộc không mong muốn bằng cách sử dụng plugin Maven Helper hoặc chỉnh sửa trực tiếp trong pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.7.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>
</dependencies>

4. Khóa phiên bản

Sử dụng dependencyManagement để quản lý phiên bản, đảm bảo các dự án con sử dụng phiên bản thống nhất:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>
</dependencies>

Tóm tắt

Bài viết đã trình bày cách kiểm tra và giải quyết xung đột phụ thuộc trong Maven. Cách tốt nhất để kiểm tra xung đột là sử dụng plugin Maven Helper, và cách giải quyết hiệu quả là sử dụng dependencyManagement để khóa phiên bản.

Thẻ: Maven Spring dependency-management maven-helper dependency-tree

Đăng vào ngày 29 tháng 6 lúc 14:40