美文网首页
maven引入jar包冲突问题

maven引入jar包冲突问题

作者: 死拍照的 | 来源:发表于2017-04-19 14:06 被阅读1791次

    最近做的个项目要在原工程里引入个二方hqueue的jar包,包里用了hbase的包又引用了Guava11版本,但是原工程里又已经在用Guava18,Guava的11和18版本的API更改很大,比如StopWatch的创建和时间获取,看看这个运行时异常,就是由于加载了高版本Stopwatch类引起的

    org.apache.hadoop.hbase.DoNotRetryIOException: java.lang.IllegalAccessError: tried to access method com.google.common.base.Stopwatch.<init>()V from class org.apache.hadoop.hbase.zookeeper.MetaTableLocator at org.apache.hadoop.hbase.client... 
    

    类冲突检测方法

    1. tomcat启动参数中加入-verbose:class,启动日志里可以看到没有load的jar包
    2. 执行maven命令检查Jar包版本冲突:mvn -X compile dependency:tree -Dverbose | grep 'omitted for conflict with'
    3. IntelliJ IDEA查找依赖关系,解决jar包冲突 这种方式好像检查不出所有的依赖
    4. 加入maven enforce插件:首先在你的项目中需要添加maven-enforcer-plugin插件,然后在插件中定义自己的rule规则,例如如下配置,再执行maven命令 mvn enforcer:enforce
    <properties>
         <maven-enforcer-plugin_version>1.1</maven-enforcer-plugin_version>
         <extra-enforcer-rules_version>1.0-alpha-3</extra-enforcer-rules_version>
    </properties>
    <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-enforcer-plugin</artifactId>
                        <version>1.1.1</version>
                        <dependencies>
                            <dependency>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>extra-enforcer-rules</artifactId>
                                <version>${extra-enforcer-rules_version}</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                </plugins>
            </pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-enforcer-plugin</artifactId>
                    <configuration>
                        <rules>
                            <bannedDependencies>
                                <searchTransitive>true</searchTransitive>
                                <excludes>
                                    <exclude>commons-logging</exclude>
                                    <exclude>org.slf4j:*:(,1.6)</exclude>
                                </excludes>
                            </bannedDependencies>
                            <evaluateBeanshell>
                                <condition>print("[INFO] [Alibaba Enforcer Rules] parent-pom ");1==1</condition>
                            </evaluateBeanshell>
                            <banDuplicateClasses>
                                <findAllDuplicates>true</findAllDuplicates>
                                <ignoreClasses>
                                    <ignoreClass>junit.*</ignoreClass>
                                    <ignoreClass>org.junit.*</ignoreClass>
                                    <ignoreClass>org.w3c.dom.*</ignoreClass>
                                    <ignoreClass>javax.xml.namespace.*</ignoreClass>
                                    <ignoreClass>org.apache.axis2.*</ignoreClass>
                                </ignoreClasses>
                                <message>[ERROR] [Alibaba Enforcer Rules] find DuplicateClasses</message>
                            </banDuplicateClasses>
                        </rules>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    解决方案

    1. 同个jar包不同版本冲突并且向下兼容,则在引入低版本的包中排除依赖
    <exclusions>
        <exclusion>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        </exclusion>
    </exclusions>
    
    1. 如果两个版本不兼容,可以用shade插件重新打成一个新的jar包,替换掉不兼容的类
      插件配置参数:http://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html
      pom说明
        <dependencies>
            <dependency>
                <groupId>com.x.hbase</groupId>
                <artifactId>hqueue-client</artifactId>
                <version>0.0.1</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                </plugin>
            </plugins>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>3.0.0</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>
                                </goals>
                                <configuration>
                                    <!-- 如果为true则有一个普通jar包和一个-shade.jar包,如果为false合包去掉原来包的依赖关系,shade过的jar作为项目默认的包-->  
                                    <shadedArtifactAttached>false</shadedArtifactAttached>  
                                    <!-- When true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed. -->  
                                    <keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>  
                                    <!-- When true, transitive deps of removed dependencies are promoted to direct dependencies. This should allow the drop in replacement of the removed deps with the new shaded jar and everything should still work. -->  
                                    <promoteTransitiveDependencies>true</promoteTransitiveDependencies>  
                                    <!--jar包的pom.xml里去掉了dependency(META-INF/maven/$groupId/$artifactId/pom.xml),并创建了一个dependency-reduced-pom.xml在根目录-->  
                                    <createDependencyReducedPom>true</createDependencyReducedPom>  
                                    <!-- 生成source包,如果依赖的包没有source会报错-->  
                                    <createSourcesJar>false</createSourcesJar>  
                                    <relocations>
                                        <relocation>
                                            <pattern>com.google.common</pattern>
                                            <shadedPattern>shaded.com.google.common</shadedPattern>
                                        </relocation>
                                    </relocations>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>
    

    相关文章

      网友评论

          本文标题:maven引入jar包冲突问题

          本文链接:https://www.haomeiwen.com/subject/iogczttx.html