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

创建GCP Spring Cloud Function时遇net.bytebuddy相关启动错误求助

问题

我正在为GCP开发可作为后台函数运行的Spring Cloud Function,因后续需使用数据库逻辑提前引入了JPA和H2依赖,但目前还没创建实体类、仓库类等相关代码。项目只有两个类:一个带main方法的主类,一个实现打印输入字符串功能的任务类。执行mvn function:run启动时触发了异常。


执行命令

mvn function:run

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yuknis</groupId>
    <artifactId>in</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>in</name>
    <description>Receive a message and perform any transformation on it</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2022.0.2</spring-cloud.version>
        <spring-cloud-function.version>4.0.3-SNAPSHOT</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-adapter-gcp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </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-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>8.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <!-- <scope>test</scope> -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <outputDirectory>target/deploy</outputDirectory>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.cloud</groupId>
                        <artifactId>spring-cloud-function-adapter-gcp</artifactId>
                        <version>${spring-cloud-function.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.functions</groupId>
                <artifactId>function-maven-plugin</artifactId>
                <version>0.9.1</version>
                <configuration>
                    <functionTarget>org.springframework.cloud.function.adapter.gcp.GcfJarLauncher</functionTarget>
                    <port>8080</port>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.5.0</version>
                <configuration>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.5.0</version>
                <configuration>
                </configuration>
            </plugin>
        </plugins>
    </reporting>
</project>

application.yaml

spring:
  config.activate.on-profile: default
  datasource:
    url: jdbc:h2:mem:mydb
    username: redacted
    password: redacted
    driverClassName: org.h2.Driver
  data.jdbc.repositories.enabled: false
  jpa:
    spring.jpa.database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update
    show-sql: true
    properites:
      hibernate:
        format_sql: true

resources/META-INF/MANIFEST.MF

Main-Class: com.yuknis.in.InApplication

InApplication.java

package com.yuknis.in;

import java.util.function.Consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.yuknis.in.task.ProcessFileTask;

/**
 * Receives a message and performs any transformation on it.
 */
@SpringBootApplication
public class InApplication {

    /**
     * The main entry point of the application.
     * 
     * @param args Command line arguments passed to the application.
     */
    public static void main(String[] args) {

        SpringApplication.run(InApplication.class, args);

    }

    /**
     * Provides the Java Function which contains the code for the task.
     * 
     * @return The function containing the logic needed to acomplish the task.
     */
    @Bean
    Consumer<String> executeTask() {

        return new ProcessFileTask();

    }

}

ProcessFileTask.java

package com.yuknis.in.task;

import java.util.function.Consumer;

/**
 * The function which is used to process the incomming message when the task is triggered.
 */
public class ProcessFileTask implements Consumer<String> {

    /**
     * Accepts a message and commits it's contens to the database.
     * 
     */
    @Override
    public void accept(String t) {
        
        System.out.println(String.format("%s", t));

    }
    
}

完整错误信息

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.413 s
[INFO] Finished at: 2023-05-05T10:59:26-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.google.cloud.functions:function-maven-plugin:0.9.1:run (default-cli) on project in: Could not invoke function: Could not construct an instance of org.springframework.cloud.function.adapter.gcp.GcfJarLauncher: java.lang.reflect.InvocationTargetException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: net/bytebuddy/NamingStrategy$SuffixingRandom$BaseNameResolver: net.bytebuddy.NamingStrategy$SuffixingRandom$BaseNameResolver -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

解决方案

错误核心是ByteBuddy依赖缺失,导致Hibernate无法创建EntityManagerFactory。结合你的场景,提供两种修复方案:

方案一:移除未使用的JPA依赖

你目前还没编写实体类和仓库代码,完全不需要JPA相关依赖,直接删除pom.xml中以下依赖项:

  • spring-boot-starter-data-jpa
  • spring-boot-starter-data-jdbc(如果暂时不需要JDBC也可移除)

同时注释或删除application.yaml中的JPA配置段,简化配置内容。

方案二:补充ByteBuddy依赖

如果需要保留JPA依赖以便后续开发,在pom.xml的<dependencies>节点中添加ByteBuddy依赖:

<dependency>
    <groupId>net.bytebuddy</groupId>
    <artifactId>byte-buddy</artifactId>
    <!-- 版本选择与Spring Boot 3.0.6兼容的版本,可直接使用Spring Boot依赖管理中的默认版本 -->
</dependency>

额外修复点

  1. application.yaml中存在拼写错误:properites应改为properties,否则Hibernate的格式化SQL配置不会生效。
  2. pom.xml重复引入了spring-boot-starter-test,删除其中一个即可,避免依赖冗余。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 22:04:51