版本 | 日期 | 修訂人 | 描述 |
---|---|---|---|
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項目關聯的幾種方式(比如:依賴、繼承、聚合),以及它們的適用場景。
最后
小弟不才,學識有限,如有錯漏,歡迎指正哈。
如果本文對你有幫助,記得“一鍵三連
”(“點贊
”、“評論
”、“收藏
”)哦!