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

求助:IntelliJ中gcloud run deploy部署Java Google Cloud HelloWorld失败

解决Google Cloud Run部署Java 17项目时Buildpack版本不兼容问题

1. 强制Buildpack使用Java 17 Runtime

Cloud Run默认Buildpack会选用Java 11,需明确指定Java 17版本,可通过以下三种方式实现:

方式一:部署命令直接指定

执行gcloud run deploy时添加版本指定参数:

gcloud run deploy YOUR_SERVICE_NAME --image gcr.io/YOUR_PROJECT_ID/YOUR_IMAGE_NAME --set-env-vars GOOGLE_RUNTIME_VERSION=17 --platform managed

方式二:添加system.properties文件

在项目根目录创建system.properties文件,写入以下内容:

java.runtime.version=17

Buildpack会自动识别该文件并切换至Java 17环境。

方式三:配置Maven编译插件版本

在项目的pom.xml中确保Maven编译插件指定正确的Java版本,避免本地编译与云端Buildpack版本冲突:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <source>17</source>
                <target>17</target>
                <release>17</release>
            </configuration>
        </plugin>
    </plugins>
</build>

2. 消除JAVA_HOME未设置警告

在system.properties中补充JAVA_HOME配置(可选,但可消除日志警告):

java.runtime.version=17
java.home=/usr/lib/jvm/java-17-openjdk-amd64

注:云端Buildpack的Java 17默认路径通常为上述值,若不确定可查看Cloud Build日志中的JDK实际路径。

3. 修复Maven MojoExecutionException

该错误多由编译版本不匹配导致,需确保:

  • IntelliJ中项目SDK设置为Java 17(路径:File > Project Structure > Project Settings > Project)
  • 本地执行mvn clean install编译通过,无版本相关报错
  • pom.xml中maven-compiler-plugin的source/target/release参数均设置为17(如步骤1.3所示)

验证部署

完成上述配置后,在IntelliJ中重新执行gcloud run deploy命令,检查Cloud Build日志,确认使用Java 17 Runtime且编译无报错即可完成部署。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 12:27:30