使用 force 强制指定版本
Gradle
1 2 3
| compile("group:name:version") { force = true }
|
使用 exclude 排除依赖
Gradle
1 2 3
| compile("group:name:version") { exclude(group: 'com.google.xxx', module: 'zzz') }
|
Maven
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>{group}</groupId> <artifactId>{name}</artifactId> <version>{version}</version> <exclusions> <exclusion> <groupId>{group_to_exclude}</groupId> <artifactId>{name_to_exclude}</artifactId> </exclusion> </exclusions> </dependency>
|
使用shade
shade 用于一个库需要同时存在的情况
比如,A库依赖X库的1.0版本,B项目需要使用A库,然而B项目还需要使用X库的2.0版本,与X的1.0版本不兼容,解决方案是,在A打包时将X库的1.0版本作为shared一起打包(如果X库的路径是com.google.xxx,shared之后是com.you.shared.com.google.xxx),如果这样打包,B项目可以同时使用X库的1.0版本(com.you.shared.com.google.xxx)和2.0版本(com.google.xxx)。
maven (maven-shade-plugin)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>shade-xxx</id> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <shadeTestJar>false</shadeTestJar> <artifactSet> <includes> <include>*:*</include> </includes> </artifactSet> <relocations> <relocation> <pattern>com.google.xxx</pattern> <shadedPattern>com.you.shared.com.google.xxx</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin>
|
插件文档:https://imperceptiblethoughts.com/shadow/introduction/
https://www.zybuluo.com/xtccc/note/317832
1 2 3 4 5 6 7 8 9 10 11 12
| shadowJar { baseName = 'prophet-importer-app' classifier = null version = null zip64 true
manifest { attributes 'Main-Class': 'com.you.Main' }
relocate 'com.google.xxx', 'com.you.shared.com.google.xxx' }
|