简体   繁体   中英

Setting Java version for maven build

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.

I am running on a linux centos 7 os.

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

output:

/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

I found a solution:

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

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

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>

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM