Maven依赖传递

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_35255384/article/details/78857619

新建mavenA项目,pom.xml配置如下:

<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.cn.maven1</groupId>
  <artifactId>A</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>A</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
选中pom.xml,右键run as--->maven build,goal输入:install进行打包并发布到本地仓库,供调用发使用
------------------

新建mavenB项目,B项目依赖A项目,pom.xml配置如下:

<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.cn.maven1</groupId>
  <artifactId>B</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>B</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- 添加A的坐标,表示对A的依赖 -->
    <dependency>
        <groupId>com.cn.maven1</groupId>
          <artifactId>A</artifactId>
          <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>
选中pom.xml,右键run as--->maven build,goal输入:install进行打包并发布到本地仓库,供调用发使用
---------------------

新建mavenC项目,依赖B项目,pom.xml配置如下:

<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.cn.maven1</groupId>
  <artifactId>C</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>C</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
         <groupId>com.cn.maven1</groupId>
          <artifactId>B</artifactId>
          <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>
选中pom.xml,右键run as--->maven build,goal输入:compile进行编译,会在Maven dependencies下看到A,B项目,这是因为maven具有依赖传递性。
如果想让C只依赖B而不依赖A,则C的pom.xml这样写:

<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.cn.maven1</groupId>
  <artifactId>C</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>C</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
         <groupId>com.cn.maven1</groupId>
          <artifactId>B</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <!-- 加入排除标签 exclusions,排除A的坐标-->
          <exclusions>
              <exclusion>
                  <groupId>com.cn.maven1</groupId>
                  <artifactId>A</artifactId>
              </exclusion>
          </exclusions>
    </dependency>
  </dependencies>
</project>

此时Maven dependencies下会发现A不见了。




展开阅读全文

没有更多推荐了,返回首页