1、pom.xml 配置文件简介
POM 代表项目对象模型。它是 Maven 中工作的基本单位,这是一个 XML 文件。它始终保存在该项目基本目录中的 pom.xml
文件。
POM 文件主要描述了项目:配置文件,开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以及其他所有的项目相关因素。
在执行任务或目标时,Maven 会使用当前目录中的 POM。
一个Maven项目,在POM中配置坐标,标识项目在仓库的唯一性。
2、项目中 Maven 使用 pom.xml
最简单的POM配置文件
1 2 3 4 5 6 7 8 9
| <?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.tengj</groupId> <artifactId>springBootDemo1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springBootDemo1</name> </project>
|
XML 中最重要的是包含了groupId,artifactId和version,这三个是必填项。这三个元素定义了一个项目基本的坐标,在Maven的世界,任何的jar、pom或者jar都是以基于这些基本的坐标进行区分的。
3、pom 文件结点概览
pom.xml文件的节点大致分为以下几个部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <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>
|
4、POM 元素节点说明
4.1、parent 节点
1 2 3 4 5 6 7 8 9 10 11 12
| <parent> <artifactId/> <groupId/> <version/>
<relativePath/> </parent>
|
4.2、项目工程相关节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <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.flepeng</groupId>
<artifactId>demo</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>demo-maven</name> <url>https://www.flepeng.com</url>
<description>maven pjoect Demo.</description> </project>
|
4.3、prerequisites 节点
1 2 3 4 5
| <prerequisites> <maven/> </prerequisites>
|
4.4、issueManagement 节点
1 2 3 4 5 6 7
| <issueManagement> <system>jira</system> <url>http://jira.baidu.com/banseon</url> </issueManagement>
|
4.5、ciManagement 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <ciManagement> <system/> <url/> <notifiers> <notifier> <type/> <sendOnError/> <sendOnFailure/> <sendOnSuccess/> <sendOnWarning/> <address/> <configuration/> </notifier> </notifiers> </ciManagement>
|
4.6、mailingLists 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <mailingLists> <mailingList> <name>Demo</name> <post>flepeng@163.com</post> <subscribe>flepeng@163.com</subscribe> <unsubscribe>flepeng@163.com</unsubscribe> <archive>https://www.flepeng.com</archive> </mailingList> </mailingLists>
|
4.7、developers 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <developers> <developer> <id>HELLO WORLD</id> <name>FlyGo</name> <email>digu_test100@163.com</email> <url /> <roles> <role>Project Manager</role> <role>Architect</role> </roles> <organization>demo</organization> <organizationUrl>https://www.flepeng.com</organizationUrl> <properties> <dept>No</dept> </properties> <timezone>8</timezone> </developer> </developers>
|
4.8、contributors 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <contributors> <contributor> <name /> <email /> <url /> <organization /> <organizationUrl /> <roles /> <timezone /> <properties /> </contributor> </contributors>
|
4.9、licenses 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <licenses> <license> <name>Apache 2</name> <url>http://www.baidu.com/banseon/LICENSE-2.0.txt</url> <distribution>repo</distribution> <comments>A business-friendly OSS license</comments> </license> </licenses>
|
4.10、scm 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <scm> <connection> scm:svn:http://svn.sundaytech.cn/trunk </connection> <developerConnection> scm:svn:http://svn.sundaytech.cn/trunk </developerConnection> <tag /> <url>http://svn.http://svn.sundaytech.cn/trunk</url> </scm>
|
4.11、organization 节点
1 2 3 4 5 6 7
| <organization> <name>demo</name> <url>https://www.flepeng.com</url> </organization>
|
4.12、build 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| <build> <sourceDirectory /> <scriptSourceDirectory /> <testSourceDirectory /> <outputDirectory /> <testOutputDirectory /> <extensions> <extension> <groupId /> <artifactId /> <version /> </extension> </extensions> <defaultGoal /> <resources> <resource>
<targetPath /> <filtering /> <directory /> <includes /> <excludes /> </resource> </resources> <testResources> <testResource> <targetPath /> <filtering /> <directory /> <includes /> <excludes /> </testResource> </testResources> <directory /> <finalName /> <filters /> <pluginManagement> <plugins> <plugin> <groupId /> <artifactId /> <version /> <extensions /> <executions> <execution> <id /> <phase /> <goals /> <inherited /> <configuration /> </execution> </executions> <dependencies> <dependency> ...... </dependency> </dependencies> <inherited /> <configuration /> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId /> <artifactId /> <version /> <extensions /> <executions> <execution> <id /> <phase /> <goals /> <inherited /> <configuration /> </execution> </executions> <dependencies> <dependency> ...... </dependency> </dependencies> <goals /> <inherited /> <configuration /> </plugin> </plugins> </build>
|
4.13、profiles 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| <profiles> <profile> <id /> <activation> <activeByDefault /> <jdk /> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> <property> <name>mavenVersion</name> <value>2.0.3</value> </property> <file> <exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</exists> <missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</missing> </file> </activation> <build> <defaultGoal /> <resources> <resource> <targetPath /> <filtering /> <directory /> <includes /> <excludes /> </resource> </resources> <testResources> <testResource> <targetPath /> <filtering /> <directory /> <includes /> <excludes /> </testResource> </testResources> <directory /> <finalName /> <filters /> <pluginManagement> <plugins> <plugin> <groupId /> <artifactId /> <version /> <extensions /> <executions> <execution> <id /> <phase /> <goals /> <inherited /> <configuration /> </execution> </executions> <dependencies> <dependency> ...... </dependency> </dependencies> <goals /> <inherited /> <configuration /> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId /> <artifactId /> <version /> <extensions /> <executions> <execution> <id /> <phase /> <goals /> <inherited /> <configuration /> </execution> </executions> <dependencies> <dependency> ...... </dependency> </dependencies> <goals /> <inherited /> <configuration /> </plugin> </plugins> </build> <modules /> <repositories> <repository> <releases> <enabled /> <updatePolicy /> <checksumPolicy /> </releases> <snapshots> <enabled /> <updatePolicy /> <checksumPolicy /> </snapshots> <id /> <name /> <url /> <layout /> </repository> </repositories> <pluginRepositories> <pluginRepository> <releases> <enabled /> <updatePolicy /> <checksumPolicy /> </releases> <snapshots> <enabled /> <updatePolicy /> <checksumPolicy /> </snapshots> <id /> <name /> <url /> <layout /> </pluginRepository> </pluginRepositories> <dependencies> <dependency> ...... </dependency> </dependencies> <reports /> <reporting> ...... </reporting> <dependencyManagement> <dependencies> <dependency> ...... </dependency> </dependencies> </dependencyManagement> <distributionManagement> ...... </distributionManagement> <properties /> </profile> </profiles>
|
4.14、repositories 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <repositories> <repository> <releases> <enabled /> <updatePolicy /> <checksumPolicy /> </releases>
<snapshots> <enabled /> <updatePolicy /> <checksumPolicy /> </snapshots> <id>banseon-repository-proxy</id> <name>banseon-repository-proxy</name> <url>http://192.168.1.169:9999/repository/</url> <layout>default</layout> </repository> </repositories>
|
4.15、pluginRepositories 节点
1 2 3 4 5 6 7
| <pluginRepositories> <pluginRepository> ...... </pluginRepository> </pluginRepositories>
|
4.16、dependencies 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-artifact</artifactId> <version>3.8.1</version>
<type>jar</type>
<classifier></classifier>
<scope>test</scope> <systemPath>${project.basedir}/你的包路径</systemPath> <exclusions> <exclusion> <artifactId>spring-core</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> <optional>true</optional> </dependency> </dependencies>
|
根元素 project 下的 dependencies 可以包含一个或者多个dependency元素,以声明一个或者多个项目依赖。每个依赖可以包含的元素有:
- grounpId、artifactId和version:以来的基本坐标,对于任何一个依赖来说,基本坐标是最重要的,Maven根据坐标才能找到需要的依赖。
- type:依赖的类型,对于项目坐标定义的packaging。大部分情况下,该元素不必声明,其默认值为jar
- scope:依赖的范围
- optional:标记依赖是否可选
- exclusions:用来排除传递性依赖
4.17、reporting 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
<reporting> <excludeDefaults /> <outputDirectory /> <plugins> <plugin> <groupId /> <artifactId /> <version /> <inherited /> <configuration /> <reportSets> <reportSet> <id /> <configuration /> <inherited /> <reports /> </reportSet> </reportSets> </plugin> </plugins> </reporting>
|
4.18、dependencyManagement 节点
1 2 3 4 5 6 7 8 9 10
|
<dependencyManagement> <dependencies> <dependency> ...... </dependency> </dependencies> </dependencyManagement>
|
4.19、distributionManagement 节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <distributionManagement> <repository> <uniqueVersion /> <id>banseon-maven2</id> <name>banseon maven2</name> <url>file://${basedir}/target/deploy</url> <layout /> </repository> <snapshotRepository> <uniqueVersion /> <id>banseon-maven2</id> <name>Banseon-maven2 Snapshot Repository</name> <url>scp://svn.sundaytech.cn/flygo:/usr/local/maven-snapshot</url> <layout /> </snapshotRepository> <site> <id>banseon-site</id> <name>business api website</name> <url> scp://svn.sundaytech.cn/flygo:/var/www/localhost/banseon-web </url> </site> <downloadUrl /> <relocation> <groupId /> <artifactId /> <version /> <message /> </relocation>
<status /> </distributionManagement>
|
4.20、inceptionYear 节点
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
| <?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.0http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <artifactId/> <groupId/> <version/> </parent> <modelVersion>4.0.0</modelVersion> <groupId>cn.missbe.web</groupId> <artifactId>search-resources</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>search-resources</name> <url>http://www.missbe.cn</url> <description>A maven project to study maven.</description> <prerequisites> <maven/> </prerequisites> <build> <sourceDirectory/> <scriptSourceDirectory/> <testSourceDirectory/> <outputDirectory/> <testOutputDirectory/> <extensions> <extension> <groupId/> <artifactId/> <version/> </extension> </extensions> <resources> <resource> <targetPath/> <filtering/> <directory/> <includes/> <excludes/> </resource> </resources> <testResources> <testResource> <targetPath/> <filtering/> <directory/> <includes/> <excludes/> </testResource> </testResources> <directory/> <finalName/> <filters/> <pluginManagement> <plugins> <plugin> <groupId/> <artifactId/> <version/> <extensions/> <executions> <execution> <id/> <phase/> <goals/> <inherited/> <configuration/> </execution> </executions> <dependencies> <dependency>......</dependency> </dependencies> <inherited/> <configuration/> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId/> <artifactId/> <version/> <extensions/> <executions> <execution> <id/> <phase/> <goals/> <inherited/> <configuration/> </execution> </executions> <dependencies> <dependency>......</dependency> </dependencies> <goals/> <inherited/> <configuration/> </plugin> </plugins> </build> <modules/> <repositories> <repository> <releases> <enabled/> <updatePolicy/> <checksumPolicy/> </releases> <snapshots> <enabled/> <updatePolicy/> <checksumPolicy/> </snapshots> <id>banseon-repository-proxy</id> <name>banseon-repository-proxy</name> <url>http://192.168.1.169:9999/repository/</url> <layout>default</layout> </repository> </repositories> <pluginRepositories> <pluginRepository>......</pluginRepository> </pluginRepositories> <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-artifact</artifactId> <version>3.8.1</version> <type>jar</type> <classifier/>
<scope>test</scope> <systemPath/> <exclusions> <exclusion> <artifactId>spring-core</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> <optional>true</optional> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency>......</dependency> </dependencies> </dependencyManagement> <distributionManagement> <repository> <uniqueVersion/> <id>banseon-maven2</id> <name>banseon maven2</name> <url>file://${basedir}/target/deploy</url> <layout/> </repository> <snapshotRepository> <uniqueVersion/> <id>banseon-maven2</id> <name>Banseon-maven2 Snapshot Repository</name> <url>scp://svn.baidu.com/banseon:/usr/local/maven-snapshot</url> <layout/> </snapshotRepository> <site> <id>banseon-site</id> <name>business api website</name> <url>scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web</url> </site> <downloadUrl/> <status/> </distributionManagement> <properties/> </project>
|