设置Java版本进行Maven构建 [英] Setting Java version for maven build

查看:35
本文介绍了设置Java版本进行Maven构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从shell脚本执行Maven构建时,我试图设置其Java版本.出于某种原因,它没有选择预期的Java版本.任何建议欢迎.

I am trying to set the java version for a maven build when I execute it from a shell script. Fot some reason it is not picking up the intended java version. Any advise welcome.

我在linux centos 7 os上运行.

I am running on a linux centos 7 os.

我从 https://jdk.java.net/14/下载了Java 14(Linux/x64),并将其提取到:

I have downloaded java 14 from https://jdk.java.net/14/ (Linux / x64), and extracted it to:

/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

推荐答案

我找到了解决方法:

如果我设置了,就可以了.Maven编译代码.

If I set, this it works. Maven compiles the code.

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

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

However, this is not ideal because the path to the install is hard-coded, so on other servers with a different JAVA_HOME location, it won't work. Is it possible to reference $JAVA_HOME from within the POM file? and do something like this?

    <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>

这篇关于设置Java版本进行Maven构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆