2.1、maven父子模块

在实际开发中,我们基本都会用maven父子分模块的方式进行项目的开发。

2.2、实际操作

2.2.1、手工建立一个ssmm0的文件夹,并在该文件夹中加入一个pom.xml文件,该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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xxx</groupId>
<artifactId>ssmm0</artifactId>
<version>1.0-SNAPSHOT</version> <name>ssmm0</name>
<packaging>pom</packaging><!-- 父模块 --> <!-- 管理子模块 -->
<modules>
<module>ssmm</module>
</modules> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>

注意:

  • 父模块的pom.xml文件的<packaging>标签内容为pom;而需要部署的子项目为war;只是作为其他项目的工具的子项目为jar
  • 使用<modules>标签管理所有的子模块,以后再有新的子模块,只需要在<modules>添加新的<module>子标签即可

2.2.2、将上一章建好的那个项目ssmm放入ssmm0文件夹下,修改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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 指定父模块 -->
<parent>
<groupId>com.xxx</groupId>
<artifactId>ssmm0</artifactId>
<version>1.0-SNAPSHOT</version>
</parent> <groupId>com.xxx.ssmm0</groupId>
<artifactId>ssmm0-ssmm</artifactId>
<!--<version>1.0-SNAPSHOT</version>--><!-- 父模块已经指定了版本号,这里就不用了--> <name>ssmm0-ssmm</name>
<packaging>war</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties> <!-- 引入实际依赖 -->
<dependencies>
<!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.39</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<!-- 这个是使用velocity的必备包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.6.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
<scope>runtime</scope>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.47</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
<!-- velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>velocity-tools</groupId>
<artifactId>velocity-tools-generic</artifactId>
<version>1.2</version>
</dependency>
<!-- 用于加解密 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.47</version>
</dependency>
<!-- 集合工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<!-- http -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.6</version>
</dependency>
</dependencies>
</project>

注意:在上一章的基础上做了以下几点修改

  • 添加了<parent>标签,用来指定父模块,该标签内有父模块的坐标(三要素:groupId/artifactId/version)
  • 子模块不需要再有版本号了,由父模块来指定就好
  • 将子模块中下边这一块儿代码也删掉(因为在父模块中已经指定了)
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

建议:

  • "子模块的groupId"设为"父模块的groupId.父模块的artifactId"
  • "子模块的artifactId"设为"父模块的artifactId-子模块的的名称","父模块的artifactId-子模块的的名称"也就是子模块的项目名
  • 无论父模块还是子模块,建议同一个pom.xml文件中的artifactId与name标签内容相同

这几点建议,在我们编写和部署项目的时候都一目了然;以上几点建议,请对照着以上两个pom.xml文件对号入座。

2.2.3、在ssmm0的文件夹中,即父模块pom.xml所在的文件夹中运行命令窗口,执行"mvn clean compile"命令

2.2.4、编译成功后,将项目ssmm0以maven项目引入eclipse(具体方法:见第一章)

引入的项目结构如下:

注意:

  • 以上第一个框处的内容是ssmm0-ssmm编译出来的
  • 第二个红框正好印证了"父模块的artifactId-子模块的的名称"也就是子模块的项目名这句

2.2.5、运行ssmm项目,进行测试

这样,就建立起了一个maven的父子模块项目了。

注:若以后再增加一个新的模块该怎么做?

1)我们在父模块的pom.xml文件中添加<module>

2)在ssmm0文件夹下手工创建一个字maven项目(创建方式见第一章),假设为ssmm2,ssmm2的pom.xml文件类似于ssmm子项目的pom.xml即可

3)在ssmm2文件夹中,打开命令窗口,执行"mvn compile"

4)将ssmm2引入eclipse

5)用eclipse的maven插件编译在ssmm0上执行:"Run as"-->"Maven build"-->"clean compile"

这样,就新建了一个子项目了。

针对maven这一块有几点建议:(很重要)

  • 安装maven后,最好在M2_HOME/conf/setting.xml文件添加如下代码,将之后项目中用到的jar包全部下载到下边的文件夹中(当然,在修改setting.xml文件时,最好先复制一份最备份),防止C盘撑爆。

    <localRepository>E:/Java/mavenLocalRepository</localRepository>
  • 在实际使用中,我们会架设私服(一般采用nexus),这样做主要是为了加快jar的下载速度,之后需要在父pom.xml文件中指定私服的位置
       <!-- nexus -->
    <repositories>
    <repository>
    <id>xxx-Nexus</id>
    <name>xxx Maven Repository</name>
    <url>http://xxx.com/nexus/</url>
    </repository>
    </repositories>

    一个<repository>标签就是一个私服,可以加多个私服。

  • 在实际使用中,我们会在父pom.xml文件中加入一个依赖管理池<dependencyManagement>,在这个池中指定了jar的版本号<version>和范围<scope>,之后在子项目中实际引入jar的坐标的时候,就不需要写<version>标签和<scope>了;当然,这样做,也可以保证在整个项目中,我们使用的同一个jar都是同一个版本;同时,需要指出的是,为了子模块重复代码的减少,在父模块处,会先引入一些所有子模块都会使用的jar(例如:log4j等),这样在子项目中就不需要再引入这些jar了。这里给出父pom.xml与子项目ssmm的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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xxx</groupId>
    <artifactId>ssmm0</artifactId>
    <version>1.0-SNAPSHOT</version> <name>ssmm0</name>
    <packaging>pom</packaging><!-- 父模块 --> <!-- 管理子模块 -->
    <modules>
    <module>ssmm</module>
    </modules> <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties> <!-- dependencyManagement不会引入实际的依赖,只是作为一个依赖池,供其和其子类使用 -->
    <dependencyManagement>
    <dependencies>
    <!-- json -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.1.39</version>
    </dependency>
    <!-- servlet -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <!-- spring -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.2.6.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.2.6.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.6.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.2.6.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.2.6.RELEASE</version>
    </dependency>
    <!-- 这个是使用velocity的必备包 -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>3.2.6.RELEASE</version>
    </dependency>
    <!-- mysql -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.27</version>
    <scope>runtime</scope>
    </dependency>
    <!-- 数据源 -->
    <dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jdbc</artifactId>
    <version>7.0.47</version>
    </dependency>
    <!-- mybatis -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.1.1</version>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.1.1</version>
    </dependency>
    <!-- velocity -->
    <dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.5</version>
    </dependency>
    <dependency>
    <groupId>velocity-tools</groupId>
    <artifactId>velocity-tools-generic</artifactId>
    <version>1.2</version>
    </dependency>
    <!-- 用于加解密 -->
    <dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.7</version>
    </dependency>
    <dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.47</version>
    </dependency>
    <!-- 集合工具类 -->
    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.0</version>
    </dependency>
    <!-- http -->
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.6</version>
    </dependency>
    </dependencies>
    </dependencyManagement> <!-- 引入实际依赖 -->
    <dependencies>
    <!-- json -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    </dependency>
    <!-- servlet -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    </dependency>
    <!-- spring -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    </dependency>
    <!-- 这个是使用velocity的必备包 -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    </dependency>
    <!-- mysql -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- 数据源 -->
    <dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jdbc</artifactId>
    </dependency>
    <!-- mybatis -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    </dependency>
    <!-- velocity -->
    <dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    </dependency>
    <dependency>
    <groupId>velocity-tools</groupId>
    <artifactId>velocity-tools-generic</artifactId>
    </dependency>
    <!-- 集合工具类 -->
    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    </dependency>
    </dependencies>
    </project>
     <?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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 指定父模块 -->
    <parent>
    <groupId>com.xxx</groupId>
    <artifactId>ssmm0</artifactId>
    <version>1.0-SNAPSHOT</version>
    </parent> <groupId>com.xxx.ssmm0</groupId>
    <artifactId>ssmm0-ssmm</artifactId>
    <!--<version>1.0-SNAPSHOT</version>--><!-- 父模块已经指定了版本号,这里就不用了--> <name>ssmm0-ssmm</name>
    <packaging>war</packaging> <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties> <!-- 引入实际依赖 -->
    <dependencies>
    <!-- 用于加解密 -->
    <dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    </dependency>
    <dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    </dependency>
    <!-- http -->
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    </dependency>
    </dependencies>
    </project>
    对于maven3来讲,常用的scope有以下4种:

    • compile:默认,作用于编译、测试和运行
    • provided:常见的就是servlet-api,作用于编译和测试(运行的时候由容器提供了)
    • runtime:常见的就是mysql-connector,作用在运行和测试时
    • test:常见的就是junit,spring-test,作用在测试时

    在配置pom.xml的时候,需要将这些写上scope写上。

  • 在实际使用中,我们会在父pom.xml配置<profiles>标签,在其中配置多套运行环境(一般而言,配置三套:开发,预上线,上线),每套环境配置不同的数据库和服务器。

第二章 企业项目开发--maven父子模块的更多相关文章

  1. 第一章 企业项目开发--maven+springmvc+spring+mybatis+velocity整合

    说明:本系列文章主要是对自己在一家大型互联网公司实习的过程中对所学知识的总结!参与的是实际中使用的上线项目. 代码的github地址:https://github.com/zhaojigang/ssm ...

  2. 第六章 企业项目开发--cookie

    注:本章代码基于<第五章 企业项目开发--mybatis注解与xml并用>的代码,链接如下: http://www.cnblogs.com/java-zhao/p/5120792.html ...

  3. 第九章 企业项目开发--分布式缓存Redis(1)

    注意:本章代码将会建立在上一章的代码基础上,上一章链接<第八章 企业项目开发--分布式缓存memcached> 1.为什么用Redis 1.1.为什么用分布式缓存(或者说本地缓存存在的问题 ...

  4. 第十一章 企业项目开发--消息队列activemq

    注意:本章代码基于 第十章 企业项目开发--分布式缓存Redis(2) 代码的github地址:https://github.com/zhaojigang/ssmm0 消息队列是分布式系统中实现RPC ...

  5. 第七章 企业项目开发--本地缓存guava cache

    1.在实际项目开发中,会使用到很多缓存技术,而且数据库的设计一般也会依赖于有缓存的情况下设计. 常用的缓存分两种:本地缓存和分布式缓存. 常用的本地缓存是guava cache,本章主要介绍guava ...

  6. 第五章 企业项目开发--mybatis注解与xml并用

    本章的代码建立在第四章<Java框架整合--切分配置文件>的项目代码之上,链接如下: http://www.cnblogs.com/java-zhao/p/5118184.html 在实际 ...

  7. 第八章 企业项目开发--分布式缓存memcached

    注意:本节代码基于<第七章 企业项目开发--本地缓存guava cache> 1.本地缓存的问题 本地缓存速度一开始高于分布式缓存,但是随着其缓存数量的增加,所占内存越来越大,系统运行内存 ...

  8. 企业项目开发--分布式缓存Redis

    第九章 企业项目开发--分布式缓存Redis(1) 注意:本章代码将会建立在上一章的代码基础上,上一章链接<第八章 企业项目开发--分布式缓存memcached> 1.为什么用Redis ...

  9. 企业项目开发--cookie(1)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 注:本章代码基于<第五章 企业项目开发--mybatis注解与xml并用>的代码,链接如下: h ...

随机推荐

  1. 远程连接mysql root账号报错:2003-can't connect to MYSQL serve

    1.远程连接Linux系统,登录数据库:mysql -uroot -p(密码) 2.修改root账号的设置: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDE ...

  2. [CodeForces-606E] Freelancer's Dreams 凸包 模型转换

    大致题意: 有一个人想要获得p个经验点和q元钱.现在给出n份工作,每份工作每天能得到Ai的经验值和Bi的钱,问最少需要工作多少天, 能使得总经验值>=p,总钱>=q. 先对给出的n份工作以 ...

  3. 如何快速分析出现性能问题的Linux服务器

    Brendan Gregg曾经分享过当遇到一个系统性能问题时,如何利用登录的前60秒对系统的性能情况做一个快速浏览和分析,主要包括如下10个工具,这是一个非常有用且有效的工具列表.本文将详细介绍这些命 ...

  4. [漏洞复现] CVE-2017-11882 通杀所有Office版本

    此漏洞是由Office软件里面的 [公式编辑器] 造成的,由于编辑器进程没有对名称长度进行校验,导致缓冲区溢出,攻击者通过构造特殊的字符,可以实现任意代码执行. 举个例子,如果黑客利用这个漏洞,构造带 ...

  5. 【基础知识】Dom基础

    [学习日记]Dom基础 1.   内容:使用JavaScript操作Dom进行DHTML开发 2.   目标:能共使用JavaScript操作Dom实现常见的DHTML效果 3.   DHTML= C ...

  6. NetCore控制台实现自定义CommandLine功能

    命令行科普: 例如输入: trans 123 456 789 -r 123 -r 789上面例子中:trans是Command,123 456 789是CommandArgument,-r之后的都是C ...

  7. 学会使用DNSPod,仅需三步

    学会使用DNSPod,仅需三步   第一步:在DNSPod添加记录 1.访问 https://www.dnspod.cn网站,在DNSPod官网首页的右上角,有[注册],如下图所示,点击[注册]按钮 ...

  8. 决策树(CART)

    CART算法全称是分类回归算法,(Classification And Regression Tree),他与ID3.C4.5的不同在于: 1.既可以处理分类问题又可以处理回归问题 2.使用基尼系数作 ...

  9. bzoj3456 城市规划 多项式求In

    \(n\)个点的无向联通图的个数 打着好累啊 一定要封装一个板子 记\(C(x)\)为无向图个数的指数型生成函数,\(C(0) = 1\) 记\(G(x)\)为无向联通图个数的指数型生成函数,\(G( ...

  10. apt-get出现无法定位安装包问题解决

    这个问题出现在sources.list上 编辑/etc/apt/sources.list下的文件 找到检查你的存储库是否正确 你可以在以下页面找到不同版本 Kali Linux 的存储库:http:/ ...