前言
Maven 是一個(gè)項(xiàng)目管理工具,可以對(duì) Java 項(xiàng)目進(jìn)行構(gòu)建和管理依賴。
本文,我們認(rèn)識(shí)下 pom.xml 文件。POM(Project Object Model, 項(xiàng)目對(duì)象模型) 是 Maven 工程的基本工作單位,也是 Maven 的核心。其包含項(xiàng)目的基本信息,用于描述項(xiàng)目如何構(gòu)建、聲明項(xiàng)目依賴等。
什么是 pom?
POM 是 Project Object Model 的縮寫,即項(xiàng)目對(duì)象模型。
pom.xml 就是 maven 的配置文件,用以描述項(xiàng)目的各種信息。
pom配置一覽
<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>
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!-- Build Settings -->
<build>...</build>
<reporting>...</reporting>
<!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
<!-- Environment Settings -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>
1. dependencies
在該元素下添加依賴,可以包含多個(gè) 依賴:
<dependencies>
<dependency></dependency>
<dependency></dependency>
</dependencies>
之間有三個(gè)標(biāo)識(shí):
- groupId: 定義隸屬的實(shí)際項(xiàng)目
- artifactId: 定義項(xiàng)目中的一個(gè)模塊
- version: 依賴或者項(xiàng)目的版本
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-start-web</artifactId>
</dependency>
2.scope
如果在編譯的時(shí)候需要而在發(fā)布的時(shí)候不需要的 JAR 包,則可以使用 scope 標(biāo)簽標(biāo)記該包,并將值設(shè)置為 provided。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
- compile:scope 的默認(rèn)值,表示該依賴項(xiàng)目需要參與當(dāng)前項(xiàng)目的編譯、測(cè)試、運(yùn)行階段,是比較強(qiáng)的依賴。打包時(shí)也要包含進(jìn)去。
- provided:上面提到??
- runtime:會(huì)作用在運(yùn)行和測(cè)試階段。
- system:和 provided 相似,但是在系統(tǒng)中要以外部 JAR 包的形式提供,Maven 不會(huì)在 repository 中查找它。
- test:會(huì)作用在測(cè)試階段。
3.properties
在 <properties></properties>
中自定義變量,方便在依賴配置時(shí)引用變量,可達(dá)到統(tǒng)一版本號(hào)的目的。比如:
<properties>
<java.version>1.8.0</java.version>
<solr.version>8.0.0</solr.version>
</properties>
通過 ${變量名}
來調(diào)用:文章來源:http://www.zghlxwxcb.cn/news/detail-616385.html
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>${solr.version}</version>
</dependency>
4.plugin
在創(chuàng)建 Spring Boot 項(xiàng)目的時(shí),默認(rèn)提供了 spring-boot-maven-plugin 插件。它提供打包時(shí)需要的信息,將 Spring Boot 應(yīng)用打包為可執(zhí)行的 JAR 或者 WAR 文件。文章來源地址http://www.zghlxwxcb.cn/news/detail-616385.html
pom.xml 類比 package.json
參考
- Maven POM
- 《Spring Boot 實(shí)戰(zhàn)派》
到了這里,關(guān)于【Maven】Maven 中 pom.xml 文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!