我們在開發中難免會遇到多個文件相互依賴的情況,使用Maven來進行項目的依賴管理,我們需要進行以下幾步操作:
依賴聲明
比如,我們有項目Restaurant,其中有一個方法依賴于另一個項目Kitchen中的一個方法,那么,我們需要
import com.netease.Kitchen;
注意,這里的地址寫的是Kitchen包地址+文件名的形式。在物理目錄下它們并不在一個目錄里。
Maven配置
這樣,我們的代碼在Eclipse里就不會報錯了,但是想要讓服務器能夠正確的區分依賴關系,需要對pom.xml進行配置。
首先,我們需要一個父層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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.netease.restaurant</groupId>//和管理的項目相同
<artifactId>restaurant-parent</artifactId>//加上parent
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>//類型為pom
<name> Multi modules demo </name>//名字任意
//聲明管理的模塊
<modules>
<module>Restaurant</module>
<module>Kitchen</module>
</modules>
</project>
然后,我們需要分別配置被管理的兩個子模塊
Kitchen:
//這個標簽在<project>標簽下
<parent>
<groupId>com.netease.restaurant</groupId>
<artifactId>restaurant-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
Restaurant:
//聲明管理者
<parent>
<groupId>com.netease.restaurant</groupId>
<artifactId>restaurant-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
//聲明依賴的對象
<dependency>
<groupId>com.netease.restaurant</groupId>
<artifactId>Kitchen</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
然后,使用
mvn install
命令來判斷是否聲明正確,如果正確的話,便可以進入Restaurant來運行了。