在整个项目中,我们要使用分模块的方式进行开发,如果是分模块开发的话,我们会要需要用到别人编写的模块,我们就需要导入别人编写的模块的坐标
<!--在每一个pom.xml中的第一个坐标就是当前模块的坐标-->
<groupId>com.cyf</groupId>
<artifactId>spring_demo_dao</artifactId>
<version>1.0-SNAPSHOT</version>
如果其他模块需要使用到此模块,我们需要先将此模块下载到maven本地仓库中
pom.xml中导入坐标
<dependency>
<!--在每一个pom.xml中的第一个坐标就是当前模块的坐标-->
<groupId>com.cyf</groupId>
<artifactId>spring_demo_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
可选依赖
在别人导入我们的模块坐标时,我们不想让别人使用我们模块所用的依赖,我们可以将自己的模块设置可选依赖,也就是将自己模块的依赖全部设置为不透明。
dependency>
<groupId>com.cyf</groupId>
<artifactId>spring_demo_dao</artifactId>
<version>1.0-SNAPSHOT</version>
<!--可选依赖是隐藏当前工程所依赖的资源,隐藏后对应资源将不具有依赖传递性-->
<optional>true</optional>
</dependency>
排除依赖
我们在使用别人的模块时,别人使用到依赖和我们自己使用的依赖会产生冲突导致报错,我们就可以在导入别人模块坐标时排除掉别人使用的依赖
<dependency>
<groupId>com.cyf</groupId>
<artifactId>spring_demo_dao</artifactId>
<version>1.0-SNAPSHOT</version>
<!--排除依赖是隐藏当前资源对应的依赖关系-->
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency>
聚合
创建一个父类模块
打包方式设置为pom
<packaging>pom</packaging>
在配置文件中写上聚合工程包含的子模块名称
<!--子模块-->
<modules>
<module>spring_controller</module>
<module>spring_service</module>
</modules>
继承
在父类pom文件中配置的依赖子类将直接沿用
<!--定义该工程的父工程-->
<parent>
<groupId>com.cyf</groupId>
<artifactId>spring_maven_parent</artifactId>
<version>1.0-SNAPSHOT</version> <!--填写父工程的pom文件,根据实际情况填写-->
<relativePath>../maven_parent/pom.xml</relativePath>
</parent>
因为不是所有模块的都需要使用所有的依赖,所有可以设置可选依赖
<!--可选依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
如果在子类模块中需要使用到可选的依赖就只要写<groupId>和<artifactId>版本号就不用写了
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
</dependencies>
公共依赖,就是所有模块都需要使用到的依赖
<!--公共依赖-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
</dependencies>
属性管理
依赖引用属性
如果很多依赖版本号相同的话,在我们要修改版本的时候就会造成极大的工程量,也可能写错,所有我们可以定义一个属性,用来管理所有相同的版本号
<properties>
<spring.version>5.2.10.RELEASE</spring.version>
</properties>
在依赖中引用属性
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<!--应用属性使用占位符-->
<version>${spring.version}</version>
</dependency>
</dependencies>
资源文件引用属性
对于一些资源文件,也可以使用引用属性的方式
<properties>
<jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
</properties>
配置文件
jdbc.driver=${jdbc.driver}
还需要开启资源过滤器
<!--开启资源目录加载属性的过滤器-->
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
在打包时会经过web.xml检查,配置maven打包war时,可以忽略检查
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
版本管理
SNAPSHOT(快照版本):也就是测试版,项目开发过程中临时输出的版本
RELEASE(发布版本):正式版,发布较为稳定的版本
多环境配置
<!--定义多环境-->
<profiles>
<!--定义具体的环境-->
<profile>
<!--定义环境名称-->
<id>env_dep</id>
<!--定义环境中使用的属性值-->
<properties>
<jdbc.url>jdbc:mysql://127.0.0.1:3306/mybatis</jdbc.url>
</properties>
<!--设置默认启动-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
里面可以继续添加<profile>定义更多的开发环境,因为我们编写的项目不可能只在一个环境中运行
跳过测试环境
细粒度控制跳过测试
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skipTests>true</skipTests>
<!--设置跳过测试-->
<includes>
<!--包含指定的测试用例-->
<include>**/User*Test.java</include>
</includes>
<excludes>
<!--排除指定的测试用例-->
<exclude>**/User*TestCase.java</exclude>
</excludes>
</configuration>
</plugin>
私服
Nexus安装和启动
启动命令(cmd启动)
nexus.exe /run nexu
服务器端口号为8081
需要在maven的setting.xml中<mirrors>标签中配置配置,需要注销掉原来的aliyun配置
配置本地仓库访问私服权限
同样是在setting中的<server>标签配置
<server>
<!--id任意,多个server的id不重复就行,后面会用到-->
<id>cyf-release</id>
<username>admin</username>
<password>admin</password><!--填写自己nexus设定的登录秘密-->
</server>
<server>
<id>cyf-snapshot</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
在pom.xml中配置当前项目访问私服上传的资源保存位置
<distributionManagement>
<repository>
<!--和maven/settings.xml中server中的id一致,表示使用该id对应的用户名和密码-->
<id>cyf-release</id>
<!--如果jar的版本是release版本,那么就上传到这个仓库,根据自己情况修改-->
<url>http://localhost:8081/repository/cyf-release/</url>
</repository>
<snapshotRepository>
<id>cyf-snapshot</id>
<!--如果jar的版本是snapshot版本,那么就上传到这个仓库,根据自己情况修改-->
<url>http://localhost:8081/repository/cyf-snapshot/</url>
</snapshotRepository>
</distributionManagement>
使用deploy命令上传