索引:
参看代码 GitHub:
一、Intelij IDEA 中构建 maven 项目及模块
1.新建 maven 项目,在IDE中的菜单位置


2.项目中新建模块在IDE中的菜单位置

3.整个项目的结构

二、Maven 模块及模块关系要点讲解
1.solution/pom.xml
<groupId>lm.solution</groupId>
<artifactId>solution</artifactId>
<version>1.0-SNAPSHOT</version>
讲解:
groupId :模块项目归属组织名称
artifactId:项目内模块名称
version:模块的版本号
这三个属性唯一的标识了一个模块,也被称为 maven 坐标。
<modules>
<module>webapi</module>
… …
</modules>
讲解:
因为 solution 这个模块是做为项目父模块存在的,在 modules 标签内就指明了该模块都包含哪些子模块:
webapi、web、common、service、mapper、pojo、console、webservice。
<packaging>pom</packaging>
讲解:
因为 solution 模块将包含多个子模块,所以这里的打包方式选择 pom 值。
<properties>
<spring.version>4.3.13.RELEASE</spring.version>
… …
</properties>
讲解:
在父模块中定义的 properties maven 属性变量,除了在本模块中可直接引用外,在所有的子模块中也可以直接被引用,
也就是说在子模块中就可以不必在重新定义 maven的属性变量了,如 spring.version 这个属性变量,在所有模块及子模块中
可以被直接使用 ${spring.version} 。
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
… …
</dependency>
… …
</dependencies>
讲解:
在父模块中定义的类库依赖,除了在父模块中会做引用以外,在所有子模块中也都会做自动的引用,
所以在子模块中就不必添加 dependencies 中的依赖了,如 junit 这个第三方类库,在子模块中 maven
会自动继承父模块的 junit 依赖,子模块中就不必再添加 junit 的类库依赖了。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
… …
</dependencies>
</dependencyManagement>
讲解:
有的类库依赖,在部分子模块中需要,在部分子模块中不需要,此时,就可将依赖关系放到此节点中,
例如:mybatis 这个第三方 orm 库,在 pojo(仅是映射关系的实体层) 子模块中没有依赖,而在 mapper(orm 存储仓层) 这个
子模块中就是关键依赖。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
… …
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat8-maven-plugin</artifactId>
… …
</plugin>
</plugins>
</pluginManagement>
</build>
讲解:
这里 plugins 与 pluginManagement 可参看上面讲解到的 dependencies 与 dependencyManagement 的作用,他们作用
类似,因为每个模块都需要 maven-compiler-plugin 编译插件,所以直接将其放到父模块的 plugins 标签内,而 tomcat8-maven-plugin
插件只有 web 子模块需要,所以将其放到 pluginManagement 标签中。
solution/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">
<modelVersion>4.0.0</modelVersion>
<groupId>lm.solution</groupId>
<artifactId>solution</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>webapi</module>
<module>web</module>
<module>common</module>
<module>service</module>
<module>mapper</module>
<module>pojo</module>
<module>console</module>
<module>webservice</module>
</modules>
<packaging>pom</packaging>
<name>solution</name>
<url>http://maven.apache.org</url>
<properties>
<!--编译字符集-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- spring -->
<spring.version>4.3.13.RELEASE</spring.version>
<!-- log4j -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<!-- jackson -->
<jackson.version>2.9.4</jackson.version>
<!-- junit -->
<junit.version>4.12</junit.version>
<!-- aspectj -->
<aspectj.version>1.8.13</aspectj.version>
<!-- cglib -->
<cglib.version>3.1</cglib.version>
<!-- mybatis -->
<mybatis.version>3.4.5</mybatis.version>
<!-- mybatis-spring -->
<mybatisSpring.version>1.3.1</mybatisSpring.version>
<!--mysql-connector-->
<mysql.version>5.1.34</mysql.version>
<!--druid-->
<druid.version>1.0.5</druid.version>
<!--javax.servlet-->
<javaxServlet.version>3.0.1</javaxServlet.version>
<!--jsp-api-->
<jspApi.version>2.2</jspApi.version>
<!--jstl-->
<jstl.version>1.2</jstl.version>
<!--json-lib-->
<jsonLib.version>2.1</jsonLib.version>
<!--jackson old-->
<jacksonOld.version>1.9.13</jacksonOld.version>
<!--dom4j-->
<dom4j.version>1.6.1</dom4j.version>
<!--ehcache core-->
<ehcacheCore.version>2.6.9</ehcacheCore.version>
<!--ehcache web-->
<ehcacheWeb.version>2.0.4</ehcacheWeb.version>
<!--commons-fileupload-->
<commonsFileupload.version>1.3.1</commonsFileupload.version>
<!--commons-io-->
<commonsIo.version>2.4</commonsIo.version>
<!--commons-codec-->
<commonsCodec.version>1.9</commonsCodec.version>
<!--commons-collections4-->
<commonsCollections4.version>4.0</commonsCollections4.version>
<!--commons-beanutils-->
<commonsBeanutils.version>1.7.0</commonsBeanutils.version>
<!--freemarker-->
<freemarker.version>2.3.19</freemarker.version>
<!-- poi poi-ooxml -->
<poi.version>3.9</poi.version>
<!--org.apache.httpcomponents-->
<httpcore.version>4.4.8</httpcore.version>
<httpclient.version>4.5.4</httpclient.version>
<!-- jaxws-rt -->
<jaxwsRt.version>2.3.0</jaxwsRt.version>
<!-- jedis -->
<jedis.version>2.9.0</jedis.version>
<!-- rabbitmq -->
<amqpClient.version>5.1.2</amqpClient.version>
<!--fastjson-->
<fastjson.version>1.2.46</fastjson.version>
<!-- jsr250 -->
<jsr250.version>1.0</jsr250.version>
</properties>
<dependencies>
<!--单元测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<!--test 说明这个包的存活是在test周期,也就是发布时将不包含这个jar包-->
<scope>test</scope>
</dependency>
<!--spring单元测试依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<!-- 因依赖 会自动引入 spring-aop spring-beans spring-core spring-expression 四个包 -->
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/7487.html