IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)

一、前言

IntelliJ IDEA开发环境搭建:具体参考《IntelliJ IDEA教程之如何配置Maven》

Maven环境搭建:具体参考《Windows下Maven安装以及配置》

Maven私服搭建:具体参考《Windows下Maven私服怎么搭建》

主要模拟企业开发是如何搭建Maven工程的,以Spring+SpringMVC+MyBatis为框架搭建,搭建环境Windows8系统。

二、步骤

♦首先,我们创建一个新的工作空间

点击File ->New-Project…,弹出以下界面

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 1

♦点击Next

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 3

♦点击Finish,进入到一个新的工作空间

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 5

♦创建queen-parent工程

点击File ->New->Module…,进入以下界面

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 7

♦点击Next,填写组织名称和项目名称,以及版本号(GAV)

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 9

♦点击Next

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 11

♦点击Next,父工程最后一步,给Module取名及选择工作目录

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 13

♦点击Finish,完成,如图:

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 15

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.queen.parent</groupId>
	<artifactId>queen-parent</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>pom</packaging>

	<!-- 集中定义依赖版本号 -->
	<properties>
		<junit.version>4.10</junit.version>
		<spring.version>4.1.3.RELEASE</spring.version>
		<mybatis.version>3.2.8</mybatis.version>
		<mybatis.spring.version>1.2.2</mybatis.spring.version>
		<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
		<mysql.version>5.1.32</mysql.version>
		<slf4j.version>1.6.4</slf4j.version>
		<jackson.version>2.4.2</jackson.version>
		<druid.version>1.0.9</druid.version>
		<httpclient.version>4.3.5</httpclient.version>
		<jstl.version>1.2</jstl.version>
		<servlet-api.version>2.5</servlet-api.version>
		<jsp-api.version>2.0</jsp-api.version>
		<joda-time.version>2.5</joda-time.version>
		<commons-lang3.version>3.3.2</commons-lang3.version>
		<commons-io.version>1.3.2</commons-io.version>
	</properties>

	<dependencyManagement>
		<dependencies>
			<!-- 单元测试 -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>${junit.version}</version>
				<scope>test</scope>
			</dependency>

			<!-- Spring -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jdbc</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aspects</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<!-- Mybatis -->
			<dependency>
				<groupId>org.mybatis</groupId>
				<artifactId>mybatis</artifactId>
				<version>${mybatis.version}</version>
			</dependency>
			<dependency>
				<groupId>org.mybatis</groupId>
				<artifactId>mybatis-spring</artifactId>
				<version>${mybatis.spring.version}</version>
			</dependency>
			<!-- 分页助手 -->
			<dependency>
				<groupId>com.github.pagehelper</groupId>
				<artifactId>pagehelper</artifactId>
				<version>3.7.5</version>
			</dependency>
			<dependency>
				<groupId>com.github.jsqlparser</groupId>
				<artifactId>jsqlparser</artifactId>
				<version>0.9.1</version>
			</dependency>
			<!-- 通用Mapper -->
			<dependency>
				<groupId>com.github.abel533</groupId>
				<artifactId>mapper</artifactId>
				<version>2.3.4</version>
			</dependency>

			<!-- MySql -->
			<dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
				<version>${mysql.version}</version>
			</dependency>

			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
				<version>${slf4j.version}</version>
			</dependency>

			<!-- Jackson Json处理工具包 -->
			<dependency>
				<groupId>com.fasterxml.jackson.core</groupId>
				<artifactId>jackson-databind</artifactId>
				<version>${jackson.version}</version>
			</dependency>

			<!-- 连接池 -->
			<dependency>
				<groupId>com.jolbox</groupId>
				<artifactId>bonecp-spring</artifactId>
				<version>0.8.0.RELEASE</version>
			</dependency>

			<!-- httpclient -->
			<dependency>
				<groupId>org.apache.httpcomponents</groupId>
				<artifactId>httpclient</artifactId>
				<version>${httpclient.version}</version>
			</dependency>

			<!-- JSP相关 -->
			<dependency>
				<groupId>jstl</groupId>
				<artifactId>jstl</artifactId>
				<version>${jstl.version}</version>
			</dependency>
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>servlet-api</artifactId>
				<version>${servlet-api.version}</version>
				<scope>provided</scope>
			</dependency>
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>jsp-api</artifactId>
				<version>${jsp-api.version}</version>
				<scope>provided</scope>
			</dependency>

			<!-- 时间操作组件 -->
			<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>

		</dependencies>
	</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.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</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>

注意:项目的打包方式为pom

♦创建公用工程queen-common

点击File->New->Module…,进入以下界面,请按照下图红色框框进行选中

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 17

点击Next

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 19

♦点击Next

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 21

♦点击Next

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 23

♦点击Finish,创建成功

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 25

♦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.queen.common</groupId>
	<artifactId>queen-common</artifactId>
	<parent>
		<groupId>com.queen.parent</groupId>
		<artifactId>queen-parent</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
</project>

注意:项目的打包方式为jar和父项目有所不同

♦创建queen-manage

跟前面创建queen-parent步骤类似,就不再赘述了,注意:下面打包方式为pom

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 27

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 29

♦点击Finish,创建成功

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.queen.manage</groupId>
    <artifactId>queen-manage</artifactId>
    <packaging>pom</packaging>
    <parent>
        <groupId>com.queen.parent</groupId>
        <artifactId>queen-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
</project>

♦接下来我们创建工程queen-manage的子模块工程queen-manage-pojo

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 31

♦点击Next

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 33

♦点击Next

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 35

♦点击Next

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 37

♦点击Finish完成

紧接着我们依次创建queen-manage-mapper,queen-manage-service,queen-manage-controller工程,步骤跟queen-manage-pojo一样。

注意:在我们创建queen-manage-controller时,打包方式要选择war。

♦最终我们新建的所有工程如下:

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 39

♦现在我们要整理一下子工程之间的依赖关系

queen-manage-controller-依赖-queen-manage-service-依赖-queen-manage-mapper-依赖-queen-manage-pojo

上面可知,我们的依赖关系如此。

那我们修改一下pom文件

在queen-manage-mapper的pom文件中加入queen-manage-pojo的坐标

<dependency>
	<groupId>com.queen.manage</groupId>
	<artifactId>queen-manage-pojo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

在queen-manage-service的pom文件中加入queen-manage-mapper的坐标

<dependency>
	<groupId>com.queen.manage</groupId>
	<artifactId>queen-manage-mapper</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

在queen-manage-controller的pom文件中加入queen-manage-service的坐标

<dependency>
	<groupId>com.queen.manage</groupId>
	<artifactId>queen-manage-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

♦各个工程中导入依赖

在queen-manage-controller工程的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>
  <parent>
    <groupId>com.queen.manage</groupId>
    <artifactId>queen-manage</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>queen-manage-controller</artifactId>
  <packaging>war</packaging>
  <dependencies>
		<dependency>
			<groupId>com.queen.manage</groupId>
			<artifactId>queen-manage-service</artifactId>
			<version>1.0.0-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
		</dependency>
		<!-- MySql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- Jackson Json处理工具包 -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>
		<!-- 连接池 -->
		<dependency>
			<groupId>com.jolbox</groupId>
			<artifactId>bonecp-spring</artifactId>
		</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>
			<version>1.3.1</version>
		</dependency>
		<!-- 时间操作组件 -->
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
		</dependency>
	</dependencies>
</project>

在queen-manage-service工程的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>
  <parent>
    <groupId>com.queen.manage</groupId>
    <artifactId>queen-manage</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>
  <artifactId>queen-manage-service</artifactId>
  <dependencies>
		<dependency>
			<groupId>com.queen.manage</groupId>
			<artifactId>queen-manage-mapper</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<!-- 分页助手 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.jsqlparser</groupId>
			<artifactId>jsqlparser</artifactId>
		</dependency>
	</dependencies>
</project>

在queen-manage-mapper工程的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>
  <parent>
    <groupId>com.queen.manage</groupId>
    <artifactId>queen-manage</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>queen-manage-mapper</artifactId>
  <dependencies>
		<dependency>
			<groupId>com.queen.manage</groupId>
			<artifactId>queen-manage-pojo</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<!-- Mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
		</dependency>
		<!-- 通用Mapper -->
		<dependency>
			<groupId>com.github.abel533</groupId>
			<artifactId>mapper</artifactId>
			<exclusions>
				<exclusion>
					<artifactId>persistence-api</artifactId>
					<groupId>javax.persistence</groupId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
</project>

在queen-manage-pojo工程的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>
  <parent>
    <groupId>com.queen.manage</groupId>
    <artifactId>queen-manage</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>queen-manage-pojo</artifactId>
  <dependencies>
		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0</version>
		</dependency>
	</dependencies>
</project>

在queen-manage工程的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>
	<parent>
		<groupId>com.queen.parent</groupId>
		<artifactId>queen-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.queen.manage</groupId>
	<artifactId>queen-manage</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<modules>
		<module>queen-manage-pojo</module>
		<module>queen-manage-mapper</module>
		<module>queen-manage-service</module>
		<module>queen-manage-web</module>
	</modules>
	<dependencies>
		<dependency>
			<groupId>com.queen.common</groupId>
			<artifactId>queen-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<!-- 单元测试 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</dependency>
		<!-- Apache工具组件 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-io</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<!-- 配置Tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<port>8080</port>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

♦运行工程,这里我们使用Maven Tomcat Plugin运行web项目

点击主界面右上角IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 41,进入以下界面

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 43

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 45

注意:运行之前,每个工程需要clean和install一下 ,否则启动不了,如果你不知道IDEA如何使用Maven的clean功能,请参考这篇文章《IntelliJ IDEA教程之如何clean或者install Maven项目》

web.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>queen-manage</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

</web-app>

index.jsp文件如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
</head>
<body>
	Welcome:queen
</body>

♦点击主界面右上角绿色三角形运行项目

运行起来之后,我们访问一下服务http://localhost:8080/index.jsp

IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)的照片 - 47

至此,我们关于IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)介绍完毕。其实博主也写过一篇关于Eclipse下如何创建聚合工程,具体可以参考《Eclipse下Maven工程多模块继承和聚合创建(详解)》这篇文章。

博客地址:http://www.marsitman.com/idea/intellij-idea-create-maven-union.html
版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。

 

 

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作



点赞
  1. 丨木頭丶吅说道:

    index.jsp需要在controller/src/main下新建webapp包中添加,web.xml在webapp下新建web-inf包添加。
    install manage前要先install parent和common,才能启动tomcat。

  2. 说道:

    为啥我的没有web.xml和index.jsp呢?

    1. rongjing说道:

      文中有说怎么新建这两个文件哟

  3. 小丿懒虫说道:

    那些pom文件都要那样写?基础生成的都差点东西

    1. 黛玛Queen说道:

      看你自己的需要

  4. 安全说道:

    感谢博主 IDEA一系列文章讲的很好,已经初步入门!谢谢!

    1. rongjing说道:

      博主,也是刚开始使用这个,共勉,共勉 :cool:

发表评论

电子邮件地址不会被公开。 必填项已用*标注