Maven--pom.xml文件详解

1.pom简介

pom指的是project object model,又叫项目对象模型。Maven的pom文件是一个XML文件,用于描述项目的各种属性、依赖和构建信息,包括项目的名称、版本、许可证、作者、描述、依赖关系、构建过程、插件等。总的来说,POM文件是Maven工程的基本工作单元,它包含了项目的所有必要信息,使得Maven能够自动化地构建和管理项目。

2.常用的pom元素配置
bash 复制代码
<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>
	
	<!--                          父项目坐标属性                              -->
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.1</version>
        <relativePath>/home/mymodule</relativePath> 
    </parent>

	<!--                           当前项目基本属性                           -->
	<groupId>com.njh</groupId>
    <artifactId>Njh-Cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Njh-Cloud</name>
    <description>Njh-Cloud</description>
    <packaging>pom</packaging>
	
	<!--                           当前项目下的子模块配置                      -->
	<modules>
        <module>njh-gateway</module>
    </modules>
	
	<!--                           项目常量属性设置                                 -->
	<properties>
        <java.version>1.8</java.version>
    </properties>
	
	<!--                           项目依赖管理                                 -->
	<dependencyManagement>
        <dependencies>
            <!-- SpringCloud 微服务 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- SpringBoot 依赖配置 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

	<!--                           项目依赖定义                            	    -->
	<dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
    </dependencies>

	<!--                           项目构建信息                              	 -->
	<build>
        <!-- 插件管理 -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
        <!-- 资源管理 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 关闭过滤 -->
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/webapp/</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 引入所有 匹配文件进行过滤 -->
                <includes>
                    <include>application*</include>
                    <include>bootstrap*</include>
                    <include>logback*</include>
                </includes>
                <!-- 启用过滤 即该资源中的变量将会被过滤器中的值替换 -->
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

	<!--                           项目环境信息                              	  -->
	<profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <!-- 默认激活dev环境 -->
            <activation> 
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>prd</id>
            <properties>
                <profiles.active>prd</profiles.active>
            </properties>
        </profile>
    </profiles>
	
	<!--                           项目仓库信息                              	  -->
	<repositories>
        <repository>
            <id>public</id>
            <name>nexus</name>
            <url>https://mirrors.njh.cloud.com/repository/maven/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

	<!--                           项目插件仓库信息                                 -->
	<pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>nexus</name>
            <url>https://mirrors.njh.cloud.com/repository/maven/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
	
</project>
3.父项目坐标属性

在Maven中使用<parent>标签是为了在多模块项目中共同管理依赖,可以让多个模块之间共享依赖关系,从而避免了重复的依赖定义。通过<parent>标签中的<groupId><artifactId><version>元素来指定父项目的坐标,子项目就可以通过该坐标找到父项目并自动继承父项目的pom文件中定义的相关依赖,然后子项目就可以不用重复去声明父项目中已存在的依赖,可以直接使用父项目中的依赖。

bash 复制代码
<parent>
	<!-- 父项目的群组标识符 -->
    <groupId>org.springframework.boot</groupId>  
    <!-- 父项目的项目名标识符 -->
    <artifactId>spring-boot-starter-parent</artifactId> 
    <!-- 父项目的版本号标识符 -->
    <version>3.2.1</version> 
    <!-- 用于指示Maven在查找父级POM文件时应该搜索的相对路径 -->
    <relativePath>/home/mymodule</relativePath> 
</parent>
4. 当前项目基本属性

用于设置当前项目的一些基本信息,通过<groupId><artifactId><version>元素来指定当前项目的坐标;<name>用来设置项目名称;<description>用来设置项目的描述信息;<url>用来设置项目的主也url地址;<packaging>用来设置项目的构建类型,如:jar、war、pom等。

bash 复制代码
<groupId>com.njh</groupId>
<artifactId>Njh-Cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Njh-Cloud</name>
<description>Njh-Cloud</description>
<url>https://gitee.com/Njh-Cloud</url>
<packaging>pom</packaging>
5.当前项目下的子模块配置

在Maven中,<modules>标签用于定义项目的模块。一个Maven项目可以包含多个模块,每个模块可以被视为一个独立的子项目。通过使用<modules>标签,可以将一个大项目拆分成多个小模块,每个模块都有自己的构建过程和依赖关系,从而提高项目的可维护性和可重用性。

bash 复制代码
<modules>
    <module>njh-gateway</module>
</modules>
6.项目常量属性设置

在Maven中,<properties>标签中用于定义项目的常量属性,这些常量属性可以在整个项目中被共享和重用,从而简化了项目的配置和管理。每个属性都包含一个id和一个值,在使用时通过${id}就能获取到设置的属性值。

bash 复制代码
<properties>
    <java.version>1.8</java.version>
</properties>

<!-- 使用上面的属性 -->
<version>${java.version}</version>
7.项目依赖管理

在Maven中,<dependencyManagement>标签用于管理项目中的依赖关系,确保所有子模块共享相同的依赖版本。通过在<dependencyManagement>标签中添加需要共用的依赖,在该项目下的所有子模块会自动引入相同的依赖,可以避免在每个子模块中重复定义相同的依赖,提高了项目构建的效率和一致性。

bash 复制代码
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>groupId</groupId>
            <artifactId>artifactId</artifactId>
            <version>version</version>
        </dependency>
    </dependencies>
</dependencyManagement>
8.项目依赖定义

在Maven中,<dependencies>标签用于定义项目的依赖关系,可以将项目所需的依赖库添加到项目中,并确保这些依赖被正确地管理和使用。每个依赖都是使用<dependency>标签定义的,<dependency>标签中常用的属性有:

  • <groupId>:依赖库的唯一标识符,通常是组织或项目的唯一标识符。
  • <artifactId>:依赖库的唯一标识符,通常是项目或库的唯一标识符。
  • <version>:依赖库的版本号。
  • <scope>:依赖项的作用范围 ,包括compile、provided、runtime、test、system、optional。
  • <optional>:标注依赖是否传递。默认值为 false,表示可以将该依赖向下传递给其他依赖了该项目的项目。设置成true,则不会向下传递。
  • <exclusions>:排除项目中引入的依赖中所包含的某些间接依赖。
bash 复制代码
<dependencies>
    <dependency>
        <groupId>groupId</groupId>
        <artifactId>artifactId</artifactId>
        <version>version</version>
        <scope>compile</scope>
      	<optional>true</optional>
      	<exclusions>
        	<exclusion> 
          		<groupId>groupid</groupId>
          		<artifactId>artifactid</artifactId>
        	</exclusion>
      	</exclusions>
    </dependency>
</dependencies>

scope 依赖项常用的适用范围如下:

  • compile:缺省值,表示所有阶段(编译、测试、运行)可用,该依赖包需要全程存在会随着项目一起发布。
  • provided:表示只在提供依赖的环境中是可用的,也就是在编译、测试时需要,运行时不需要,该依赖不会被打包到项目中;
  • runtime:表示只在运行时可用,该依赖会打包到项目中;
  • test:表示只在测试期间是可用的,该依赖不会被打包到项目中;
  • system:表示只能在系统类路径中找到,不会在Repository中查找它;
9.项目构建信息

在Maven中,<build>标签用于配置项目的构建过程。它包含了一系列的子标签,用于定义项目的构建策略和任务。常用的字标签有:

  1. <plugins>:用于定义Maven插件的列表。Maven插件可以用来执行各种任务,例如编译代码、运行测试、打包和发布项目等。
  2. <pluginManagement>:用于定义项目的构建插件的列表,以及这些插件的配置。
  3. <resources>:用于定义资源的配置,例如在构建过程中要处理的属性文件或资源文件。
  4. <testResources>:类似于<resources>,但是用于配置测试资源,例如在测试过程中要处理的属性文件或资源文件。
  5. <sourceDirectory>:用于指定项目的源代码目录。
  6. <outputDirectory>:用于指定项目的输出目录,例如生成的JAR文件或WAR文件。
  7. <filters>:用于定义项目的过滤器,例如在构建过程中要应用的属性文件过滤器。
  8. <finalName>:用于指定项目的最终名称,例如生成的JAR文件的名称。
bash 复制代码
<build>
	<plugins></plugins>
	<resources></resources>
	<testResources></testResources>
	<sourceDirectory></sourceDirectory>
	<outputDirectory></outputDirectory>
	<filters></filters>
	<finalName></finalName>
	<pluginManagement></pluginManagement>
</build>
10.项目环境信息

在Maven中,<profiles>标签用于定义项目的构建环境的配置,它允许你为不同的构建环境或目标提供不同的配置。通过不同的<profile>标签来定义不同的环境配置,在<profile>标签里面包含以下部分:

  • <id>:配置文件的唯一标识符。
  • <activation>:用于激活配置文件的逻辑。例如,使用<activeByDefault>属性来指定默认激活的配置文件。
  • <properties>: 用于存储配置文件中使用的属性。
  • <build>: 用于定义配置文件中的构建任务和插件。
bash 复制代码
<profiles>
    <profile>
        <!-- 本地开发环境 -->
        <id>dev</id>
        <properties>
            <profiles.active>dev</profiles.active>
        </properties>
        <!-- 默认激活dev环境 -->
        <activation> 
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <!-- 生产环境 -->
        <id>prd</id>
        <properties>
            <profiles.active>prd</profiles.active>
        </properties>
    </profile>
</profiles>
11.项目仓库信息

在Maven中,<repositories>标签用于配置项目所需的依赖库的位置,使得项目能够方便地获取和管理依赖。通过配置不同的仓库,可以轻松地在不同的环境中使用不同的依赖库,从而增加了项目的灵活性和可移植性。其中<repository>标签包含以下部分:

  • <id>:仓库的唯一标识符。
  • <url>:仓库的URL地址。
  • <releases>:用于配置仓库中发布的版本。如果<releases>标签被包含,那么Maven会在这个仓库中搜索并下载项目所需的依赖库。
  • <snapshots>:用于配置仓库中快照版本的位置。如果<snapshots>标签被包含,那么Maven会在这个仓库中搜索并下载项目所需的快照版本。
bash 复制代码
<repositories>
    <repository>
        <id>snapshots</id>
        <url>http://repo.maven.apache.org/maven-snapshots/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
    <repository>
        <id>releases</id>
        <url>http://repo.maven.apache.org/maven-releases/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>
12.项目插件仓库信息

在Maven中,<pluginRepositories>标签用于配置项目所需的插件库的位置,与<repositories>标签功能基本相同,主要的区别是为了获取和管理Maven插件和插件依赖的。

bash 复制代码
<pluginRepositories>
    <pluginRepository>
        <id>central</id>
        <url>http://repo.maven.apache.org/maven2</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
相关推荐
一只特立独行的猪6111 小时前
Java面试——集合篇
java·开发语言·面试
讓丄帝愛伱2 小时前
spring boot启动报错:so that it conforms to the canonical names requirements
java·spring boot·后端
weixin_586062022 小时前
Spring Boot 入门指南
java·spring boot·后端
Dola_Pan6 小时前
Linux文件IO(二)-文件操作使用详解
java·linux·服务器
wang_book6 小时前
Gitlab学习(007 gitlab项目操作)
java·运维·git·学习·spring·gitlab
蜗牛^^O^6 小时前
Docker和K8S
java·docker·kubernetes
从心归零7 小时前
sshj使用代理连接服务器
java·服务器·sshj
IT毕设梦工厂8 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
Ylucius9 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
七夜zippoe9 小时前
分布式系统实战经验
java·分布式