首页 技术 正文
技术 2022年11月18日
0 收藏 916 点赞 4,466 浏览 12169 个字

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(具体方法:见第一章)

引入的项目结构如下:

第二章 企业项目开发–maven父子模块

注意:

  • 以上第一个框处的内容是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这一块有几点建议:(很重要)

  • 安装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>标签,在其中配置多套运行环境(一般而言,配置三套:开发,预上线,上线),每套环境配置不同的数据库和服务器。
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294