Implementation in Maven: exclusion of dependencies

Exclusion of dependencies

Article directory

  • dependent exclusion
    • 1. Concept
    • 2. Configuration method
    • 3. Test
    • 4. Finally: thanks

1. Concept

When A depends on B, B depends on C and C can be passed to A, A does not want C, and needs to exclude C from A. And often this is to avoid conflicts between jar packages.

So the exclusion of configuration dependencies is actually to prevent the delivery of certain jar packages. Because such a jar package will conflict with other jar packages.

Background: We passed the commons-logging which is dependent on pro01-maven-java in spring-core in pro02-mavne-web. As shown below:

The following is: pom.xml configuration of pro1-maven-java:

<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>com.rainbowSea.maven</groupId>
  <artifactId>pro01-maven-java</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>pro01-maven-java</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <!--Test test cannot pass dependencies -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.0.0.RELEASE</version>
</dependency>


<!--Test provided cannot pass dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
     <version>3.1.0</version>
<scope>provided</scope>
</dependency>
  </dependencies>
</project>

2. Configuration method

For the pom.xml configuration of pro02-maven-web, remove: pro01-maven-java depends on the passed commons-logging jar package

Note: it is configured under the label you import pro01-maven-java, not in other locations, because you want to exclude the dependency transfer that exists in pro01-maven-java, so you need to configure it in pro01-mavne- Exclude dependencies configured under the java tag.

<!--The import is the dependency on the packaged jar in pro01-maven-java-->
<dependency>
<!--Complete dependency by specifying the coordinates of the dependent project -->
<groupId>com.rainbowSea.maven</groupId>
      <artifactId>pro01-maven-java</artifactId>
      <version>1.0-SNAPSHOT</version>
<!--The scope of the life cycle-->
<scope>compile</scope>
      
      
       <!-- Use the excludes tag to configure dependent exclusions -->
     <exclusions>
       <!-- Configure a specific exclusion in the exclude tag -->
       <exclusion>
        <!-- Specify the coordinates of the dependencies to be excluded (no need to write version) only need to write: groupId and artifatId, -->
<!--What I want to exclude here is the dependency transfer that exists in pro02-maven-web: commons-logging-->
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
       </exclusion>
     </exclusions>
</dependency>

The complete information of pom.xml of pro02-maven-web:




  4.0.0

  com.rainbowSea.maven
  pro02-maven-web
  1.0-SNAPSHOT
  war

  pro02-maven-web Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    



      javax.servlet
      javax.servlet-api
      3.1.0
      provided


<!--The import is the dependency on the packaged jar in pro01-maven-java-->
<dependency>
<!--Complete dependency by specifying the coordinates of the dependent project -->
<groupId>com.rainbowSea.maven</groupId>
      <artifactId>pro01-maven-java</artifactId>
      <version>1.0-SNAPSHOT</version>
<!--The scope of the life cycle-->
<scope>compile</scope>
      
      
       <!-- Use the excludes tag to configure dependent exclusions -->
     <exclusions>
       <!-- Configure a specific exclusion in the exclude tag -->
       <exclusion>
        <!-- Specify the coordinates of the dependencies to be excluded (no need to write version) only need to write: groupId and artifatId, -->
<!--What I want to exclude here is the dependency transfer that exists in pro02-maven-web: commons-logging-->
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
       </exclusion>
     </exclusions>
</dependency>
  

  
    pro02-maven-web
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  


3. Test

Test method: configure the exclusion of commons-logging in the pro02-maven-web project

<dependency>
  <groupId>com.rainbowSea.maven</groupId>
  <artifactId>pro01-maven-java</artifactId>
  <version>1.0-SNAPSHOT</version>
  <scope>compile</scope>
  <!-- Use the excludes tag to configure dependent exclusions -->
  <exclusions>
    <!-- Configure a specific exclusion in the exclude tag -->
    <exclusion>
      <!-- Specify the coordinates of the dependencies to be excluded (no need to write version) -->
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
    </exclusion>
  </exclusions>
</dependency>

Run the mvn dependency:tree command to see the effect:

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ pro02-maven-web ---
[INFO] com.rainbowSea.maven:pro02-maven-web:war:1.0-SNAPSHOT
[INFO] + - junit:junit:jar:4.11:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] + - javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] \- com.rainbowSea.maven:pro01-maven-java:jar:1.0-SNAPSHOT:compile
[INFO] \- org.springframework:spring-core:jar:4.0.0.RELEASE:compile
[INFO] ----------------------------------------------- -------------------------

It is found that there is no commons-logging under spring-core.

We are checking: the dependencies in pro01-maven-java have no impact and are still the same.

4. Finally: Thanks

This article refers to the following blogger’s sharing. Here again, we sincerely thank the bloggers for their enthusiastic sharing of their technology.

Thanks to the following bloggers for sharing

[1]: Weapon | Code Rework