首页 > Java > maven pom.xml 注释说明

maven pom.xml 注释说明

2025-04-27 13:31:42

 

<?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>  <!-- 必须的POM模型版本 -->

    <!-- 项目坐标(唯一标识) -->
    <groupId>com.example</groupId>       <!-- 组织或公司域名倒序 -->
    <artifactId>my-project</artifactId>  <!-- 项目名称 -->
    <version>1.0.0-SNAPSHOT</version>    <!-- 版本号(SNAPSHOT表示开发中版本) -->
    <packaging>jar</packaging>           <!-- 打包方式(jar/war/pom等) -->

    <!-- 父项目继承(可选) -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- 查找父pom时不从本地路径查找 -->
    </parent>

    <!-- 项目属性定义(可自定义变量) -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 源码编码 -->
        <maven.compiler.source>11</maven.compiler.source> <!-- Java版本 -->
        <maven.compiler.target>11</maven.compiler.target>
        <spring.version>5.3.20</spring.version> <!-- 自定义版本变量 -->
    </properties>

    <!-- 依赖管理 -->
    <dependencyManagement>
        <dependencies>
            <!-- 此处定义依赖版本,供子模块继承 -->
        </dependencies>
    </dependencyManagement>

    <!-- 项目依赖 -->
    <dependencies>
        <!-- 典型依赖项示例 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version> <!-- 使用属性变量 -->
            <scope>compile</scope> <!-- 默认作用域,可不写 -->
        </dependency>

        <!-- 测试依赖 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope> <!-- 仅在测试时有效 -->
        </dependency>

        <!-- 运行时依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
            <scope>runtime</scope> <!-- 编译不需要,运行时需要 -->
        </dependency>
    </dependencies>

    <!-- 构建配置 -->
    <build>
        <plugins>
            <!-- 编译器插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>

            <!-- 打包插件示例 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal> <!-- 构建可执行jar -->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <!-- 资源文件配置 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering> <!-- 是否替换资源文件中的变量 -->
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>

    <!-- 仓库配置(可选) -->
    <repositories>
        <repository>
            <id>aliyun</id>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>

    <!-- 多模块配置 -->
    <modules>
        <module>sub-module1</module> <!-- 子模块目录名称 -->
        <module>sub-module2</module>
    </modules>

    <!-- 环境配置(可选) -->
    <profiles>
        <profile>
            <id>dev</id> <!-- 开发环境配置 -->
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault> <!-- 默认激活 -->
            </activation>
        </profile>
        <profile>
            <id>prod</id> <!-- 生产环境配置 -->
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>
</project>
 

关键元素说明:

  1. 坐标标识

    • groupId: 组织/公司唯一标识(反向域名)
    • artifactId: 项目唯一标识
    • version: 项目版本(SNAPSHOT表示开发版本)
    • packaging: 打包方式(默认jar)
  2. 依赖管理

    • dependencies: 项目依赖清单
    • scope作用域:
      • compile(默认):编译、测试、运行都需要
      • test: 仅测试阶段需要
      • provided: 容器提供,如Servlet API
      • runtime: 运行时需要
  3. 构建配置

    • build/plugins: 构建过程使用的插件
    • resources: 资源文件处理配置
    • 常用插件:
      • compiler-plugin: 指定Java版本
      • surefire-plugin: 执行单元测试
      • jar/war-plugin: 打包工具
  4. 高级功能

    • dependencyManagement: 统一管理多模块依赖版本
    • profiles: 不同环境配置切换
    • modules: 多模块项目管理
使用 Ctrl+D 可将网站添加到书签
收藏网站
扫描二维码
关注早实习微信公众号
官方公众号
Top