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

如何将Spring Boot中的Gradle依赖转换为Maven依赖?

将Spring Boot Gradle配置转换为Maven配置

以下是对应你提供的Gradle配置的完整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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 继承Spring Boot父项目,统一管理依赖版本,对应Gradle的Spring Boot插件功能 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.4</version>
        <relativePath/>
    </parent>

    <!-- 项目基础信息,与Gradle中的group、version对应 -->
    <groupId>com.arhohuttunen</groupId>
    <artifactId>spring-boot-integration-testing</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-integration-testing</name>
    <description>Spring Boot Integration Testing Example</description>

    <!-- 编译版本配置,对应Gradle的sourceCompatibility和targetCompatibility -->
    <properties>
        <java.version>11</java.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <testcontainers.version>1.17.6</testcontainers.version>
    </properties>

    <!-- 依赖管理,导入Testcontainers的BOM,对应Gradle的dependencyManagement配置 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.testcontainers</groupId>
                <artifactId>testcontainers-bom</artifactId>
                <version>${testcontainers.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- 对应Gradle的implementation依赖 -->
        <dependency>
            <groupId>org.javamoney</groupId>
            <artifactId>moneta</artifactId>
            <version>1.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <!-- 对应Gradle的compileOnly依赖 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>compileOnly</scope>
        </dependency>

        <!-- 对应Gradle的runtimeOnly依赖 -->
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- 对应Gradle的annotationProcessor依赖 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>annotationProcessor</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <scope>annotationProcessor</scope>
        </dependency>

        <!-- 对应Gradle的testImplementation依赖 -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>mockwebserver</artifactId>
            <version>4.10.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>postgresql</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 对应Gradle的testRuntimeOnly依赖 -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.10.0</version>
            <scope>test</scope>
            <type>runtime</type>
        </dependency>
    </dependencies>

    <!-- 配置JUnit Platform运行测试,对应Gradle的test.useJUnitPlatform() -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useJUnitPlatform>true</useJUnitPlatform>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

关键对应说明:

  • Spring Boot依赖管理:通过继承spring-boot-starter-parent实现,替代Gradle中的Spring Boot插件和依赖管理插件功能。
  • 编译版本:在<properties>中配置maven.compiler.source和maven.compiler.target,对应Gradle的sourceCompatibility和targetCompatibility。
  • 依赖作用域映射:
    • implementation → Maven默认compile作用域(无需显式声明)
    • compileOnly → <scope>compileOnly</scope>
    • runtimeOnly → <scope>runtime</scope>
    • annotationProcessor → <scope>annotationProcessor</scope>
    • testImplementation → <scope>test</scope>
    • testRuntimeOnly → <scope>test</scope> + <type>runtime</type>
  • Testcontainers BOM:在<dependencyManagement>中导入BOM,对应Gradle的dependencyManagement.imports配置。
  • JUnit平台测试:通过maven-surefire-plugin配置useJUnitPlatform=true,对应Gradle的test.useJUnitPlatform()。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 22:57:55