use-mvn-to-resolve-dependency-conflicts

maven 依赖处理

  • 用于依赖冲突
  • 公司本身对某个jar做了特殊处理.例如修改了spring的源码.需要特殊指定.

使用的项目 : Building an Application with Spring Boot

指定依赖版本

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- 排除依赖spring-core -->
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- specify spring-core version -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.1.0.RELEASE</version>
</dependency>

打包的时候排除依赖jar

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>

    <!-- 打包的时候排除依赖spring-context -->
    <configuration>
        <excludes>
            <exclude>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclude>
        </excludes>
    </configuration>
</plugin>

修改后的pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!-- 排除依赖spring-core -->
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- specify spring-core version -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- 打包的时候排除依赖spring-context -->
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-context</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

利用插件maven-dependency-plugin拷贝依赖

注意不要和打包插件spring-boot-maven-plugin一起用,因为后者打包的时候本身就对依赖的jar做了拷贝,放在最终生成的jar中.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>

使用本地依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.0.11.MULANG</version>
    <scope>system</scope>
    <systemPath>${basedir}/encrypt-jar/spring-core-5.0.11.MULANG.jar</systemPath>
</dependency>
<!-- 由于使用了本地依赖 spring-core的传递依赖不生效, 所以需要单独引入 -->
<!-- spring-core 5 依赖于spring-jcl. -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jcl</artifactId>
</dependency>

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
        <goals>
            <goal>repackage</goal>
        </goals>
        <configuration>
            <!-- 打包时候包含本地依赖,默认是关闭的 -->
            <includeSystemScope>true</includeSystemScope>
        </configuration>
        </execution>
    </executions>
</plugin>

重新下载依赖

有时候网络不好, 下载的依赖有问题.这时可以指定参数dependency:purge-local-repository重新下载项目需要的依赖.

mvn dependency:purge-local-repository  clean  package -DskipTests

REFERENCE

使用的项目 : Building an Application with Spring Boot
打包插件 : Spring Boot Maven Plugin

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.