在一个大型Maven项目中,往往包含多个子项目,而这些子项目很有可能都引用了相同的依赖,所以可以在parent pom中进行依赖的设置,以便子项目进行继承,进而减少重复的设置。
例如在parent pom中添加spring-boot-starter-web依赖,那么所有子项目就都获得了这个依赖。
选择性继承org.springframework.boot spring-boot-starter-web
但是在有些情况下,有可能只有一部分子项目需要这个依赖,其他子项目是不需要的,这时候在parent pom中直接添加dependency就不合适了,因为它会被所有的子项目直接继承。如果我们需要选择性继承,则可以使用profile。
首先,我们在parent pom中添加一个profile,在profile中我们设置依赖。
web org.springframework.boot spring-boot-starter-web
接下来需要设置何时激活profile,来达到选择性继承的目的。
property activation的bugprofile的激活有很多种,一眼看上去最合适的就是property,官方文档上对它如下说明
property: The profile will activate if Maven detects a property (a value which can be dereferenced within the POM by ${name}) of the corresponding name=value pair.
那么假设如下的profile
web type web org.springframework.boot spring-boot-starter-web
那么在需要这个依赖的子项目中设置以下properties不就好了么?
web
然而事实证明想多了,这个properties只能是system property。
https://issues.apache.org/jira/browse/MNG-6851
这样只能考虑用file来进行选择性依赖了。
web ${basedir}/web org.springframework.boot spring-boot-starter-web
在需要依赖的子项目中建立web文件,就可以获得相关依赖了。



