【问题标题】:Setting Java version for maven build为 Maven 构建设置 Java 版本
【发布时间】:2020-11-12 19:38:27
【问题描述】:

当我从 shell 脚本执行时,我正在尝试为 maven 构建设置 java 版本。出于某种原因,它没有选择预期的 java 版本。欢迎任何建议。

我在 linux centos 7 操作系统上运行。

我从https://jdk.java.net/14/(Linux / x64)下载了java 14,并解压到:

/usr/lib/jvm/jdk-14.0.2

pom.xml

<properties>
    <java.version>14</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <maven.compiler.release>${java.version}</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jsk.version>2.2.3</jsk.version>
    <start-class>com.nexct.approvalservice.NexctApprovalServiceApplication</start-class>
</properties>

<build>
    <plugins>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <fork>true</fork>
            </configuration>
        </plugin>

deploy.sh

export JAVA_HOME=/usr/lib/jvm/jdk-14.0.2
export PATH=$JAVA_HOME/bin:$PATH
echo $JAVA_HOME
echo | java -version
echo "maven build ..."
mvn clean install -V -DskipTests -Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2

输出:

/usr/lib/jvm/jdk-14.0.2
openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment (build 14.0.2+12-46)
OpenJDK 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)
maven build ...
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/apache-maven-3.6.3
Java version: 1.7.0_161, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.161-2.6.12.0.el7_4.x86_64/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "3.10.0-957.21.3.el7.x86_64", arch: "amd64", family: "unix"

[INFO] BUILD FAILURE
Compilation failure
[ERROR] javac: invalid flag: -parameters
[ERROR] Usage: javac <options> <source files>
[ERROR] use -help for a list of possible options

【问题讨论】:

  • 嗨@DhanukaPerera,感谢您的评论。这就是让我感到困惑的事情,我正在按照这些答案的建议做,但是 maven 没有使用 java 14。我设置了 JAVA_HOME,我还在 pom 中设置了 java 14。
  • 你安装了其他版本的java吗?
  • 在maven编译器插件配置中,尝试添加&lt;release&gt;${java.version}&lt;/release&gt;
  • @Lokesh 我加了&lt;release&gt;${java.version}&lt;/release&gt;,但没什么区别。

标签: java maven


【解决方案1】:

我找到了解决办法:

如果我设置,它会起作用。 Maven 编译代码。

    <executable>/usr/lib/jvm/jdk-14.0.2/bin/javac</executable>

但是,这并不理想,因为安装路径是硬编码的,因此在具有不同 JAVA_HOME 位置的其他服务器上,它将无法正常工作。是否可以从 POM 文件中引用 $JAVA_HOME?并做这样的事情?

    <executable><$JAVA_HOME>/bin/javac</executable>

例如

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <release>${java.version}</release>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <compilerVersion>${java.version}</compilerVersion>
                <fork>true</fork>
                <executable>/usr/lib/jvm/jdk-14.0.2/bin/javac</executable>
            </configuration>
        </plugin>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-04-12
  • 2012-09-07
  • 2013-11-03
  • 2018-05-18
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多