Java_maven構建項目(多模組項目)

來源:互聯網
上載者:User

標籤:

在eclipse下構建maven項目,該項目由多個子模組組成。

1.建立一個父項目

NEW -->project-->maven-->maven Project,點擊下一步,進入new maven Project的Select project name and location介面

,什麼也不做,直接下一步到Select an Archetype介面。

在這個介面中選擇maven-Archetype-site-simple,然後選擇下一步,進入選擇Enter a group id for the artifact的介面

,在group id 和artifact id中輸入你的group和artifact名稱。我用的分別是A和B,選擇完成。這時會在eclipse中產生一個項目,項目名是B。

此時B的pom檔案如下所示:

[html] view plaincopyprint?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   
  5.   <groupId>A</groupId>  
  6.   <artifactId>B</artifactId>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <packaging>pom</packaging>  
  9.   
  10.   <distributionManagement>  
  11.     <site>  
  12.       <id>website</id>  
  13.       <url>scp://webhost.company.com/www/website</url>  
  14.     </site>  
  15.   </distributionManagement>  
  16.   
  17.   <properties>  
  18.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  19.   </properties>  
  20. </project>  
<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>A</groupId>  <artifactId>B</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>pom</packaging>  <distributionManagement>    <site>      <id>website</id>      <url>scp://webhost.company.com/www/website</url>    </site>  </distributionManagement>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties></project>

 

2.建立子項目

2.1 將項目B中的src檔案刪除(可有可無,主要看個人需要) 2.2 選中項目B,點擊右鍵,選擇NEW -->project-->maven-->maven Module,點擊下一步,在出現的介面中輸入子模組的名稱C,點擊下一步,出現Select an Archetype介面。這時選擇maven-Archetype-site-quickStart或者maven-Archetype-webapp(構建web層時使用),然後選擇完成,即產生子項目C。

這時B的pom檔案就變成了這樣(和上面的比只是多了個modules標籤):

[html] view plaincopyprint?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   
  5.   <groupId>A</groupId>  
  6.   <artifactId>B</artifactId>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <packaging>pom</packaging>  
  9.   
  10.   <distributionManagement>  
  11.     <site>  
  12.       <id>website</id>  
  13.       <url>scp://webhost.company.com/www/website</url>  
  14.     </site>  
  15.   </distributionManagement>  
  16.   
  17.   <properties>  
  18.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  19.   </properties>  
  20.   <modules>  
  21.     <module>C</module>  
  22.   </modules>  
  23. </project>  
<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>A</groupId>  <artifactId>B</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>pom</packaging>  <distributionManagement>    <site>      <id>website</id>      <url>scp://webhost.company.com/www/website</url>    </site>  </distributionManagement>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <modules>  <module>C</module>  </modules></project>

 

這時構建B的子項目完成,構建其他的子項目和此類似。

在構建第二個子項目D時可能在B中沒有及時出現,這時只要把B項目重新整理下就可以了。

ps:

其實在構建多模組的項目時,在選擇父項目的類型時是可以選擇任意的類型。選擇項目類型的介面如下:

對於這些非常規類型的項目,如果想要成為父項目,需要做如下一些改動:

1.將pom.xml檔案中的<packaging>jar</packaging>改為<packaging>pom</packaging>,若不換為pom則在打包的時候父項目會產生一個target檔案;如果沒有<packaging>pom</packaging>的可以在<version>0.0.1-SNAPSHOT</version>後面添加上這句話即可。

2.刪除除pom.xml外的所有檔案,其中JRE System Library是不能直接刪除的,需要選擇中,並單擊右鍵Bulid Path-->Remove from bilud path即可移除。

這時的父項目就顯得很乾淨整潔了。

 

其實在普通的項目上是不允許構建子模組的,能否在一個項目下建立子模組,主要取決於是否有<packaging>pom</packaging>這個配置。如果有這個配置則任何項目都可以建立子模組,硬要這樣建立的話整個項目會變的不倫不類。看來maven在判斷一個項目是否可以建立子模組其實是取決於這個配置的。

Java_maven構建項目(多模組項目)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.