使用 mvn 創建一個僅包含 pom.xml 文件的父項目,可以借助 maven-archetype-quickstart 原型,然后移除不必要的文件,或者直接通過命令生成最簡的 pom.xml 文件。以下是具體操作步驟:
一、方法一:使用原型創建后清理
1. 創建項目
打開命令行工具,運行以下命令創建一個基于?maven-archetype-quickstart
?原型的項目:
?
mvn archetype:generate -DgroupId=com.example -DartifactId=parent-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
此命令會創建一個名為?parent-project
?的項目,其?groupId
?為?com.example
。
2. 進入項目目錄
cd parent-project
3. 刪除不必要的文件和目錄
刪除 src 目錄,因為我們僅需要 pom.xml 文件:
rm -r src
4. 配置?pom.xml
將?pom.xml
?文件配置為父項目的?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.example</groupId><artifactId>parent-project</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><!-- 子模塊列表 --><modules><!-- <module>child-module-1</module> --><!-- <module>child-module-2</module> --></modules><!-- 依賴管理 --><dependencyManagement><dependencies><!-- 定義依賴版本 --></dependencies></dependencyManagement></project>
二、方法二:手動創建?pom.xml
1. 創建項目目錄
mkdir parent-project
cd parent-project
2. 創建 pom.xml 文件
使用文本編輯器創建 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.example</groupId><artifactId>parent-project</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><!-- 子模塊列表 --><modules><!-- <module>child-module-1</module> --><!-- <module>child-module-2</module> --></modules><!-- 依賴管理 --><dependencyManagement><dependencies><!-- 定義依賴版本 --></dependencies></dependencyManagement></project>
通過以上任意一種方法,都可以創建一個僅包含 pom.xml 文件的父項目。