目录
分模块开发
按照功能将项目进行拆分,每一层都可以单独维护,也可以很方便的被别人使用
银行项目:银行柜台、取款机、网站、手机app对应不同的场景,因此需要按照功能拆分
按照模块拆分:
如电商的项目中,有订单和商品两个模块,订单中需要包含商品的详细信息,所以需要商品的模型类,商品模块也会用到商品的模型类,这个时候如果两个模块中都写模型类,就会出现重复代码,后期的维护成本就比较高。我们就想能不能将它们公共的部分抽取成一个独立的模块,其他模块要想使用可以像添加第三方jar包依赖一样来使用我们自己抽取的模块,这样就解决了代码重复的问题,这种拆分方式就说我们所说的按照模块拆分
(4)修改原项目的pom.xml文件添加domain项目的坐标进行jar包的引入
<dependencies><dependency><groupId> com.itheima </groupId><artifactId> maven_03_pojo </artifactId><version> 1.0-SNAPSHOT </version></dependency></dependencies>
依赖传递与冲突
<dependency><groupId> com.itheima </groupId><artifactId> maven_03_pojo </artifactId><version> 1.0-SNAPSHOT </version><!-- 可选依赖是隐藏当前工程所依赖的资源,隐藏后对应资源将不具有依赖传递 --><optional> true </optional></dependency>
依赖范围
依赖的jar默认情况可以在任何地方使用,可以通过scope标签设定其使用范围
作用范围:
- 主程序范围内有效(main文件夹范围内)
- 测试程序范围有效(test文件夹范围内
- 是否参与打包(package指令范围内)
聚合
聚合工程:通常是一个不具有业务功能的"空"工程(有且仅有一个pom文件),将多个模块组织成一个整体,同时进行项目构建;
项目打包方式为pom:说明该项目为聚合或继承项目
<groupId> com.itheima </groupId><artifactId> maven_01_parent </artifactId><version> 1.0-RELEASE </version><packaging> pom </packaging><!-- 设置管理的模块名称 --><modules><module> ../maven_02_ssm </module><module> ../maven_03_pojo </module><module> ../maven_04_dao </module><modules>
继承
<!-- 配置当前工程继承自 parent 工程 --><parent><groupId> com.itheima </groupId><artifactId> maven_01_parent </artifactId><version> 1.0-RELEASE </version><!--设置父项目 pom.xml 位置路径,可以不写 --><relativePath> ../maven_01_parent/pom.xml </relativePath></parent><dependencies>......</ dependencies >1 <!--定义依赖管理, <dependencyManagement> 标签不真正引入jar包,而是配置可供子项目选择的jar包依赖, 子项目要想使用它所提供的这些jar包,需要自己添加依赖,并且不需要指定<version>
-->2 <dependencyManagement>3 <dependencies>4 <dependency>5 <groupId> junit </groupId>6 <artifactId> junit </artifactId>7 <version> 4.12 </version>8 <scope> test </scope>9 </dependency>10 </dependencies>11 </dependencyManagement>当 dependencyManagement 标签中 jar 包版本发生变化,所有子项目中有用到该 jar 包的地方对应的版本会自动随之更新,实现版本管理,方便维护
- 聚合用于快速构建项目,对项目进行管理
- 继承用于快速配置和管理子项目中所使用jar包的版本
- 聚合与继承的pom.xml文件打包方式均为pom,可以将两种关系制作到同一个pom文件中
- 聚合与继承均属于设计型模块,并无实际的模块内容
- 聚合是在当前模块中配置关系,聚合可以感知到参与聚合的模块有哪些
- 继承是在子模块中配置关系,父模块无法感知哪些子模块继承了自己
属性
<!-- 定义属性 --><properties><spring.version> 5.2.10.RELEASE </spring.version><junit.version> 4.12 </junit.version><mybatis-spring.version> 1.3.0 </mybatis-spring.version><jdbc.url> jdbc:mysql://127.1.1.1:3306/ssm_db </jdbc.url></properties><dependency><groupId> org.springframework </groupId><artifactId> spring-jdbc </artifactId><version> ${spring.version} </version> <!--用${key}来获取--></dependency>1 <build>2 <resources>3 <!-- 设置资源目录,并设置能够解析 ${}-->4 <resource>5 <directory> ../maven_02_ssm/src/main/resources </directory>6 <filtering> true </filtering>7 </resource>8 <resource>9 <directory> ../maven_03_pojo/src/main/resources </directory>10 <filtering> true </filtering>11 </resource>12 ...13 </resources>14 </build>
jdbc.properties配置文件中也可以引用maven属性 ,将jdbc.url的值直接获取Maven配置的属性
jdbc.url = ${jdbc.url}
多环境
两种方式实现不动文件快速切环境
- 父工程配置多个环境,并指定默认激活环境
- 命令行实现环境切换mvn install -P env_dep
<profiles>
<!--开发环境-->
<profile>
<id>env_dep</id>
<properties>
<jdbc.url>jdbc:mysql://127.1.1.1:3306/ssm_db</jdbc.url>
</properties>
<!--设定是否为默认启动环境-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--生产环境-->
<profile>
<id>env_pro</id>
<properties>
<jdbc.url>jdbc:mysql://127.2.2.2:3306/ssm_db</jdbc.url>
</properties>
</profile>
<!--测试环境-->
<profile>
<id>env_test</id>
<properties>
<jdbc.url>jdbc:mysql://127.3.3.3:3306/ssm_db</jdbc.url>
</properties>
</profile>
</profiles>
跳过测试
- IDEA工具实现跳过测试
-
配置插件实现跳过测试
<build><plugins><plugin><artifactId> maven-surefire-plugin </artifactId><version> 2.12.4 </version><configuration><skipTests> false </skipTests><!--排除掉不参与测试的内容--><excludes><exclude> **/BookServiceTest.java </exclude></excludes></configuration></plugin></plugins></build>
3.命令行跳过测试
私服
企业内部搭建一个类似于中央仓库的东西,把自己的内容上传上去,其他人就可以从上面下载jar包使用,用于解决团队内部的资源共享与资源同步问题
工具:Nexus,JFrog
以nexus为例,进入nexus-3.30.1-01\bin目录执行nexus.exe /run nexus,浏览器访问http://localhost:8081
<!--配置本地Maven对私服的访问权限-->
<servers>
<server>
<id>itheima-snapshot</id>
<username>admin</username>
<password>admin</password>
</server>
<server>
<id>itheima-release</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
<!--配置私服的访问路径-->
<mirrors>
<mirror>
<!--配置仓库组的ID-->
<id>maven-public</id>
<!--*代表所有内容都从私服获取-->
<mirrorOf>*</mirrorOf>
<!--私服仓库组maven-public的访问路径-->
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
- 配置工程上传私服的具体位置
<!--配置当前工程保存在私服中的具体位置-->
<distributionManagement>
<repository>
<!--和maven/settings.xml中server中的id一致,表示使用该id对应的用户名和密码--
>
<id>itheima-release</id>
<!--release版本上传仓库的具体地址-->
<url>http://localhost:8081/repository/itheima-release/</url>
</repository>
<snapshotRepository>
<!--和maven/settings.xml中server中的id一致,表示使用该id对应的用户名和密码--
>
<id>itheima-snapshot</id>
<!--snapshot版本上传仓库的具体地址-->
<url>http://localhost:8081/repository/itheima-snapshot/</url>
</snapshotRepository>
</distributionManagement>
2.发布资源到私服
mvn deploy