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

如何在Gradle与Maven共存的Spring Boot项目中统一管理依赖版本

Centralized Dependency Version Management for Maven + Gradle Spring Boot Projects

Absolutely! You can totally centralize your dependency versions in a shared location to wipe out the tedious manual sync between Maven and Gradle. Here are two practical, widely-used approaches tailored for Spring Boot projects:

Spring Boot already uses BOMs to manage its own dependencies, so extending this pattern for your custom dependencies feels like a natural fit. The BOM acts as a single source of truth for versions, which both Maven and Gradle can reference directly.

Step 1: Define the BOM in Maven

Create a dedicated Maven module (or add this to your parent pom) to host the BOM:

<!-- pom.xml (BOM module or parent pom) -->
<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>
    <artifactId>project-dependency-bom</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <name>Project Dependency BOM</name>

    <dependencyManagement>
        <dependencies>
            <!-- Your centralized dependency versions -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.31</version>
            </dependency>
            <!-- Add other dependencies you want to centralize here -->
        </dependencies>
    </dependencyManagement>
</project>

Step 2: Reference the BOM in your regular Maven project

In your application's pom.xml, import the BOM so you no longer need to specify versions for dependencies defined in it:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.yourcompany</groupId>
            <artifactId>project-dependency-bom</artifactId>
            <version>1.0.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- Now declare the dependency without a version -->
<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

Step 3: Reference the same BOM in Gradle

In your build.gradle, use Spring's dependency management plugin to import the BOM and inherit the versions:

// build.gradle
plugins {
    id 'java'
    id 'io.spring.dependency-management' version '1.1.4'
}

dependencyManagement {
    imports {
        mavenBom 'com.yourcompany:project-dependency-bom:1.0.0'
    }
}

dependencies {
    implementation(project(':demo'))
    // No version needed — it's pulled directly from the BOM
    implementation 'mysql:mysql-connector-java'
}

2. Use a Shared Properties File

If you prefer a simpler, file-based approach, you can create a single properties file that both Maven and Gradle read to fetch version values.

Step 1: Create a shared properties file

Add a versions.properties file at the root of your project:

# versions.properties
mysql.version=8.0.31
# Add other version keys here (e.g., spring.boot.version=3.2.0)

Step 2: Load the properties in Maven

In your pom.xml, use the properties-maven-plugin to load the file, then reference the properties in your dependencies:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>${project.basedir}/../versions.properties</file>
                            <!-- Adjust the path to match your project structure -->
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>
</dependencies>

Step 3: Load the properties in Gradle

In your build.gradle, read the properties file and use the values for your dependencies:

// build.gradle
def versionsProps = new Properties()
file('versions.properties').withInputStream { versionsProps.load(it) }

dependencies {
    implementation(project(':demo'))
    implementation group: 'mysql', name: 'mysql-connector-java', version: versionsProps['mysql.version']
}

Which Approach to Pick?

  • BOM is the top recommendation for Spring Boot projects: It aligns with Spring's native dependency management, handles transitive version resolution smoothly, and scales better as your project grows.
  • Shared properties file is a quick, lightweight option for smaller projects or if you don't want to maintain a separate BOM module.

Either way, you'll only need to update versions in one place—no more manual syncing between Maven and Gradle configs!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 13:04:09