maven依赖某工程确定的版本

来源:互联网 发布:tcl 惠州酷友网络 编辑:程序博客网 时间:2024/05/29 16:33

背景

maven项目中,可能存在依赖的多个工程中同时依赖某个工程,而该工程被依赖的版本却不一样。比如工程demo依赖工程A,工程B。工程A和工程B依赖的thrift的版本如下

A <-- org.apache.thrift:thrift:[0.6.0]B <-- org.apache.thrift:thrift:[0.7.0]

demo工程运行的时候,加载的thrift版本是不确定的,这可能导致程序运行过程中出错。这就导致了这样一个需求:demo工程依赖确定版本的thrift

过程

让demo工程依赖org.apache.thrift:thrift:[0.5.0]
step1 在demo工程的pom.xml中写依赖时,从A和B中将thrift exclusion出来
step2 在demo工程的pom.xml中加入对org.apache.thrift:thrift:[0.5.0]的依赖
step3 加入插件:让demo工程强制依赖org.apache.thrift:thrift:[0.5.0]

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-enforcer-plugin</artifactId>    <version>1.4.1</version>    <executions>        <execution>            <id>enforce-banned-dependencies</id>            <goals>                <goal>enforce</goal>            </goals>            <configuration>                <rules>                    <bannedDependencies>                        <excludes>                            <exclude>org.apache.thrift:thrift</exclude>                        </excludes>                        <includes>                            <include>org.apache.thrift:thrift:[0.5.0]</include>                        </includes>                    </bannedDependencies>                </rules>                <fail>true</fail>            </configuration>        </execution>    </executions></plugin>

参考

Banned Dependencies

0 0