Maven pom.xml文件详解

POM(Project Object Model), 是名为pom.xml的Maven项目XML表现。

包含配置文件,所涉及的开发人员及角色,组织,许可证,项目地址,项目依赖等等

POM Overview

这是一个完整的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <!-- 4.0.0 当前唯一受支持的POM版本,并且始终是必需的 -->
  <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>

Basic Setting

<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
  • groupId: 在组织或项目中是唯一的。点注明的groupID不必与项目包含的软件包结构对应

  • artifactId: 项目名称

  • version: 项目版本

  • packaging: 项目的打包格式(jar/war)

  • parent: 父项目

  • dependencies: 依赖集合

    • dependency: 依赖
      • groupId:与artifaceId构成直接的依赖坐标
      • artifactId
      • version: 该依赖的版本(注意规范)
      • type: 该依赖类型(默认jar)
      • scope: 作用范围 (compile, provided, runtime, test, system )
      • systemPath: 仅当scope = system时需要配置
      • optional
      • exclusions: 排除元素
        • exclusion: 每个元素包含一个groupidArtifactId
  • dependencyManagement: 依赖的配置

    dependencyManagement只是声明依赖,不自动实现引入。子项目需要显式地声明需要用地依赖,且当未指定具体版本号时,读取父POM中的versionscope

    dependencies里的依赖都会自动引入,并默认被子项目继承

  • modules: 多模块(聚合)

  • properties: 配置, 用于指定一些参数,使用值占位符(${xxx})调用

Build Setting

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <!-- "Project Build" contains more elements than just the BaseBuild set -->
  <build>...</build>
 
  <profiles>
    <profile>
      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
      <build>...</build>
    </profile>
  </profiles>
</project>

Resource

resource是指不需要编译,但需要捆绑在项目中或其他用途的项(如代码生成)。

<!-- eg.  plexus项目需要一个指定的配置文件configuration.xml -->

<build>
  ...
  <resources>
    <resource>
      <targetPath>META-INF/plexus</targetPath>
      <filtering>false</filtering>
      <directory>${basedir}/src/main/plexus</directory>
      <includes>
         <include>configuration.xml</include>
      </includes>
      <excludes>
         <exclude>**/*.properties</exclude>
      </excludes>
    </resource>
  </resources>
  <testResources>
    ...
  </testResources>
  ...
</build>
  • resources:资源列表,每个元素描述与此项目相关的文件的内容和位置
  • targetPath:指定一个目录结构以存放生成的资源集,常用的在JAR中放置打包资源的targetPath通常是META-INF
  • filtering:是否要为此资源启用过滤
  • directory:资源的目录所在,默认的是${basedir}/src/main/resource
  • includes:一组文件,指定哪些文件作为资源打包,使用*作为通配符
  • excludes:一组文件,指定哪些文件排除打包,优先级高于includes
  • testResources:类似于resource,但是在测试阶段使用,不会被部署

如果还不清楚可以进项目看看引用的其他人的依赖,往往在其依赖中有两个文件,一个是依赖的源代码,另一个就是META-INF,里面基本上一些配置文件。

Plugin

<build>
  ...
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>2.6</version>
      <extensions>false</extensions>
      <inherited>true</inherited>
      <configuration>
         <classifier>test</classifier>
      </configuration>
      <dependencies>...</dependencies>
      <executions>...</executions>
    </plugin>
  </plugins>
</build>
  • extension:是否加载此插件的扩展,默认为false
  • inherited:是否应用于从其继承的POM,默认为true
  • configuration:针对单插件的配置。不需要深入了解插件的运作方式就可以在这里配置插件需要的一些属性。POM会继承在父POM中在build/pluginspluginManagement配置的插件属性
    • 默认继承:child有的用child的,child没有的用parent的
    • 高级继承:通过对元素添加属性控制子POM如何从父POM处继承
      • combine.childrenappend, merge(default)
      • combine.selfoverride, merge(default)
  • dependency:做为所在插件的依赖项列表
  • executions

PluginManagement

PluginManagement是配置从这个POM继承的POM。其类似于dependencyManagement

Environment Setting

Repositories

Maven的仓库配置

Maven的中心仓库Central并不是包含了所有的依赖,一些依赖来自于私人的仓库,这种就需要自行去配置,比如说我之前在《Maven的安装配置(附geotools踩坑)》所提到的geotools。

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <repositories>
    <repository>
      <releases>
        <enabled>false</enabled>
        <updatePolicy>always</updatePolicy>
        <checksumPolicy>warn</checksumPolicy>
      </releases>
      <snapshots>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
        <checksumPolicy>fail</checksumPolicy>
      </snapshots>
      <name>Nexus Snapshots</name>
      <id>snapshots-repo</id>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
      <layout>default</layout>
    </repository>
  </repositories>
  <pluginRepositories>
    ...
  </pluginRepositories>
  ...
</project>
  • release, snapshots: 对仓库的发布、快照版本的调用管理
  • enabled: 是否为相应类型启用此存储库
  • updatePolicy: 更新频率(always, daily, interval:X (间隔Xminutes), never
  • checksumPolicy: 校验和策略 (ignore, fail, warn)
  • layout: 上述仓库描述的布局版本(default (Maven2/3), legacy (Maven1))

Example in GeoTools

<repositories>
 <repository>
     <id>osgeo</id>
     <name>OSGeo Release Repository</name>
     <url>https://repo.osgeo.org/repository/release/</url>
     <snapshots><enabled>false</enabled></snapshots>
     <releases><enabled>true</enabled></releases>
 </repository>
</repositories>

<dependencies>
	<!-- https://mvnrepository.com/artifact/org.geotools/gt-geojson -->
 <dependency>
     <groupId>org.geotools</groupId>
     <artifactId>gt-geojson</artifactId>
     <version>25.2</version>
 </dependency>
</dependencies>

Profile

配置文件

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <profiles>
    <profile>
      <id>test</id>
      <activation>        
        <activeByDefault>false</activeByDefault>
        <jdk>1.5</jdk>
        <os>
          <name>Windows XP</name>
          <family>Windows</family>
          <arch>x86</arch>
          <version>5.1.2600</version>
        </os>
        <property>
          <name>sparrow-type</name>
          <value>African</value>
        </property>
        <file>
          <exists>${basedir}/file2.properties</exists>
          <missing>${basedir}/file1.properties</missing>
        </file>
      </activation>
      <build>...</build>
      <modules>...</modules>
      <repositories>...</repositories>
      <pluginRepositories>...</pluginRepositories>
      <dependencies>...</dependencies>
      <reporting>...</reporting>
      <dependencyManagement>...</dependencyManagement>
      <distributionManagement>...</distributionManagement>
    </profile>
  </profiles>
</project>
  • activation: 激活指定的配置文件
    • jdk: 匹配JDK版本激活
    • os: 定义一些特定于操作系统的属性
    • property: 根据系统属性和命令行属性来激活
    • file: 通过给定的文件名激活配置,这里能使用的插值只有${basedir}, 系统属性,请求属性

IDEA ignored pom.xml

通过IDEA创建Maven项目很简单,点点点就好了,但偶尔也会出现ignored pom.xml的情况。

一般是在删除了一个项目后,又新建一个同名的项目,而IDEA的缓存仍旧会认为该项目是丢弃的,其Maven工具会将其认为是Ingored File,导致这个maven项目不生效。这个问题导致我在自己瞎折腾时候严重怀疑自己是不是创建出问题了。。。

只需要在IDEA中的Setting(MacOS中为Preferences)-> Build, Execution, Deployment -> Build Tools -> Maven -> Ignored Files中将项目的pom.xml文件取消勾选即可。

posted @   Zang998  阅读(83)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示