maven学习笔记(三) maven的坐标

时间:2022-01-17 09:36:13

maven坐标是什么

maven仓库的作用是存放各种构件,而仓库里放了这么多东西要使用的时候就需要坐标来找到自己需要的东西,比如我们去图书馆找书,会先查询自己想要的书,电脑会显示你要找的书是xxx馆,xxx区,xxx书架标号为xxx的书。这样我们直接过去拿就可以了,maven仓库的坐标也是如此。maven项目将所需要jar坐标写在配置文档的pom.xml文件里,每个maven的根目录下都有一个pom.xml文件,maven就会根据坐标自动寻找jar。举个例子。

   <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>

根据坐标在仓库中找到对应的jar包

上面的配置就是一个坐标,然后我们到仓库去找。

maven学习笔记(三) maven的坐标

可以看见在仓库的org/sl4j/slf4j-api文件夹下面。这就是根据坐标寻找jar的方法。

坐标标签含义

groupId :当前项目的实际项目名。
artifactId:项目对应的一个具体模块。
version:当前项目所处的版本。
比如:

      <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>

在上面的配置文档中,用到了springframework项目下的三个模块,分别是:context、webmvc、jdbc。版本都是3.2.4.RELEASE。这种情况是很多的。有时候我们不知道自己的jar包到底属于哪个项目,我们也可以查询,有很多网站可以查询jar的坐标,比如https://mvnrepository.com/.输入要查询的jar包即可,比如junit,会返回许多版本,选择版本就会告诉你坐标:

maven学习笔记(三) maven的坐标

值得一提的是,很多时候我们用的spring版本都是一样的,如果后期维护或者升级要改版本还是比较麻烦,我们可以在pom.xml中设置properties标签。比如:

        <properties>
<spring.version>3.2.4.RELEASE</spring.version>
</properties>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

这样后期版本更新就会很方便