设置Maven构建的Java版本

huangapple go评论83阅读模式
英文:

Setting Java version for maven build

问题

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
英文:

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

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

&lt;build&gt;
	&lt;plugins&gt;

		&lt;plugin&gt;
			&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
			&lt;configuration&gt;
				&lt;source&gt;${java.version}&lt;/source&gt;
				&lt;target&gt;${java.version}&lt;/target&gt;
				&lt;fork&gt;true&lt;/fork&gt;
			&lt;/configuration&gt;
		&lt;/plugin&gt;

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 &quot;maven build ...&quot;
mvn clean install -V -DskipTests -Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2

output:

/usr/lib/jvm/jdk-14.0.2
openjdk version &quot;14.0.2&quot; 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: &quot;linux&quot;, version: &quot;3.10.0-957.21.3.el7.x86_64&quot;, arch: &quot;amd64&quot;, family: &quot;unix&quot;

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

答案1

得分: 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>
英文:

I found a solution:

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

	&lt;executable&gt;/usr/lib/jvm/jdk-14.0.2/bin/javac&lt;/executable&gt;

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?

	&lt;executable&gt;&lt;$JAVA_HOME&gt;/bin/javac&lt;/executable&gt;

e.g.

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

huangapple
  • 本文由 发表于 2020年7月23日 14:25:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63048137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定