Maven项目pom.xml文件解析及父子项目依赖关系总结

pom.xml文件常见结构:

<?xml version="1.0" encoding="UTF-8">
<project xmlns=...
        xmlns:xsi=...
        xsi:schemaLocation=...>
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <packaging>pom</packaging>
    <version>...</version>
    <!-- 这是(IDEA)创建工程时(根据自己输入)即定义好的内容 -->
    <!-- packaging是本工程产生的类型例如jar pom war,默认类型是jar

<!-----------------------=---------我是分割线-------------------------------------------->

    <dependencies>
        <dependency>
            <groupId></groupId>
            <artifactId></artifactId>
            <version></version>
        </dependency>
        ...
        <!-- 在dependencies这组标签内添加本工程的依赖jar包 -->
    </dependencies>

<!-----------------------=---------我是分割线-------------------------------------------->
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId></groupId>
                <artifactId></artifactId>
                <version></version>
                <type></type>
                <scope></scope>
            </dependency>
        </dependencies>
        <!-- 在dependencyManagement这组标签内添加的依赖包不会引入到本工程,通常用于父工程,方便管理子工程的依赖包,在本项目使用仍需在上面的dependencies标签中显式声明(可以不加版本号)-->
    </dependencyManagement>
        
    
</project>

当出现父子关系的项目时,父项目的pom.xml文件中会有<modules><module>son</module></modules>标签,子项目中会有<parent><artifactId></artifactId><groupId></groupId><version></version></parent>标签。


注意事项:

  1.         父项目dependencies中的所有的依赖项都会被子项目继承
  2.         父项目dependencyManagement中的依赖项不会被继承,子项目若要使用需要显式声明(可以不加版本号)默认使用父项目中的版本
  3.         子项目中也有dependencyManagement(需要指定版本号),相同依赖项会使用子项目中的。

实践:

        在创建工程的时候,可能发现dependencyManagement中的依赖项有<type>pom</type><scope>import</scope>,我在父项目中添加依赖包的时候也跟着加,结果子项目中显式声明(不使用版本号)依赖就是添加不上,提示unknown版本号未知。

        这个<type>pom</type><scope>import</scope>是dependencyManagement中特有的选项,使用这组标签,代表从目标pom文件中dependencyManagement的配置导入合并到当前pom的dependencyManagement中。

Apache Maven介绍中有关import的原句:

This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency is to be replaced with the effective list of dependencies in the specified POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

        最后一句说的是:“由于它们被替换,具有导入范围的依赖项实际上并不参与限制依赖项的传递性。”,我个人实践后理解的是,使用<type>pom</type><scope>import</scope>标签后,这个依赖项就不会被传递给子项目(子项目中显式声明)。

        但还有其他情况不指定版本号,仍能添加上依赖,比如alibaba的fastjson不添加版本号仍能添加上,它会有默认的版本。我误认为是从父项目dependencyManagement中继承的依赖。 


参考文章和链接:

Maven Model – Maven

Maven – Introduction to the Dependency Mechanism

Java-Maven常见的依赖范围_了解->熟悉->掌握->精通-CSDN博客


开发错误总结:

①SpringBoot启动类要放在包内

②Service层的@Service注解要添加在Impl实现类上

③数据库表对应实体类int类型要使用Integer包装类

④Mapper层SQL语句使用的参数要添加@Param("arg")注解

⑤数据库表和字段避免使用保留关键字,如 key、order,若要使用得反引号``包裹

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值