Maven多模塊構建實例

建立coffee-parent項目

New->Maven Project
web

建立coffee-web項目

右鍵coffee-parent項目->New->Project...
apache

注意:須要在src/main/webapp下建立WEB-INF文件夾,以及在WEB-INF下建立web.xml。api

建立coffee-api項目

右鍵coffee-parent項目->New->Project...
注意:前面步驟同coffee-web項目
app

項目結構:

pom.xml文件

coffee-parent項目的pom.xml:webapp

<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.coffee</groupId>
  <artifactId>coffee-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
    <module>coffee-web</module>
    <module>coffee-api</module>
  </modules>
  
  <!-- 管理依賴,供子項目選擇使用  -->
  <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.coffee</groupId>
            <artifactId>coffee-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
  </dependencyManagement>
  
  <build>
    <!-- 配置使用jdk1.7編譯,全部子項目都會繼承此配置,如需配置爲子項目可選繼承,則須要配置到 pluginManagement下-->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

coffee-web項目的pom.xml:maven

<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>
  <parent>
    <groupId>com.coffee</groupId>
    <artifactId>coffee-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>coffee-web</artifactId>
  <packaging>war</packaging>
  
    <!-- coffee-web依賴coffee-api,無需配置version,使用父項目coffee-parent中配置的version-->
    <dependencies>
        <dependency>
            <groupId>com.coffee</groupId>
            <artifactId>coffee-api</artifactId>
        </dependency>
    </dependencies>
</project>

coffee-api項目的pom.xml:ui

<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>
  <parent>
    <groupId>com.coffee</groupId>
    <artifactId>coffee-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>coffee-api</artifactId>
</project>

構建

右鍵coffee-parent項目->Run As->Maven install
code

構建成功:xml

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for coffee-parent 0.0.1-SNAPSHOT:
[INFO] 
[INFO] coffee-parent ...................................... SUCCESS [  0.754 s]
[INFO] coffee-api ......................................... SUCCESS [  2.085 s]
[INFO] coffee-web ......................................... SUCCESS [  0.888 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.980 s
[INFO] Finished at: 2019-06-03T21:44:43+08:00
[INFO] ------------------------------------------------------------------------
相關文章
相關標籤/搜索