新建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不見了。