1. 程式人生 > >(轉)在Eclipse中創建Maven多模塊工程

(轉)在Eclipse中創建Maven多模塊工程

http term water 表現 優化配置 -i sco 文件 man

背景:以前只總結了怎麽在命令行下創建maven的多模塊項目,在eclipse下怎麽創建不是很清楚。最近需要在git的資源庫中上傳多模塊項目,方便後期的維護,所以將網上的資料進行整理。

原文鏈接:http://blog.csdn.net/leipeng321123/article/details/50995736

模塊結構

在平時的Javaweb項目開發中為了便於後期的維護,我們一般會進行分層開發,最常見的就是分為domain(域模型層)、dao(數據庫訪問層)、service(業務邏輯層)、web(表現層),這樣分層之後,各個層之間的職責會比較明確,後期維護起來也相對比較容易,今天我們就是使用Maven來構建分模塊項目。

在這個分模塊項目中主要分為兩個模塊,Service模塊和web模塊,Service模塊中包含dao,下面是項目結構

Complaints-parent
    |----pom.xml(pom)
    |----Complaints-web
        |----pom.xml (war)
    |----Complaints-service
        |----pom.xml(jar)

Complaints-parent是父工程,同時承擔聚合模塊和父模塊的作用,沒有實際代碼和資源文件;

Complaints-web是web工程,最終形成最終的war包;

Complaints-service是業務模塊的邏輯部分,包含了數據庫訪問層和業務邏輯層,但是不包括web相關的部分;

上述簡單示意圖中,有一個父項目(Complaints-parent)聚合很多子項目(Complaints-web,Complaints-service)。每個項目,不管是父子,都含有一個pom.xml文件。而且要註意的是,小括號中標出了每個項目的打包類型。父項目是pom,也只能是pom。子項目有jar,或者war。

依賴關系是:Complaints-web需要依賴Complaints-service

接下來就是在Eclipse中搭建項目了:

1.父項目(Complaints-parent)

在Eclipse裏面New -> Maven Project

在彈出界面中選擇“Create a simple project”

技術分享圖片

技術分享圖片

父項目建好之後,目錄下面只有一個src和pom.xml文件

將src文件夾刪除然後修改pom.xml文件,將<packaging>jar</packaging>修改為<packaging>pom</packaging>

pom表示它是一個被繼承的模塊。(如果再創建項目時已經選為了pom就不需要修改)

以下是父項目的pom.xml的內容

<?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/maven-v4_0_0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.hrtel</groupId>  
  <artifactId>Complaints-parent</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <packaging>pom</packaging>  
  <name>Complaints-parent</name>  
  <modules>  
    <module>Complaints-service</module>  
    <module>Complaints-web</module>  
  </modules>  
    
  <dependencyManagement>  
    <dependencies>  
        <dependency>  
          <groupId>junit</groupId>  
          <artifactId>junit</artifactId>  
          <version>4.12</version>  
          <scope>test</scope>  
        </dependency>  
    </dependencies>  
  </dependencyManagement>  
</project>  

2.創建Complaints-service項目

選中剛建的父項目,在彈出菜單中點擊 New -> Maven Module;

技術分享圖片

使用默認的Archetype(默認:GroupId:org.apache.maven.archetypes,Artifact Id:maven-archetype-quickstart)

這樣一個子項目就創建完成了,在文件系統中,子項目會建在父項目的目錄中。

細心一點會發現,此時父項目的pom.xml文件中就多了一句

<modules>  
    <module>Complaints-service</module>  
  </modules>  

3.創建web工程Complaints-web

選中剛建的父項目,在彈出菜單中點擊 New -> Maven Module;

技術分享圖片

技術分享圖片

創建完之後就會發現父項目的pom.xml文件中又自動多了一句

<modules>  
    <module>Complaints-service</module>  
    <module>Complaints-web</module>  
  </modules>  

4.優化配置

Complaints-web是依賴service項目的

需要在web的pom.xml中添加依賴配置

<dependency>    
        <groupId>com.hrtel</groupId>    
        <artifactId>Complaints-service</artifactId>    
        <version>${project.version}</version>    
    </dependency>

按上面步驟創建的子項目,在pom.xml中有個parent節點,所以,他可以繼承父項目的相關信息。沒錯,父子項目中存在繼承關系。

在子項目的pom.xml中,子項目的groupIdversion一般和父項目相同,那麽可以把子項目的這兩個參數刪除,這樣會自動繼承父項目的取值

同樣,如果其他的一些屬性,所有子項目都是一樣的,那麽可以上移到父項目中設置,子項目中無需重復設置。比如:<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>可以僅在父項目中設置一次。

Manen提供dependencyManagementpluginManagement兩個標簽。使用這兩個標簽,可以在父項目中統一管理依賴和插件的配置參數,比如版本號啥的。而在子項目中,僅需列出需要使用的依賴和插件的groupIdartifactId就可以了,其他信息會自動從父項目管理的信息裏面獲取。

在父項目中:

<dependencyManagement>  
  <dependencies>  
    <dependency>  
        <groupId>junit</groupId>  
        <artifactId>junit</artifactId>  
        <version>${junit.version}</version>  
        <scope>test</scope>  
    </dependency>  
    <dependency>  
        <groupId>org.slf4j</groupId>  
        <artifactId>slf4j-log4j12</artifactId>  
        <version>1.7.5</version>  
        <scope>test</scope>  
    </dependency>  
    <dependency>  
        <groupId>org.slf4j</groupId>  
        <artifactId>slf4j-api</artifactId>  
        <version>1.7.5</version>  
    </dependency>     
  </dependencies></dependencyManagement> 

在子項目中

<dependencies>  
  <dependency>  
    <groupId>junit</groupId>  
    <artifactId>junit</artifactId>  
  </dependency>  
</dependencies>

由於web項目依賴service項目,子項目中只需service中添加配置,web項目會自動加載過去的

最後是打包配置:

打包時需要把service項目中的xml文件和別的property文件也打進去,就得在pom文件中說明

<!-- 打包時把需要的xml文件一塊打進去 -->  
  <build>    
        <finalName>Complaints-service</finalName>    
        <plugins>    
            <plugin>    
                <groupId>org.apache.maven.plugins</groupId>    
                <artifactId>maven-jar-plugin</artifactId>    
            </plugin>    
        </plugins>    
        <resources>    
            <resource>    
                <directory>src/main/java</directory>    
                <includes>    
                    <include>**/*.xml</include>    
                </includes>    
            </resource>    
            <resource>    
                <directory>src/main/resources</directory>    
                <includes>    
                    <include>**/*.xml</include>    
                    <include>**/*.properties</include>    
                </includes>    
            </resource>    
        </resources>    
    </build>  

以下是三個項目的pom.xml中的全部內容

Complaints-parent

<?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/maven-v4_0_0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.hrtel</groupId>  
  <artifactId>Complaints-parent</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <packaging>pom</packaging>  
  <name>Complaints-parent</name>  
  <modules>  
    <module>Complaints-service</module>  
    <module>Complaints-web</module>  
  </modules>  
    
  <dependencyManagement>  
    <dependencies>  
    <span style="white-space:pre">    </span><dependency>  
          <groupId>junit</groupId>  
          <artifactId>junit</artifactId>  
          <version>4.12</version>  
          <scope>test</scope>  
        </dependency>  
        <!-- spring beans -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>4.2.5.RELEASE</version>  
        </dependency>  
    </dependencies>  
  </dependencyManagement>  
</project>  

Complaints-service:

<?xml version="1.0"?>  
<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>  
  <parent>  
    <groupId>com.hrtel</groupId>  
    <artifactId>Complaints-parent</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
  </parent>  
    
  <artifactId>Complaints-service</artifactId>  
  <name>Complaints-service</name>  
  <url>http://maven.apache.org</url>  
  <properties>  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  </properties>  
  <!-- 打包時把需要的xml文件一塊打進去 -->  
  <build>    
        <finalName>Complaints-service</finalName>    
        <plugins>    
            <plugin>    
                <groupId>org.apache.maven.plugins</groupId>    
                <artifactId>maven-jar-plugin</artifactId>    
            </plugin>    
        </plugins>    
        <resources>    
            <resource>    
                <directory>src/main/java</directory>    
                <includes>    
                    <include>**/*.xml</include>    
                </includes>    
            </resource>    
            <resource>    
                <directory>src/main/resources</directory>    
                <includes>    
                    <include>**/*.xml</include>    
                    <include>**/*.properties</include>    
                </includes>    
            </resource>    
        </resources>    
    </build>  
  <!-- 依賴配置 -->  
  <dependencies>  
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
    </dependency>  
    <!-- spring beans -->  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-core</artifactId>  
    </dependency>  
  </dependencies>  
</project>  

Complaints-web

<?xml version="1.0"?>  
<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>  
  <parent>  
    <groupId>com.hrtel</groupId>  
    <artifactId>Complaints-parent</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
  </parent>  
    
  <artifactId>Complaints-web</artifactId>  
  <packaging>war</packaging>  
  <name>Complaints-web Maven Webapp</name>  
  <url>http://maven.apache.org</url>  
  <dependencies>  
    <dependency>    
        <groupId>com.hrtel</groupId>    
        <artifactId>Complaints-service</artifactId>    
        <version>${project.version}</version>    
    </dependency>  
      
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
    </dependency>  
      
  </dependencies>  
  <build>  
    <finalName>Complaints-web</finalName>  
  </build>  
</project>  

(轉)在Eclipse中創建Maven多模塊工程