一、pom文件最佳实践
1、最好是如下这种格式
1)整个项目设置一个父pom,每个子模块一个独立的子pom
2)父pom中,通过标签,为所有依赖设置版本;
子pom中,无需写版本(可以写,但是项目会采用父pom的版本)
2、如果每个模块都有区别于其他模块的依赖
这种情况也是最好采用父子pom的形式,只不过每个模块一组父子pom
3、dependencyManagement标签的用处
dependencyManagement除了上面的,可以限制所有子pom版本的作用外;
还有一个很重要的使用场景:
有时候一个大的依赖,对于其多个子包,有的不需要or不能用,这时候需要把父包的某个子包剔除掉;
剔除的方法之一是,先在父包那里通过exclusion标签把子包摘除掉,再单独引入需要的子包(如果需要),例如
<dependency>
<groupId>com.huawei.liveapps</groupId>
<artifactId>miniapp-spring-boot-starter</artifactId>
<version>${liveapps-app.version}</version>
<exclusions>
<exclusion>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>6.1.0</version>
</dependency>
但这个方法操作起来比较麻烦,格式也不是很简洁,而且后来人可能发现不了前人的意图;
剔除的第二个方法是,直接在dependencyManagement限制子包的版本,这时父包哪里不用作任何操作,父包中引用的子包的版本也会被限制到dependencyManagement那个版本,例如
<dependency>
<groupId>com.huawei.liveapps</groupId>
<artifactId>miniapp-spring-boot-starter</artifactId>
<version>${liveapps-app.version}</version>
</dependency>
在dependencyManagement标签中对子包作版本限制
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>6.1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
这样项目的pagehelper的依赖就会以6.0.1为准,并不需要格外对父包作剔除操作。