版本 | 日期 | 修订人 | 描述 |
---|---|---|---|
V1.0 | 2025/3/7 | nick huang | 创建文档 |
背景
项目过程中,对于Maven的pom.xml文件,很多时候,我通过各种参考、仿写,最终做出想要的效果。
但实际心里有些迷糊,不清楚具体哪个基础的配置所实现的效果。
今天,特意回过头来,了解Maven的基础知识,以便以后使用起来更有条理。
最简单的Maven项目
用IntelliJ IDEA
创建一个最简单的Maven
项目,可以看到pom.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>com.nicchagil</groupId><artifactId>simple-idea-maven-project</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties></project>
通过Windows的tree命令,查看整个项目的目录结构:
└─src├─main│ ├─java│ │ └─com│ │ └─nicchagil│ └─resources└─test└─java
最简单的pom.xml底下的内容
通过以下操作,我们可以查看上述最简单pom.xml的「Effevtive POM
」:
1、在「IntelliJ IDEA」中右键「pom.xml的文件内容」
2、选择「Show Effevtive POM」
查看了「Effevtive POM
」,查阅了其中部分节点的作用,并以中文加上注释:
Tips
为了提高可读性,以下Effevtive POM的内容会适当删减。
<!-- Effective POM for project -->
<!-- 'com.nicchagil:simple-idea-maven-project:jar:1.0-SNAPSHOT' -->
<!-- -->
<!-- ====================================================================== --><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"><!-- 指定当前pom.xml文件所遵循的Maven项目对象模型(Project Object Model)版本 --><modelVersion>4.0.0</modelVersion><!-- 用于唯一标识一个公司(或项目、团队等) --><groupId>com.nicchagil</groupId><!-- 与groupId和version唯一标识一个项目或模块 --><artifactId>simple-idea-maven-project</artifactId><!-- 唯一标识项目的版本 --><version>1.0-SNAPSHOT</version><properties><!-- 指定Java源代码所遵循的版本 --><maven.compiler.source>8</maven.compiler.source><!-- 指定Java编译器生成的字节码所兼容的Java虚拟机版本 --><maven.compiler.target>8</maven.compiler.target><!-- 指定源代码文件的字符编码 --><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!-- 仓库 --><repositories><repository><snapshots><enabled>false</enabled> <!-- 表示禁用对该仓库中快照版本的支持 --></snapshots><id>central</id><name>Central Repository</name><url>https://repo.maven.apache.org/maven2</url> <!-- 这里配置的URL(https://repo.maven.apache.org/maven2)是Maven中央仓库 --></repository></repositories><!-- 插件仓库 --><pluginRepositories><pluginRepository><releases><updatePolicy>never</updatePolicy> <!-- 对于发布版本的更新策略为never,即永远不会去检查该仓库中发布版本插件是否有更新 --></releases><snapshots><enabled>false</enabled> <!-- 禁用对该仓库中快照版本插件的支持 --></snapshots><id>central</id><name>Central Repository</name><url>https://repo.maven.apache.org/maven2</url></pluginRepository></pluginRepositories><!-- 项目的构建过程 --><build><!-- 全局构建配置 --><!-- 指定项目源代码的目录,Maven在编译时从此目录获取源代码 --><sourceDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\src\main\java</sourceDirectory><!-- 没查到scriptSourceDirectory相应的资料 --><scriptSourceDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\src\main\scripts</scriptSourceDirectory><!-- 指定项目测试代码的目录 --><testSourceDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\src\test\java</testSourceDirectory><!-- 指定编译后的类文件的输出目录 --><outputDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\target\classes</outputDirectory><!-- 指定测试代码编译后的类文件输出目录 --><testOutputDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\target\test-classes</testOutputDirectory><!-- 项目资源文件 --><resources><resource><directory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\src\main\resources</directory></resource></resources><!-- 项目测试相关的资源文件 --><testResources><testResource><directory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\src\test\resources</directory></testResource></testResources><!-- 构建输出的目录 --><directory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\target</directory><!-- 项目打包后生成的最终文件的名称(不包含扩展名) --><finalName>simple-idea-maven-project-1.0-SNAPSHOT</finalName><!-- 对插件的版本、配置等信息进行集中管理与配置。通常位于父项目中,为子模块提供统一的插件配置模板。此节点的配置不会直接生效,子模块需要时显示引用对应的插件才会生效 --><pluginManagement><plugins><!-- 用于Apache Ant任务的支持 --><plugin><artifactId>maven-antrun-plugin</artifactId><version>3.1.0</version></plugin><!-- 用于将项目的输出和相关依赖打包成单独分发文件,方便项目的分发和部署 --><plugin><artifactId>maven-assembly-plugin</artifactId><version>3.6.0</version></plugin><!-- 用于管理和操作项目的依赖 --><plugin><artifactId>maven-dependency-plugin</artifactId><version>3.6.1</version></plugin><!-- 用于简化项目的发布流程 --><plugin><artifactId>maven-release-plugin</artifactId><version>3.0.1</version></plugin></plugins></pluginManagement><!-- 项目构建过程中实际要用到的插件 --><plugins><!-- 用于清理项目构建过程中生成的文件和目录 --><plugin><artifactId>maven-clean-plugin</artifactId><version>3.2.0</version><executions><execution><id>default-clean</id><phase>clean</phase><goals><goal>clean</goal></goals></execution></executions></plugin><!-- 用于处理项目中的资源文件。主要会将资源文件复制到指定的输出目录,并可以对这些资源文件进行过滤等操作 --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.3.1</version><executions><execution><id>default-testResources</id><phase>process-test-resources</phase><goals><goal>testResources</goal></goals></execution><execution><id>default-resources</id><phase>process-resources</phase><goals><goal>resources</goal></goals></execution></executions></plugin><!-- 用于创建JAR(Java Archive)文件的核心插件 --><plugin><artifactId>maven-jar-plugin</artifactId><version>3.3.0</version><executions><execution><id>default-jar</id><phase>package</phase><goals><goal>jar</goal></goals></execution></executions></plugin><!-- 用于编译Java源代码 --><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version><executions><execution><id>default-compile</id><phase>compile</phase><goals><goal>compile</goal></goals></execution><execution><id>default-testCompile</id><phase>test-compile</phase><goals><goal>testCompile</goal></goals></execution></executions></plugin><!-- 用于在项目构建过程中执行单元测试和集成测试 --><plugin><artifactId>maven-surefire-plugin</artifactId><version>3.2.2</version><executions><execution><id>default-test</id><phase>test</phase><goals><goal>test</goal></goals></execution></executions></plugin><!-- 用于将项目构建生成的产出(如JAR文件)安装到本地Maven仓库 --><plugin><artifactId>maven-install-plugin</artifactId><version>3.1.1</version><executions><execution><id>default-install</id><phase>install</phase><goals><goal>install</goal></goals></execution></executions></plugin><!-- 用于将项目构建的产物部署到远程Maven仓库 --><plugin><artifactId>maven-deploy-plugin</artifactId><version>3.1.1</version><executions><execution><id>default-deploy</id><phase>deploy</phase><goals><goal>deploy</goal></goals></execution></executions></plugin><!-- 用于生成项目的站点文档 --><plugin><artifactId>maven-site-plugin</artifactId><version>3.12.1</version><!-- 注意:因maven-site-plugin的配置内容较多,为提高可读性,已省略maven-site-plugin其中的配置 --><!-- maven-site-plugin的配置已省略 --></plugin></plugins></build><!-- 用于配置项目报告的生成的展示 --><reporting><outputDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\target\site</outputDirectory></reporting>
</project>
Maven的常用命令
通过阅读上文的「Effevtive POM
」,就容易理解我们经常使用的Maven命令,其基础配置是对应上面的默认插件。
比如:我们使用mvn clean
命令,实际起作用的是Maven的默认插件maven-clean-plugin
。
Maven的常用命令:
mvn clean
:清理项目构建生成的文件和目录mvn compile
:编译项目的源代码mvn test
:执行项目的测试mvn package
:将项目编译后的代码打包成可分发的格式,如JAR包mvn install
:将项目打包后的产物安装到本地Maven仓库mvn deploy
:将项目打包后的产物部署到远程Maven仓库
后续
后续,有时间再了解Maven项目关联的几种方式(比如:依赖、继承、聚合),以及它们的适用场景。
最后
小弟不才,学识有限,如有错漏,欢迎指正哈。
如果本文对你有帮助,记得“一键三连
”(“点赞
”、“评论
”、“收藏
”)哦!