關於maven多module的依賴問題


之前的項目因為歷史的原因,都是一個project里只包含了一個module,今年進入了新的項目組,出現了多個module,最近剛好也是在學《maven實戰》因此想要將這個東西記錄下來

工程情況如下圖

 

示例的工程名為inaction

其中位於根目錄的pom.xml內的代碼如下

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.example</groupId>
 8     <artifactId>inaction</artifactId>
 9     <packaging>pom</packaging>
10     <version>1.0-SNAPSHOT</version>
11 
12     <modules>
13         <module>inactionuser</module>
14         <module>inactioncore</module>
15         <module>inactionparent</module>
16     </modules>
17 
18 </project>

 

 

因為是多module,所以必然是有自己編寫的類或接口,是可以放在公共的module里的這里我把它命名為 inaction-core

然后有一個inaction-user表示用戶模塊的

一般的,如果我們需要將本地一些代碼抽取到公共模塊,那么還需要一個module來進行管理,在我的項目中,這個為 inaction-parent

 

記得要設置<realtivePath>../pom.xml</realtivePath>

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5 
 6     <modelVersion>4.0.0</modelVersion>
 7 
 8     <groupId>com.example</groupId>
 9     <artifactId>inaction-parent</artifactId>
10     <version>1.0-SNAPSHOT</version>
11 
12     <!-- dependmanager不會向子類真的導入依賴,只是起到一個約束的作用 -->
13     <dependencyManagement>
14         <dependencies>
15             <!-- 這里聲明本地公共的module -->
16             <dependency>
17                 <groupId>com.example</groupId>
18                 <artifactId>inaction-core</artifactId>
19                 <version>1.0-SNAPSHOT</version>
20             </dependency>
21         </dependencies>
22     </dependencyManagement>
23 
24 </project>

 

再看core和user 兩個module

(ps 在paren頭結點下,記得補充 <relativePath>../inactionparent/pom.xml</relativePath>)

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <groupId>com.example</groupId>
        <artifactId>inaction-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>inaction-user</artifactId>
    <packaging>jar</packaging>


    <dependencies>
        <!--注意到這里我們已經不需要聲明版本了,因為在parent那邊已經對版本進行了管理-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>inaction-core</artifactId>
        </dependency>
    </dependencies>
</project>

 

core模塊就很簡單,只需要聲明其父module即可

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <groupId>com.example</groupId>
        <artifactId>inaction-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <artifactId>inaction-core</artifactId>


</project>

 

然后,在user模塊中去引用core的類就不會有編譯問題了

 

如果多module沒有引用本地的module,那么可以不需要parent,直接聲明到根目錄下

 

注意,你的root,parent和公共的module,一定不要有 maven plugin,尤其是公共的module,會導致在 mvn clean install 的時候,報找不到類的錯誤

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM