How to exclude a dependency from parent's project in Maven?

前端 未结 6 2211
孤独总比滥情好
孤独总比滥情好 2020-12-24 12:11

For example, I have 2 Maven projects. One is \"project-parent\". The other is \"project-child\". Obviously, \"project-child\" is the sub project of \"project-parent\".

6条回答
  •  春和景丽
    2020-12-24 12:34

    I have met the same question just like you. In my project, let call the parent pom is parent.pom. parent defined the log4j, slf4j like this:

           
                org.slf4j
                slf4j-api
                ${slf4j-api.version}
            
            
                log4j
                log4j
                ${log4j.version}
            
            
                org.slf4j
                jcl-over-slf4j
                ${slf4j-api.version}
            
    
            
                org.slf4j
                slf4j-log4j12
                ${slf4j-log4j12.version}
            
    

    child project invoke some dependency in child.pom. But I don't want the log4j-1.2.x dependency and want to increase the version of slf4j.

    So. I add the dependency of parent

    
            parent
            myartifactId
            ${my parent version}
    
    

    and use exclusions to remove the log4j

    
            parent
            myartifactId
            ${my parent version}
            
                
                    log4j
                    log4j
                
            
    
    

    and explicitly add the slf4j and log4j2's dependency in child pom

     
            org.slf4j
            slf4j-api
            1.7.6
        
        
            org.apache.logging.log4j
            log4j-slf4j-impl
            2.8.2
        
        
            org.apache.logging.log4j
            log4j-api
            2.8.2
        
    
        
            org.apache.logging.log4j
            log4j-core
            2.8.2
        
    
        
            com.lmax
            disruptor
            3.3.4
        
    

    then use mvn dependency:tree to show the dependency list, still see the log4j

    [INFO] +- org.apache.kafka:kafka_2.10:jar:0.8.2.0:compile
    [INFO] |  +- com.yammer.metrics:metrics-core:jar:2.2.0:compile
    [INFO] |  +- org.scala-lang:scala-library:jar:2.10.4:compile
    [INFO] |  +- org.apache.zookeeper:zookeeper:jar:3.4.6:compile
    [INFO] |  |  +- org.slf4j:slf4j-log4j12:jar:1.7.5:compile
    [INFO] |  |  +- log4j:log4j:jar:1.2.17:compile
    

    well, let's add the exclusions on that dependency...remove this guy.

        
            org.apache.kafka
            kafka-clients
            0.10.1.1
            
                
                    log4j
                    log4j
                
            
        
    

    then run the command again to check the dependency list. OK! clear~

    Hope that can help you :>

提交回复
热议问题