maven的pom文件

9 篇文章 0 订阅
6 篇文章 0 订阅

maven项目中会有pom文件, 当新建项目时候, 需要添加我们需要的依赖包。所以整理了一份比较常用的依赖包的pom,方便以后新建项目或者添加依赖包时copy且快捷。不需要的依赖可以删掉,避免首次远程拉取失败和缩小项目打包大小。

<?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.yulisao</groupId>
    <artifactId>mydemo</artifactId>
    <packaging>war</packaging>
    <version>1.0.0-SNAPSHOT</version>


    <!-- pom文件的常量配置 下面需使用的地方用${标签名}进行引用  这一段非必须有 -->
    <properties>
        <project.name>yulisao</project.name> <!-- 项目名称 -->
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceencoding>UTF-8</project.build.sourceencoding> <!--使用utf-8编码-->
        <spring.version>4.3.14.RELEASE</spring.version> <!-- spring 版本 -->
        <mysql.version>5.1.42</mysql.version> <!-- mysql数据库版本 -->
        <sqlserver.version>4.1.0</sqlserver.version> <!-- sqlserver数据库版本 -->
        <!-- 需要定义变量就自行继续添加 -->
    </properties>


    <!-- 开启springboot的特性 继承parent或者引入dependencies-->
    <!-- 也表示当前pom继承了parent里面的pom 按住ctrl点击下面的parent即可进去查看-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>


    <!-- 自定义仓库地址 非必须 -->
   <!-- <repositories>
        <repository>
            <id>aliyun</id>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled> &lt;!&ndash; 允许下载releases版本 &ndash;&gt;
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories> &lt;!&ndash; pluginRepositories配置了就按配置执行,省略了没配置就按本地默认的执行 &ndash;&gt;
        <pluginRepository>
            <id>aliyun-plugin</id>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>-->


    <dependencies>
        <!--springboot框架所需-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.5.4.RELEASE</version>
        </dependency>

        <!--数据库支持-->
        <!-- 根据自己的数据库选择一个即可 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>${sqlserver.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>3.4.11</version>
        </dependency>


        <!-- 缓存相关 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>2.1.9.RELEASE</version>
        </dependency>


        <!-- 基础工具依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- Mybatis相关 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.6</version>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.9</version>
        </dependency>



        <!-- Spring相关 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>


        <!-- web相关 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.2</version>
        </dependency>


        <!-- 业务依赖包或者私有包 如有根据自己需要添加-->


        <!-- swagger2相关 非必须-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
    </dependencies>

    <!--构建打包相关-->
    <build>
        <finalName>${project.name}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <!-- 配置文件 项目中有哪些就在此处新增行配置-->
                    <!--<packagingExcludes>**/jdbc.properties</packagingExcludes>-->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <!-- 若项目结构不一样,这两个路径也要记得改,不然某文件明明存在却报错找不到 -->
                <directory>src/main/java</directory>
                <includes>
                    <!-- *是通配的,也可以按需把路径范围控制的更小 -->
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
</project>

pom结构说明解说如下

properties 定义全局变量的地方
parent 引入父依赖,类似java里面的extends关键字
repositories+pluginRepositories 配置远程仓库
dependencies 所需的依赖包
build 构建打包找谁来执行,需要用到哪些路径下的文件

maven运行流程
maven会根据pom文件拉取依赖包,根据群组id先是在我们本地仓库对应的路径去找,找不到就去远程仓库下载回来本地。远程仓库有中央仓库、其他仓库。下面这个是安装maven后配置文件里面的,它是所有pom的父pom,所有maven项目继承该配置。

<repositories>
    <repository>
      <id>central</id> <!-- 仓库的id,唯一不重复 (若重复则覆盖前者) -->
      <name>Central Repository</name> <!-- 仓库的名称 或者 描述 -->
      <url>https://repo.maven.apache.org/maven2</url> <!-- 仓库的地址 -->
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled> <!-- 禁止下载snapshots版本 -->
      </snapshots>
    </repository>
  </repositories>

上面只需稍微了解一下就行,下面需重点熟悉是个人配置。当某些依赖包存在于其他仓库、局域网仓库、私有仓库时。我就还需要配置这些非中央仓库告诉maven除了上面的中央仓库,你还可以去我提供的这些仓库里找。以阿里云仓库为例在pom文件中配置如下

<!-- 这里是仓库信息 -->
<repositories> 
    <repository>
        <id>aliyun</id>
        <url>https://maven.aliyun.com/repository/public</url>
        <releases>
            <enabled>true</enabled> <!-- 允许下载releases版本 -->
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<!-- 这里是执行者信息 -->
<pluginRepositories> <!-- pluginRepositories配置了就按配置执行,省略了没配置就按本地默认的执行 -->
    <pluginRepository>
        <id>aliyun-plugin</id>
        <url>https://maven.aliyun.com/repository/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

另外一种方法是maven的setting.xml文件里面配置,找到profiles标签。定义一个id为local的profile,使用activeProfiles标签表示当前生效的profile是local这个配置。

<settings>
  ...
  <profiles>
    <profile> <!-- 若你有多个就配置多套 这里只配置了一个为例-->
      <id>local</id>
      <!-- 把上面的repositories 和pluginRepositories 复制到这里来粘贴-->
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>local</activeProfile> <!-- 若你需同时使用多个仓库就配置多个 这里只配置了一个为例-->
  </activeProfiles>
  ...
</settings>

这两种配置方式优缺点如下,我认为第一种好一些,一劳永逸。而setting文件,我们一般都是去mirrors标签里面覆盖maven原有中央镜像以及添加其他镜像,极少去改profiles。

在项目中的pom中配置:pom会提交git/svn,这样新同事拉取代码直接能用你也不用给他交接setting.xml文件。一旦提交git了后续也不会无故去修改,稳定。
在setting.xml文件配置:是你本地全局范围的仓库配置,需要用哪个仓库时就切换到哪个,改一下配置即可。新项目不知道仓库或忘记切换或切换错了导致包拉取不下来,项目都跑不起来

parent标签

<parent>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version> <!-- 必须指定一个版本,不可省略-->
</parent>

这一段表示当前pom继承parent,parent里面的父pom包含了很多东西的(它也都是继承 来自spring-boot-dependencies)。父pom里面的properties标签下,定义了很多包的版本,属于定制套餐。我们只需要引入spring-boot-start-parent后很多其他依赖就不用在引用了,因为spring-boot-start-parent这个文件中的套餐将我们需要的所有依赖都准备好了。这个套餐里面具体有哪些依赖包,参照下图可去查看。
在这里插入图片描述
但假设我们不使用父pom来实现Spring boot,或者父pom里面的某个jar版本不是我想要的又如何替换呢。在当前pom里面添加如下

<dependencyManagement>
     <dependencies>
     	<!-- 情景一:不使用父pom的定制套餐,先添加这个依赖,然后根据自己需要再加一些你需要的依赖  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- 继续添加你项目中需要用到的依赖,毕竟没使用定制套餐了,啥都需要你自己动手 略 -->
        <!-- 另外当前pom里面的build标签内容就不能省略了,因为父pom里本身有build标签,现在不使用父pom了,build标签自然得你自己写 -->
        
        
        <!-- 情景二:不同版本可以在dependencyManagement中重写 -->
        <!-- 比如jpa这个我需要的1.5.5版本但父pom里并不是这个版本,这样就能用1.5.5进行覆盖 -->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.5.5.RELEASE</version>
        </dependency>
    </dependencies>
</dependencyManagement>

在这里插入图片描述
这种左边有蓝色圈圈的,说明父pom中已引入了,你无需再引入。除非你需要对这个依赖进行自定义(版本、作用范围等)并覆盖

jar查询下载工具
依赖包的名称,有哪些版本,可以通过这个网址去查询 https://mvnrepository.com/,它提供了官网地址下载jar,引入maven和grade文件中依赖的代码
在这里插入图片描述
或者通过阿里云地址 https://developer.aliyun.com/mvn/search
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值