How to change maven java home

前端 未结 9 2226
太阳男子
太阳男子 2020-12-04 23:13

I want to change maven java home which is open jdk with sun jdk. How can I do it ?

root@ak-EasyNote-TM98:~# mvn -version
Apache Maven 3.0.4
Maven home: /usr/         


        
相关标签:
9条回答
  • 2020-12-04 23:52

    Even if you install the Oracle JDK, your $JAVA_HOME variable should refer to the path of the JRE that is inside the JDK root. You can refer to my other answer to a similar question for more details.

    0 讨论(0)
  • 2020-12-05 00:03

    The best way to force a specific JVM for MAVEN is to create a system wide file loaded by the mvn script.

    This file is /etc/mavenrc and it must declare a JAVA_HOME environment variable pointing to your specific JVM.

    Example:

    export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
    

    If the file exists, it's loaded.

    Here is an extract of the mvn script in order to understand :

      if [ -f /etc/mavenrc ] ; then
        . /etc/mavenrc
      fi
    
      if [ -f "$HOME/.mavenrc" ] ; then
        . "$HOME/.mavenrc"
      fi
    

    Alternately, the same content can be written in ~/.mavenrc

    0 讨论(0)
  • 2020-12-05 00:04

    If you are in Linux, set JAVA_HOME using syntax export JAVA_HOME=<path-to-java>. Actually it is not only for Maven.

    0 讨论(0)
  • 2020-12-05 00:09

    I am using Mac and none of the answers above helped me. I found out that maven loads its own JAVA_HOME from the path specified in: ~/.mavenrc

    I changed the content of the file to be: JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

    For Linux it will look something like:
    JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre

    0 讨论(0)
  • 2020-12-05 00:09

    I have two Java versions on my Ubuntu server 14.04: java 1.7 and java 1.8.

    I have a project that I need to build using java 1.8.

    If I check my Java version using java -version

    I get

    java version "1.8.0_144"
    

    But when I did mvn -version I get:

    Java version: 1.7.0_79, vendor: Oracle Corporation
    

    To set the mvn version to java8

    I do this:

    export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre/
    

    Then when I do mvn -version I get:

    Java version: 1.8.0_144, vendor: Oracle Corporation
    
    0 讨论(0)
  • 2020-12-05 00:10

    Great helps above, but if you having the similar environment like I did, this is how I get it to work.

    • having a few jdk running, openjdk, oracle jdk and a few versions.
    • install apache-maven via yum, package is apache-maven-3.2.1-1.el6.noarch

    Edit this file /etc/profile.d/apache-maven.sh, such as the following, note that it will affect the whole system.

    $ cat /etc/profile.d/apache-maven.sh
    MAVEN_HOME=/usr/share/apache-maven
    M2_HOME=$MAVEN_HOME
    PATH=$MAVEN_HOME/bin:$PATH
    # change below to the jdk you want mvn to reference.
    JAVA_HOME=/usr/java/jdk1.7.0_40/
    export MAVEN_HOME
    export M2_HOME
    export PATH
    export JAVA_HOME
    
    0 讨论(0)
提交回复
热议问题