IDEA基于Maven搭建多模块聚合工程

Posted jtlgb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IDEA基于Maven搭建多模块聚合工程相关的知识,希望对你有一定的参考价值。

一.工程目录

下面是搭建之后的目录
 

 

 

先看一下目录关系

taotao-parent(父工程管理jar包的版本)

  |--taotao-common(把通用的工具类打包)

  |--taotao-manager(继承父工程)

      |-taotao-manager-pojo

      |-taotao-manager-dao

      |-taotao-manager-service

      |-taotao-manager-web(war包)

taotao-parent 为项目的父工程,用来管理jar包及版本

taotao-common为通用工程,继承父工程,就是把通用的工具类打包

taotao-manager为开发工程,同样继承父工程

其中,taotao-manager-pojo,taotao-manager-dao,taotao-manager-service和taotao-manager-web均为开发工程的一个子模块,依赖于开发工程taotao-manager

前3个子模块打成jar包,taotao-manager-web打成war包

二.创建工程
1.先创建一个空工程,步骤如下:
File——>new——>project

 

 

选择Empty——>next

 

 

填写工程名称和位置,点击finnish,完成了空工程的创建

 

 

2.创建父工程 taotao-parent,步骤如下:

File——>New——>Module

 

 

点maven,勾选archetype,选quickstart这项,然后Next下一步

 

 

填写GroupId,一般为组织名称;填写ArtifactId,为项目名称

 

 

选择本地maven目录及maven配置文件

 

 

填写项目名称及位置,点Finish,父工程创建完成

 

 

等待生成pom文件,修改打包方式为pom,pom文件为:

<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.slx</groupId>
  <artifactId>taotao-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
 
  <name>taotao-parent</name>
  <url>http://maven.apache.org</url>
 
  <!-- 集中定义依赖版本号 -->
  <properties>
    <junit.version>4.12</junit.version>
    <spring.version>4.1.3.RELEASE</spring.version>
    <mybatis.version>3.2.8</mybatis.version>
    <!--省略,自行添加-->
  </properties>
  <dependencyManagement>
    <dependencies>
      <!-- 时间操作组件 -->
      <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>${joda-time.version}</version>
      </dependency>
      <!-- Apache工具组件 -->
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>${commons-lang3.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>${commons-io.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>${commons-net.version}</version>
      </dependency>
      <!--省略,自行添加-->
  </dependencyManagement>
 
  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
      <!-- 资源文件拷贝插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <!-- java编译插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
    <pluginManagement>
      <plugins>
        <!-- 配置Tomcat插件 -->
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

3.创建通用工程taotao-common

File——>New——>Module

 

 

点maven后直接next下一步

 

 

继承taotao-parent,填写ArtifactId,然后next下一步

 

 

填写通用模块名称,点finish,通用模块创建完成

 

 

开发模块taotao-manager与通用模块创建方式一致

taotao-common的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/maven-v4_0_0.xsd">
    <parent>
        <artifactId>taotao-parent</artifactId>
        <groupId>com.slx</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../taotao-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>taotao-common</artifactId>
    <packaging>jar</packaging>
 
    <name>taotao-common</name>
    <url>http://maven.apache.org</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <!-- 添加依赖 -->
    <dependencies>
        <!-- 时间操作组件 -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
        <!-- Apache工具组件 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <!--省略,自行添加-->
        </dependency>
    </dependencies>
</project>

taotao-manager的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/maven-v4_0_0.xsd">
    <parent>
        <artifactId>taotao-parent</artifactId>
        <groupId>com.slx</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../taotao-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>taotao-manager</artifactId>
    <packaging>pom</packaging>
    <name>taotao-manager</name>
    <url>http://maven.apache.org</url>
    <modules>
        <module>taotao-manager-pojo</module>
        <module>taotao-manager-dao</module>
        <module>taotao-manager-service</module>
        <module>taotao-manager-web</module>
    </modules>
 
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <!-- 添加依赖 -->
    <dependencies>
        <dependency>
            <groupId>com.slx</groupId>
            <artifactId>taotao-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
 
    <build>
        <!-- 配置插件 -->
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <port>8080</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

4.创建开发模块下的子模块,分两类介绍,taotao-manager-pojo,taotao-manager-dao,taotao-manager-service创建方式一样,taotao-manager-web的创建有点差别

1)以taotao-manager-pojo为例

File——>new——>module

 

 

选maven——>勾选archetype——>选quickstart这项——>点next

 

 

继承taotao-manager,填写ArtifactId

 

 

这里注意改写路径,点finish,一个子模块创建完成

 

 

taotao-manager-pojo的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">
    <parent>
        <artifactId>taotao-manager</artifactId>
        <groupId>com.slx</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>taotao-manager-pojo</artifactId>
    <packaging>jar</packaging>
 
    <name>taotao-manager-pojo</name>
    <url>http://maven.apache.org</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
</project>

taotao-manager-dao的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">
    <parent>
        <artifactId>taotao-manager</artifactId>
        <groupId>com.slx</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>taotao-manager-dao</artifactId>
    <packaging>jar</packaging>
 
    <name>taotao-manager-dao</name>
    <url>http://maven.apache.org</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <!-- 依赖与pojo子模块 -->
        <dependency>
            <groupId>com.slx</groupId>
            <artifactId>taotao-manager-pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
 
    </dependencies>
</project>

taotao-manager-service的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">
    <parent>
        <artifactId>taotao-manager</artifactId>
        <groupId>com.slx</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>taotao-manager-service</artifactId>
    <packaging>jar</packaging>
 
    <name>taotao-manager-service</name>
    <url>http://maven.apache.org</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>com.slx</groupId>
            <artifactId>taotao-manager-dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
    </dependencies>
</project>

2)taotao-manager-web创建

同样File——>New——>Module

只不过这里选择webapp这项(唯一不同的地方,其他地方类似),点next下一步

 

 同样继承taotao-manager

 

 

同样需要改写路径,点击finish,taotao-manager-web子模块创建完成

 

 

taotao-manager-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/maven-v4_0_0.xsd">
    <parent>
        <artifactId>taotao-manager</artifactId>
        <groupId>com.slx</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>taotao-manager-web</artifactId>
    <packaging>war</packaging>
    <name>taotao-manager-web Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <!-- 添加依赖 -->
    <dependencies>
        <dependency>
            <groupId>com.slx</groupId>
            <artifactId>taotao-manager-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- JSP相关 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- 文件上传组件 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>taotao-manger-web</finalName>
    </build>
</project>

5.接下来运行工程使用:tomcat7:run

右上角Edit Configurations

 

 

选maven

 

 

 

 

需要把taotao-parent等需要安装的都安装到本地仓库。

 

 

最后运行,如下图所示,就说明跑通了!

 

 

 

 

总结:

之前看别人的博客,有选择portlet项,自己弄死活跑不通。

taotao-parent,taotao-manager-pojo,taotao-manager-dao,taotao-manager-service选择quickstart那项;

taotao-common,taotao-manager啥都不选;

taotao-manager-web选择webapp那项。

还要注意打包方式,jar,pom,war

 

 

以上是关于IDEA基于Maven搭建多模块聚合工程的主要内容,如果未能解决你的问题,请参考以下文章

spring boot多模块聚合工程

Maven聚合开发实例详解---5555字

1.搭建Maven 多模块应用 --Intellij IDEA 2016.3.5

微服务中基于Spring Boot的maven分布式项目框架的搭建

使用maven搭建多模块项目

maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目