在推送到Heroku时编译致命错误:无效的目标版本:11

3

我正在尝试将后端应用部署到Heroku,但运行以下命令时遇到问题:

git push --force heroku-be-app splitting-staging-be:master

我收到了这个错误信息

       [INFO] Downloaded from central: https://repo.maven.apache.org/maven2/com/thoughtworks/qdox/qdox/2.0-M9/qdox-2.0-M9.jar (317 kB at 7.6 MB/s)
       [INFO] Changes detected - recompiling the module!
       [INFO] Compiling 24 source files to /tmp/build_4e875976/target/classes
       [INFO] ------------------------------------------------------------------------
       [INFO] BUILD FAILURE
       [INFO] ------------------------------------------------------------------------
       [INFO] Total time:  12.593 s
       [INFO] Finished at: 2020-12-21T15:28:14Z
       [INFO] ------------------------------------------------------------------------
       [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project backend: Fatal error compiling: invalid target release: 11 -> [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
 !     ERROR: Failed to build app with Maven
       We're sorry this build is failing! If you can't find the issue in application code,
       please submit a ticket so we can help: https://help.heroku.com/
 !     Push rejected, failed to compile Java app.
 !     Push failed

这是我的pom文件:
<?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>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>vs</groupId>
    <artifactId>backend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>backend</name>
    <description>Main server voor van streek project</description>

    <properties>
        <java.version>15</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-core</artifactId>
      <version>5.4.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>5.3.1</version>
    </dependency>
    <dependency>
      <groupId>com.sun.mail</groupId>
      <artifactId>jakarta.mail</artifactId>
      <version>1.6.5</version>
    </dependency>
  </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>15</source>   <!-- maven.compiler.source  -->
                    <target>15</target> <!-- maven.compiler.target -->
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

这个问答没能帮助你解决问题吗? - helvete
5个回答

4

我通过添加一个新文件system.properties并在文件中添加内容java.runtime.version=11(因为我使用的是JDK 11)来解决了这个问题。

java.runtime.version=11

1
文件与 pom.xml 放在一起非常重要,我曾经因为自动将其放入 src/main/resources 中而遇到了麻烦,因为它似乎找不到。 - ch1ll

3
我建议着重研究Maven为什么会抱怨无效的11版本(Fatal error compiling: invalid target release: 11)。
从您的pom.xml配置文件中可以看出,您明确想要使用15版本,所以我建议您调查一下为什么11版本存在问题。
检查$JAVA_HOME环境变量、system.properties文件等。
还可以尝试调试路线建议在官方文档中类似主题的问答中提到的方法。

1

这是我今天的经历。

我一直在使用jdk 1.8,但现在需要安装jdk 1.11。

然后,通过我的shell脚本文件dep.sh运行mvnw时,构建jar失败了。

#!/bin/bash

MVN=$(pwd)/mvnw
echo "[1/2] Building app.jar ..."
$MVN clean package -Dmaven.test.skip=true 
  • dep.sh 调用 mvnw 脚本文件并使用目标为 clean package

错误

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile
  (default-compile) on project dictation:
  Fatal error compiling: invalid target release: 11 -> [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

我的 pom.xml 配置了jdk 1.11,但这并不能解决构建错误。

<project ...>
  <properties>
    <java.version>11</java.version>
  </properties>
  <build>
    <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <configuration>
          <source>11</source><!-- 1.11 not working -->
          <target>11</target><!-- 1.11 not working -->
       </configuration>
      </plugin>
    </plugins>
  </build>
</project>
  • 构建失败

当我安装jdk 1.8时,JAVA_HOME被设置为/my/jdk/1.8/dir,用于打包jar。

我在dep.sh中将JAVA_HOME设置为/my/jdk/1.111/dir,它可以正常工作而没有错误。

#!/bin/bash

JAVA_HOME="/C/Program Files/Java/jdk-11.0.9"

MVN=$(pwd)/mvnw
echo "[1/2] Building app.jar ..."
$MVN clean package -Dmaven.test.skip=true

1
您必须使用Heroku支持的特定版本的Java。以下是支持的版本:
  • Java 7 - 1.7.0_292
  • Java 8 - 1.8.0_282
  • Java 11 - 11.0.10
  • Java 13 - 13.0.6
  • Java 15 - 15.0.2
请在system.properties文件中设置java.runtime.version属性,如果没有该文件,请创建一个。
java.runtime.version = SUPPORTED_VERSION
了解更多信息,请参见heroku-java-support

这个解决方案解决了我的问题。 - Cuong Vo

0
创建 system.properties 文件并添加 java.runtime.version=11。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接