使用IDEA基于Maven搭建多模块聚合工程(springmvc+spring+mybatis整合)

终于有时间搞java了,今天使用IDEA基于maven搭建了多模块聚合工程,经过了多番尝试,终于成功啦!

注意:这里是在原作者的基础上改进的版本,原作者的版本的pom依赖存在问题,所以,这里的依赖全部是我自己的依赖,图片按照这个来完全没有问题!

转载修改说明:这里是对第二作者(是相对于原作者而言的)的pom信息中与图片不一致的地方进行高速,力求与原作者的图片一致,以使查看的人员不至于拷贝过去的信息还需要调整。

 

一.工程目录

下面是搭建之后的目录
这里写图片描述

先看一下目录关系

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文件为:

<?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/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>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <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>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>${commons-io.version}</version>
      </dependency>

    </dependencies>
  </dependencyManagement>

  <build>
    <finalName>${project.artifactId}</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
          <configuration>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- 配置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

<?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/xsd/maven-4.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>

    <groupId>com.slx</groupId>
    <artifactId>taotao-common</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    </dependencies>
</project>

taotao-manager的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/xsd/maven-4.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>

    <groupId>com.slx</groupId>
    <artifactId>taotao-manager</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>


    <dependencies>
        <dependency>
            <groupId>com.slx</groupId>
            <artifactId>taotao-common</artifactId>
            <version>1.0-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>
            <version>${commons-lang3.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
    </dependencies>

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

    <modules>
        <module>taotao-manager-pojo</module>
        <module>taotao-manager-dao</module>
        <module>taotao-manager-service</module>
        <module>taotao-manager-web</module>
    </modules>
</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/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>com.slx</groupId>
		<artifactId>taotao-manager</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>

	<groupId>com.slx</groupId>
	<artifactId>taotao-manager-pojo</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>com.slx</groupId>
			<artifactId>taotao-common</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0</version>
		</dependency>
	</dependencies>
</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/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>com.slx</groupId>
		<artifactId>taotao-manager</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>

	<groupId>com.slx</groupId>
	<artifactId>taotao-manager-dao</artifactId>
	<packaging>jar</packaging>

	<name>taotao-manager-dao</name>
	<url>http://maven.apache.org</url>
	<version>1.0-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<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>
		<!-- 通用Mapper -->
		<dependency>
			<groupId>com.github.abel533</groupId>
			<artifactId>mapper</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/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>com.slx</groupId>
		<artifactId>taotao-manager</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>

	<groupId>com.slx</groupId>
	<artifactId>taotao-manager-service</artifactId>
	<packaging>jar</packaging>

	<name>taotao-manager-service</name>
	<url>http://maven.apache.org</url>
	<version>1.0-SNAPSHOT</version>
	<!-- TODO project name  -->
	<description></description>

	<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>
		<!-- Spring -->
		<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>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.jsqlparser</groupId>
			<artifactId>jsqlparser</artifactId>
		</dependency>
	</dependencies>

</project>

2)taotao-manager-web创建

同样File——>New——>Module

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


这里写图片描述

同样继承taotao-manager
这里写图片描述

同样需要改写路径,点击finish,taotao-manager-web子模块创建完成
这里写图片描述
taotao-manager-web的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/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-web</artifactId>
  <packaging>war</packaging>
  <name>taotao-manager-web</name>
  <url>http://maven.apache.org</url>

  <!-- 添加依赖 -->
  <dependencies>
    <dependency>
      <groupId>com.slx</groupId>
      <artifactId>taotao-manager-service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</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>joda-time</groupId>
      <artifactId>joda-time</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
参考资料

    https://blog.csdn.net/for_my_life/article/details/78939078

    https://blog.csdn.net/sihai12345/article/details/80666764

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值