欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Maven多模块构建实例

程序员文章站 2022-08-10 10:05:51
创建coffee parent项目 New Maven Project 创建coffee web项目 右键coffee parent项目 New Project... 注意:需要在src/main/webapp下创建WEB INF文件夹,以及在WEB INF下创建web.xml。 创建coffee ......

创建coffee-parent项目

new->maven project
Maven多模块构建实例

Maven多模块构建实例

创建coffee-web项目

右键coffee-parent项目->new->project...
Maven多模块构建实例

Maven多模块构建实例

Maven多模块构建实例

注意:需要在src/main/webapp下创建web-inf文件夹,以及在web-inf下创建web.xml。

创建coffee-api项目

右键coffee-parent项目->new->project...
注意:前面步骤同coffee-web项目
Maven多模块构建实例

项目结构:

Maven多模块构建实例

pom.xml文件

coffee-parent项目的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/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:

<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:

<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
Maven多模块构建实例

构建成功:

[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] ------------------------------------------------------------------------