You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Maven Modules配置项目,避免本地仓库安装公共依赖?

How to Streamline Your Maven Build with Multi-Module Setup

Absolutely! You can ditch that manual cd ProjectCommon && mvn install step completely by setting up a Maven multi-module project. This lets Maven handle dependency order and build flow automatically—no more installing ProjectCommon to your local repo every time you need to build ProjectA or ProjectB. Here's a step-by-step guide:

Step 1: Restructure Your Projects into a Parent Container

First, create a parent project (let's call it ProjectParent) that will act as the root for all three modules. Your folder structure should look like this:

ProjectParent/
├── pom.xml          # Parent POM file
├── ProjectCommon/
│   └── pom.xml
├── ProjectA/
│   └── pom.xml
└── ProjectB/
    └── pom.xml

Step 2: Configure the Parent POM

The parent POM defines the overall project structure and shared settings. Set its packaging to pom and list all three modules:

<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.yourcompany</groupId> <!-- Replace with your actual group ID -->
    <artifactId>ProjectParent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Project Parent</name>

    <!-- List all child modules here -->
    <modules>
        <module>ProjectCommon</module>
        <module>ProjectA</module>
        <module>ProjectB</module>
    </modules>
</project>

Step 3: Update ProjectCommon's POM

Modify ProjectCommon to inherit from the parent POM. This removes the need to repeat group ID and version, and links it to the multi-module structure:

<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">
    <parent>
        <groupId>com.yourcompany</groupId>
        <artifactId>ProjectParent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath> <!-- Path to parent POM -->
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>ProjectCommon</artifactId>
    <name>Project Common</name>

    <!-- Keep your existing dependencies and configurations here -->
</project>

Step 4: Update ProjectA and ProjectB's POMs

Do the same for ProjectA and ProjectB: inherit from the parent POM, and update their dependency on ProjectCommon to omit the version (it will inherit from the parent):

<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">
    <parent>
        <groupId>com.yourcompany</groupId>
        <artifactId>ProjectParent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>ProjectA</artifactId>
    <name>Project A</name>

    <dependencies>
        <dependency>
            <groupId>com.yourcompany</groupId>
            <artifactId>ProjectCommon</artifactId>
            <!-- No version needed—uses parent's version -->
        </dependency>
        <!-- Keep your other dependencies here -->
    </dependencies>
</project>

Step 5: Build Your Projects Easily

Now you can build everything from the parent directory with a single command:

mvn clean package

Maven will automatically build ProjectCommon first (since ProjectA and ProjectB depend on it), then build ProjectA and ProjectB—no manual install steps required.

If you only want to build ProjectA (and its dependencies):

mvn clean package -pl ProjectA -am
  • -pl specifies the module to build
  • -am tells Maven to build all modules that the specified module depends on (so ProjectCommon gets built first)

Key Notes

  • Double-check the relativePath in each child POM to ensure it correctly points to the parent's pom.xml.
  • If you had previously installed old versions of ProjectCommon to your local repo, run mvn clean install once from the parent directory to sync the latest version, or delete the old artifacts from your local repo to avoid conflicts.
  • Using a parent POM also lets you manage dependency versions, plugins, and other settings centrally—making your build more consistent.

内容的提问来源于stack exchange,提问作者Beto Neto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:57:02