pom.xml 文件依赖配置详解

在maven和springboot 项目中pom.xml 十分重要,平常我们经常使用pom.xml导入依赖功能。但实际pom.xml 具有很多功能,在这里加以详细记录。我们使用POM 文件来对完成对maven项目的管理,构建等操作。

一 pom 文件结点
pom.xml文件的节点大致可以分为以下几个部分:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
            http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 基本配置 -->
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    <name>...<name>
    <packaging>...</packaging>

    <!-- 依赖配置 -->
    <dependencies>...</dependencies>
    <parent>...</parent>
    <dependencyManagement>...</dependencyManagement>
    <modules>...</modules>
    <properties>...</properties>

    <!-- 构建配置 -->
    <build>...</build>
    <reporting>...</reporting>

    <!-- 项目信息 -->
    <name>...</name>
    <description>...</description>
    <url>...</url>
    <inceptionYear>...</inceptionYear>
    <licenses>...</licenses>
    <organization>...</organization>
    <developers>...</developers>
    <contributors>...</contributors>

    <!-- 环境设置 -->
    <issueManagement>...</issueManagement>
    <ciManagement>...</ciManagement>
    <mailingLists>...</mailingLists>
    <scm>...</scm>
    <prerequisites>...</prerequisites>
    <repositories>...</repositories>
    <pluginRepositories>...</pluginRepositories>
    <distributionManagement>...</distributionManagement>
    <profiles>...</profiles>
</project>

这里重点讲解依赖配置(也是我们经常使用的)

1 dependencies:项目相关依赖,如果父项目中的依赖,会被子项目引用,所以一般在父项目中定义子项目中共有的依赖。并且如果有需要,子项目可以修改所依赖包的版本:

			<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

exclusions和exclusion属性,用于移除项目依赖相关,如果项目X需要A,而A包含B依赖,那么X可以声明不要B依赖,只要在exclusions中声明exclusion,将B从依赖树中删除即可。这一般用于解决jar包冲突的问题;

  <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

        <exclusions>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

2 parent:用于确定父项目的坐标位置。

 <parent>
        <artifactId>onlineschool_parent</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

3 dependencyManagement:用于帮助管理children的依赖。例如如果parent使用dependencyManagement定义了一个junit:junit4.0,那么它的children就可以只引用 groupId和artifactId,而version就可以直接使用父模块的,如果有需要,我们也可以设置version,这样的好处就是可以集中管理依赖的详情,并且也更灵活。
  不过dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。配置同dependencies类似;

<dependencyManagement>
    <dependencies>
    <!--Spring Cloud-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>2020.0.0</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    <dependencies>
<depemdencyManagement>

4 properties:该标签用于定义pom文件中的常量,这样,在pom文件的任何地方,都可以通过${java.version}来引用该值

<properties>
        <java.version>1.8</java.version>
        <mybatis-plus.version>3.0.7</mybatis-plus.version>
        <!--        <mybatis-plus.version>3.0.5</mybatis-plus.version>  用不了低版本不支持在注解中这样写ew.customSqlSegment,只支持xml的自定义sql-->
        <velocity.version>2.0</velocity.version>
        <swagger.version>2.7.0</swagger.version>
        <aliyun.oss.version>3.1.0</aliyun.oss.version>
        <!--        <aliyun.oss.version>2.8.3</aliyun.oss.version>-->
        <jodatime.version>2.10.1</jodatime.version>
        <poi.version>3.17</poi.version>
    </properties>
   <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>

参考:
https://www.jianshu.com/p/fddf23240f81

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pom.xmlMaven项目的核心配置文件,它描述了项目的基本信息、依赖关系、构建方式等。下面是一个简单的pom.xml文件示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 项目基本信息 --> <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0-SNAPSHOT</version> <!-- 依赖关系 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <!-- 构建方式 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project> ``` 上述示例中,`<project>`元素是根元素,包含了整个pom.xml文件的内容。`<modelVersion>`元素指定了POM模型的版本,当前为4.0.0。 `<groupId>`、`<artifactId>`和`<version>`元素组成了Maven项目的坐标信息,用于唯一标识一个项目。其中,`<groupId>`表示项目所属的组织或公司,`<artifactId>`表示项目名称,`<version>`表示项目版本号。 `<dependencies>`元素包含了项目依赖的所有库文件及其版本号等信息。在示例中,我们定义了一个JUnit测试库的依赖关系。 `<build>`元素包含了构建相关的配置信息,例如编译器插件、打包方式等。在示例中,我们使用了maven-compiler-plugin插件来指定Java源代码的编译版本。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值