Maven告诉我“致命错误编译:无效的目标版本”,尽管Java版本正确

时间:2019-02-17 11:29:34

标签: java maven raspberry-pi

要么我像地狱一样愚蠢,错过了很明显的东西,要么我的Rasberry Pi做得很奇怪。我只是在尝试编译一个简单的Java项目,尽管我安装了Java 9,但Maven一直告诉我B[n].x

struct b

我还检查了java和javac的路径,但两者似乎也是正确的:

Fatal error compiling: invalid target release: 9.0
pi@Harald:~ $ mvn -v
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 9-Raspbian, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-9-openjdk-armhf
Default locale: de_DE, platform encoding: UTF-8
OS name: "linux", version: "4.14.79-v7+", arch: "arm", family: "unix"

我想念什么?

See full error log

1 个答案:

答案 0 :(得分:0)

  1. 确保将JAVA_HOME设置为JDK 9。
  2. 指定maven-compiler-plugin源/目标/发行版属性:
<properties>
    <maven.compiler.source>1.9</maven.compiler.source>
    <maven.compiler.target>1.9</maven.compiler.target>
    <maven.compiler.release>9</maven.compiler.release>
</properties>
  1. 配置maven-compiler-pluginmaven-toolchains-plugin
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-toolchains-plugin</artifactId>
                <version>1.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-toolchains-plugin</artifactId>
            <configuration>
                <toolchains>
                    <jdk>
                        <version>1.9</version>
                        <vendor>oracle</vendor>
                    </jdk>
                </toolchains>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>toolchain</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

注释:

  • 要配置工具链,请创建~/.m2/toolchains.xml并添加以下内容:
<toolchains>
    <toolchain>
        <type>jdk</type>
        <provides>
            <version>1.9</version>
            <vendor>oracle</vendor>
        </provides>
        <configuration>
            <jdkHome>/path/to/your/jdk-9</jdkHome>
        </configuration>
    </toolchain>
</toolchains>
  • 要进行实验而不更新pom.xml:第一个符号链接/ bin / javac9到 / opt / jdk-9 / bin / javac和/ opt / jdk-9到JDK 9版本 当前正在使用。然后尝试以下命令:
mvn -Dmaven.compiler.fork -Dmaven.compiler.executable=javac9 install