摘要
本文介绍maven中pom常用标签作用和用法。
核心部分
xml
<!-- 依赖的集合 -->
<dependencies>
<!-- 具体某个依赖 -->
<dependency>
<!-- 组织或项目的顶级域名,通常为反向域名 -->
<groupId>com.alibaba</groupId>
<!-- 项目名称,通常与项目模块名一致 -->
<artifactId>easyexcel</artifactId>
<!-- 项目版本号,遵循语义化版本规范 ${}读取属性值 也可写死4.0.3 -->
<version>${easyexcel.version}</version>
<!-- jar包的类型,默认jar -->
<type>jar</type>
<!-- 默认作用域。表示该依赖在项目的编译、测试、运行等所有阶段都可用,且会被打包到最终的项目产物中 -->
<scope>compile</scope>
<!-- 此依赖是否可选,可选依赖不会被传递到依赖该项目的其他项目中 -->
<optional>true</optional>
<!-- 排除依赖的集合 -->
<exclusions>
<!-- 具体某个排除的坐标 这里不写版本号,跟随依赖版本-->
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
父模块pom.xml
xml
<?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>org.coffeebeans</groupId>
<!-- 项目名称,通常与项目模块名一致 -->
<artifactId>myproject</artifactId>
<!-- 项目版本号,遵循语义化版本规范 -->
<version>1.0.0</version>
<!-- 打包方式,父模块这里是pom -->
<packaging>pom</packaging>
<!-- 子模块列表 -->
<modules>
<!-- 具体模块 -->
<module>pom-use</module>
</modules>
<!-- 自定义属性 可定义全局通用属性和管理版本-->
<properties>
<!-- 编译jdk版本 -->
<maven.compiler.source>8</maven.compiler.source>
<!-- 运行jdk版本 -->
<maven.compiler.target>8</maven.compiler.target>
<!-- 编码 -->
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 统一管理依赖版本 -->
<spring-boot.version>2.7.18</spring-boot.version>
</properties>
<!-- 统一管理依赖 -->
<dependencyManagement>
<!-- 统一管理依赖版本 -->
<dependencies>
<!-- 具体依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<!-- 指定依赖是一个pom文件,而不是一个jar文件。这通常用于依赖管理(例如,导入其他pom文件中的依赖管理部分)-->
<type>pom</type>
<!-- 此依赖仅用于<dependencyManagement>中导入其他 POM 文件中的依赖管理,但本身不会被传递 -->
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 项目依赖的存储库地址 -->
<repositories>
<repository>
<id>public</id>
<name>aliyun nexus</name>
<url>https://maven.aliyun.com/repository/public</url>
<!-- 默认true,启用从该存储库获取正式版本的依赖 -->
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</project>
子模块pom.xml
xml
<?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及识别坐标 -->
<parent>
<groupId>org.coffeebeans</groupId>
<artifactId>myproject</artifactId>
<version>1.0.0</version>
</parent>
<!-- 项目名 -->
<name>pom-use</name>
<!-- 项目描述 -->
<description>演示项目依赖标签的使用</description>
<!-- 打包方式,默认jar,可以是pom,jar,war -->
<packaging>jar</packaging>
<!-- 当前pom的id,通常与项目模块名一致 -->
<artifactId>pom-use</artifactId>
<!-- 自定义属性 可定义全局通用属性和管理版本-->
<properties>
<!-- 编译jdk版本 -->
<maven.compiler.source>8</maven.compiler.source>
<!-- 运行jdk版本 -->
<maven.compiler.target>8</maven.compiler.target>
<!-- 编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 统一管理依赖版本 -->
<easyexcel.version>4.0.3</easyexcel.version>
</properties>
<!-- 依赖的集合 -->
<dependencies>
<!-- 具体某个依赖 -->
<dependency>
<!-- 组织或项目的顶级域名,通常为反向域名 -->
<groupId>com.alibaba</groupId>
<!-- 项目名称,通常与项目模块名一致 -->
<artifactId>easyexcel</artifactId>
<!-- 项目版本号,遵循语义化版本规范 ${}读取属性值 也可写死4.0.3 -->
<version>${easyexcel.version}</version>
<!-- jar包的类型,默认jar -->
<type>jar</type>
<!-- 默认作用域。表示该依赖在项目的编译、测试、运行等所有阶段都可用,且会被打包到最终的项目产物中 -->
<scope>compile</scope>
<!-- 此依赖是否可选,可选依赖不会被传递到依赖该项目的其他项目中 -->
<optional>true</optional>
<!-- 排除依赖的集合 -->
<exclusions>
<!-- 具体某个排除的坐标 这里不写版本号,跟随依赖版本-->
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
<!-- 此依赖仅在编译和测试时需要,运行时由容器提供,不会被打包到最终的项目产物中 -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
<!-- 此依赖仅在运行和测试时需要,编译时不需要,会打包到最终的项目产物中 -->
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- 仅在测试编译和测试运行时使用,不会被打包到最终的项目产物中,也不会被传递到依赖该项目的其他项目中。-->
<scope>test</scope>
</dependency>
</dependencies>
<!-- 构建的集合 -->
<build>
<!-- 打包后的文件名 -->
<finalName>pom-use</finalName>
<!-- 插件的集合 -->
<plugins>
<!-- 打包插件 -->
<plugin>
<!-- 组织或项目的顶级域名,通常为反向域名 -->
<groupId>org.springframework.boot</groupId>
<!-- 项目名称,通常与项目模块名一致 -->
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 项目版本号,遵循语义化版本规范 ${}读取属性值 也可写死2.7.11 -->
<version>2.7.11</version>
<!-- 插件的配置 -->
<configuration>
<!-- 启用fork,表示测试代码将在一个新的 JVM 进程中运行,而不是在 Maven 运行的当前 JVM 中运行 -->
<fork>true</fork>
<!-- 打包后的输出目录 -->
<outputDirectory>../pom-use</outputDirectory>
</configuration>
<!-- 插件的执行 -->
<executions>
<!-- 具体执行项 -->
<execution>
<!-- 目标 -->
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- 环境配置 -->
<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
<!-- 其他配置,如端口-->
<port>1000</port>
</properties>
<!-- 默认激活 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
<port>2000</port>
</properties>
</profile>
<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
<port>3000</port>
</properties>
</profile>
</profiles>
</project>
总结
以上我们了解了maven中pom里常用标签作用和用法。
关注公众号:咖啡Beans
在这里,我们专注于软件技术的交流与成长,分享开发心得与笔记,涵盖编程、AI、资讯、面试等多个领域。无论是前沿科技的探索,还是实用技巧的总结,我们都致力于为大家呈现有价值的内容。期待与你共同进步,开启技术之旅。