maven項(xiàng)目中會(huì)有pom文件, 當(dāng)新建項(xiàng)目時(shí)候, 需要添加我們需要的依賴包。所以整理了一份比較常用的依賴包的pom,方便以后新建項(xiàng)目或者添加依賴包時(shí)copy且快捷。不需要的依賴可以刪掉,避免首次遠(yuǎn)程拉取失敗和縮小項(xiàng)目打包大小。
<?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文件的常量配置 下面需使用的地方用${標(biāo)簽名}進(jìn)行引用 這一段非必須有 -->
<properties>
<project.name>yulisao</project.name> <!-- 項(xiàng)目名稱 -->
<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數(shù)據(jù)庫版本 -->
<sqlserver.version>4.1.0</sqlserver.version> <!-- sqlserver數(shù)據(jù)庫版本 -->
<!-- 需要定義變量就自行繼續(xù)添加 -->
</properties>
<!-- 開啟springboot的特性 繼承parent或者引入dependencies-->
<!-- 也表示當(dāng)前pom繼承了parent里面的pom 按住ctrl點(diǎn)擊下面的parent即可進(jìn)去查看-->
<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> <!– 允許下載releases版本 –>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories> <!– pluginRepositories配置了就按配置執(zhí)行,省略了沒配置就按本地默認(rèn)的執(zhí)行 –>
<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>
<!--數(shù)據(jù)庫支持-->
<!-- 根據(jù)自己的數(shù)據(jù)庫選擇一個(gè)即可 -->
<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>
<!-- 緩存相關(guān) -->
<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>
<!-- 基礎(chǔ)工具依賴 -->
<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相關(guān) -->
<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相關(guān) -->
<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相關(guān) -->
<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>
<!-- 業(yè)務(wù)依賴包或者私有包 如有根據(jù)自己需要添加-->
<!-- swagger2相關(guān) 非必須-->
<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>
<!--構(gòu)建打包相關(guān)-->
<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>
<!-- 配置文件 項(xiàng)目中有哪些就在此處新增行配置-->
<!--<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>
<!-- 若項(xiàng)目結(jié)構(gòu)不一樣,這兩個(gè)路徑也要記得改,不然某文件明明存在卻報(bào)錯(cuò)找不到 -->
<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結(jié)構(gòu)說明解說如下
properties 定義全局變量的地方
parent 引入父依賴,類似java里面的extends關(guān)鍵字
repositories+pluginRepositories 配置遠(yuǎn)程倉庫
dependencies 所需的依賴包
build 構(gòu)建打包找誰來執(zhí)行,需要用到哪些路徑下的文件
maven運(yùn)行流程:
maven會(huì)根據(jù)pom文件拉取依賴包,根據(jù)群組id先是在我們本地倉庫對(duì)應(yīng)的路徑去找,找不到就去遠(yuǎn)程倉庫下載回來本地。遠(yuǎn)程倉庫有中央倉庫、其他倉庫。下面這個(gè)是安裝maven后配置文件里面的,它是所有pom的父pom,所有maven項(xiàng)目繼承該配置。
<repositories>
<repository>
<id>central</id> <!-- 倉庫的id,唯一不重復(fù) (若重復(fù)則覆蓋前者) -->
<name>Central Repository</name> <!-- 倉庫的名稱 或者 描述 -->
<url>https://repo.maven.apache.org/maven2</url> <!-- 倉庫的地址 -->
<layout>default</layout>
<snapshots>
<enabled>false</enabled> <!-- 禁止下載snapshots版本 -->
</snapshots>
</repository>
</repositories>
上面只需稍微了解一下就行,下面需重點(diǎn)熟悉是個(gè)人配置。當(dāng)某些依賴包存在于其他倉庫、局域網(wǎng)倉庫、私有倉庫時(shí)。我就還需要配置這些非中央倉庫告訴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>
<!-- 這里是執(zhí)行者信息 -->
<pluginRepositories> <!-- pluginRepositories配置了就按配置執(zhí)行,省略了沒配置就按本地默認(rèn)的執(zhí)行 -->
<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標(biāo)簽。定義一個(gè)id為local的profile,使用activeProfiles標(biāo)簽表示當(dāng)前生效的profile是local這個(gè)配置。
<settings>
...
<profiles>
<profile> <!-- 若你有多個(gè)就配置多套 這里只配置了一個(gè)為例-->
<id>local</id>
<!-- 把上面的repositories 和pluginRepositories 復(fù)制到這里來粘貼-->
</profile>
</profiles>
<activeProfiles>
<activeProfile>local</activeProfile> <!-- 若你需同時(shí)使用多個(gè)倉庫就配置多個(gè) 這里只配置了一個(gè)為例-->
</activeProfiles>
...
</settings>
這兩種配置方式優(yōu)缺點(diǎn)如下,我認(rèn)為第一種好一些,一勞永逸。而setting文件,我們一般都是去mirrors標(biāo)簽里面覆蓋maven原有中央鏡像以及添加其他鏡像,極少去改profiles。
在項(xiàng)目中的pom中配置:pom會(huì)提交git/svn,這樣新同事拉取代碼直接能用你也不用給他交接setting.xml文件。一旦提交git了后續(xù)也不會(huì)無故去修改,穩(wěn)定。
在setting.xml文件配置:是你本地全局范圍的倉庫配置,需要用哪個(gè)倉庫時(shí)就切換到哪個(gè),改一下配置即可。新項(xiàng)目不知道倉庫或忘記切換或切換錯(cuò)了導(dǎo)致包拉取不下來,項(xiàng)目都跑不起來
parent標(biāo)簽
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version> <!-- 必須指定一個(gè)版本,不可省略-->
</parent>
這一段表示當(dāng)前pom繼承parent,parent里面的父pom包含了很多東西的(它也都是繼承 來自spring-boot-dependencies)。父pom里面的properties標(biāo)簽下,定義了很多包的版本,屬于定制套餐。我們只需要引入spring-boot-start-parent后很多其他依賴就不用在引用了,因?yàn)閟pring-boot-start-parent這個(gè)文件中的套餐將我們需要的所有依賴都準(zhǔn)備好了。這個(gè)套餐里面具體有哪些依賴包,參照下圖可去查看。
但假設(shè)我們不使用父pom來實(shí)現(xiàn)Spring boot,或者父pom里面的某個(gè)jar版本不是我想要的又如何替換呢。在當(dāng)前pom里面添加如下
<dependencyManagement>
<dependencies>
<!-- 情景一:不使用父pom的定制套餐,先添加這個(gè)依賴,然后根據(jù)自己需要再加一些你需要的依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 繼續(xù)添加你項(xiàng)目中需要用到的依賴,畢竟沒使用定制套餐了,啥都需要你自己動(dòng)手 略 -->
<!-- 另外當(dāng)前pom里面的build標(biāo)簽內(nèi)容就不能省略了,因?yàn)楦竝om里本身有build標(biāo)簽,現(xiàn)在不使用父pom了,build標(biāo)簽自然得你自己寫 -->
<!-- 情景二:不同版本可以在dependencyManagement中重寫 -->
<!-- 比如jpa這個(gè)我需要的1.5.5版本但父pom里并不是這個(gè)版本,這樣就能用1.5.5進(jìn)行覆蓋 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.5.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
這種左邊有藍(lán)色圈圈的,說明父pom中已引入了,你無需再引入。除非你需要對(duì)這個(gè)依賴進(jìn)行自定義(版本、作用范圍等)并覆蓋文章來源:http://www.zghlxwxcb.cn/news/detail-475974.html
jar查詢下載工具
依賴包的名稱,有哪些版本,可以通過這個(gè)網(wǎng)址去查詢 https://mvnrepository.com/,它提供了官網(wǎng)地址下載jar,引入maven和grade文件中依賴的代碼
或者通過阿里云地址 https://developer.aliyun.com/mvn/search文章來源地址http://www.zghlxwxcb.cn/news/detail-475974.html
到了這里,關(guān)于maven的pom文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!