Maven--pom
what is the POM?
pom是英文 project object model的缩写,译为项目对象模型,我们在学习编程语言的时候经常能接触到对象的概念,在maven的世界里,项目被理解为对象,具有id,版本,仓库,插件,依赖等多个属性,这些都是抽象概念,而pom.xml文件则是这些抽象的具体实现的一个载体。pom文件提供了一组标准的设置和规范,使我们能够通过pom文件能够配置项目的行为和信息,以起到管理项目的作用。
我们查阅官网,可以得到下面几个示例:
一个基本的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">
<!--模型版本 4.0.0 required -->
<modelVersion>4.0.0</modelVersion>
<!-- 唯一的组id,解释为该项目的所归属的组,并且配置时生成的路径也是由此生成, 如com.myDemo.demo,maven会将该项目打成的包放本地仓库的/com/myDemo/demo 路径下-->
<groupId>org.codehaus.mojo</groupId>
<!-- 项目的唯一ID,通常与项目名相同,一个groupId下面可能多个项目,就是靠artifactId来区分的 -->
<artifactId>my-project</artifactId>
<!-- 版本号 -->
<version>1.0</version>
</project>
上述的groupId artifactId version最终将生成项目包文件存放路径的结构和包文件名
如下示例:
POM配置
<modelVersion>4.0.0</modelVersion>
<groupId>com.myDemo.demo</groupId>
<artifactId>my-demo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-demo Maven Webapp</name>
<url>http://maven.apache.org</url>
目录结构及文件
pom文件的元素列表
注意:modelVersion 应为 4.0.0。这是目前唯一支持Maven 2和3的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">
<!-- 模型版本设置 == 4.0.0 required -->
<modelVersion>4.0.0</modelVersion>
<!-- 基础部分 -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>打包的类型(jar/war/...)</packaging>
<dependencies>依赖项</dependencies>
<parent>父级pom的信息,下有坐标系三个标签</parent>
<!--在我们项目顶层的POM文件中,我们会看到dependencyManagement元素。-->
<!--通过它元素来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。-->
<dependencyManagement>依赖声明</dependencyManagement>
<modules>模块组</modules>
<properties>属性组</properties>
<!-- 构建配置部分 -->
<build>...</build>
<!-- 报告配置部分 -->
<reporting>...</reporting>
<!-- 项目信息的描述部分,提供一些配置以设置项目的详细信息,并不是必须的 -->
<name>项目名</name>
<description>项目描述</description>
<url>项目网站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>
这里提供一份详细的pom文件配置解释:https://blog.csdn.net/weixin_43740223/article/details/89553270
Super POM
与面向对象编程中的对象继承类似,此外,正如Java对象最终从java.lang.Object继承一样,所有项目对象模型都继承自基础Super POM。
super pom 提供了pom文件的大部分默认配置,博主对此作了部分注释,仅供参考:如下
<project>
<modelVersion>4.0.0</modelVersion>
<!--默认的远程仓库 -->
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<!--该路径为maven中央仓库 -->
<!--国内建议使用阿里云中央仓库,访问速度较快 http://maven.aliyun.com/nexus/content/groups/public/ -->
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<!--默认的插件仓库 -->
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<!--该路径为maven中央仓库 -->
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
<build>
<!--默认的项目输出目录 -->
<directory>${project.basedir}/target</directory>
<!--默认的项目资源输出路径 -->
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<!--默认的项目包文件名 -->
<finalName>${project.artifactId}-${project.version}</finalName>
<!--默认的项目测试输出目录 -->
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<!--默认的source目录 -->
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<!--默认的scripSource目录 -->
<scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
<!--默认的测试source目录 -->
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<!--默认的资源文件目录 -->
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<!--默认的测试资源文件目录 -->
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<!-- NOTE: These plugins will be removed from future versions of the super POM -->
<!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
<!--默认引入的插件列表及其版本 -->
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<!--默认的网站生成目录 -->
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting>
<profiles>
<!-- NOTE: The release profile will be removed from future versions of the super POM -->
<profile>
<id>release-profile</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<!--默认引入的插件列表及其版本 -->
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
官方文档:http://maven.apache.org/ref/3-LATEST/maven-model-builder/super-pom.html
Maven坐标
在学习maven插件的引入和依赖的引入之前,我们需要先理解maven的坐标。
maven的每个项目都由POM文件中的groupId,artifactId以及version三个属性定义了唯一性。
在学几何的时候,我们了解到坐标系的构成,maven的坐标系是一个空间坐标系,坐标系由groupId,artifactId以及version三个轴组成,假设我们把groupId作为横轴代表项目的所属组别,artifactId作为纵轴作为项目在组别中的唯一标识 , version作为竖轴代表了迭代的版本,那么每个项目的这三个属性就形成了项目在坐标系内的一个点,我们也通过这三个属性在maven中定义和查找项目。
因此,我们在pom文件中引入插件和依赖的时候,也需要显式的表名这个三个属性。
示例:
<!-- 项目坐标系 -->
<groupId>com.demo.model</groupId>
<artifactId>demo-model-test</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 插件引入 -->
<plugins>
<plugin>
<groupId>org.zeroturnaround</groupId>
<artifactId>jrebel-maven-plugin</artifactId>
<version>1.1.5</version>
</plugin>
</plugins>
<!-- 依赖引入 -->
<dependencies>
<dependency>
<groupId>com.demo.common</groupId>
<artifactId>demo-common-base</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>