依赖的生命周期

通过scope标签可以设定依赖的生命周期,各类型对应生命周期如下。
image.png

依赖管理

依赖传递的冲突问题

  • 路径优先:当依赖中出现相同的资源时,层级越深,优先级越低,反之越高。
  • 声明优先:当资源在同级被依赖时,配置顺序靠前的依赖覆盖顺序靠后的。
  • 特殊优先:当配置了相同资源的不同版本,配置靠后的覆盖前面的。

    可选依赖

    使用true|false来控制依赖是否必须

  • true:该依赖不会被上层知道,或者说依赖不会进行传递

  • false:默认值,依赖会进行传递

    多环境配置

    pom文件配置

    1. <profiles>
    2. <profile>
    3. <id>dev</id>
    4. <activation>
    5. <activeByDefault>true</activeByDefault>
    6. </activation>
    7. <properties>
    8. <mysql.host>127.0.0.1</mysql.host>
    9. </properties>
    10. </profile>
    11. <profile>
    12. <id>qa</id>
    13. <properties>
    14. <mysql.host>localhost</mysql.host>
    15. </properties>
    16. </profile>
    17. </profiles>
    18. <build>
    19. <resources>
    20. <resource>
    21. <directory>${project.basedir}/src/main/resources</directory>
    22. <filtering>true</filtering>
    23. </resource>
    24. </resources>
    25. </build>

    配置文件示例

    配置文件放在/src/main/resources目录下
    1. mysql.host=${mysql.host}

    多环境运行参数

    1. mvn package -P dev|qa