How does maven find Java compiler?

前端 未结 4 1082
天涯浪人
天涯浪人 2021-01-06 10:46

I am new to learn maven by following this example given by sonatype\'s book.This is a simplest pom.xml file (works fine). But my question is, how does maven

相关标签:
4条回答
  • 2021-01-06 11:33

    Maven uses the environment variable JAVA_HOME to find the JDK.

    0 讨论(0)
  • 2021-01-06 11:37

    Since maven itself is a java app, it would just default to the jdk that runs the maven goal.

    For more compilation options, you'd have to use:

    http://maven.apache.org/plugins/maven-compiler-plugin/

    Take a look at this also:

    http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html

    EDIT:

    To your followup question: If you look in mvn executable (by default in: /usr/bin/mvn), you'd see it is a rather lengthy bash scriptie that spends a significant amount of time finding where the jdk is. Of course, you can set the JAVA_HOME system property and "help" the script, but even if you don't have it, it is going to attempt finding it, and only when it fails, it is going to spew an error.

    From the /usr/bin/mvn:

    if [ -z "$JAVA_HOME" ]; then
      javaExecutable="`which javac`"
      if [ -n "$javaExecutable" -a ! "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
        # readlink(1) is not available as standard on Solaris 10.
        readLink=`which readlink`
        if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
          javaExecutable="`readlink -f \"$javaExecutable\"`"
          javaHome="`dirname \"$javaExecutable\"`"
          javaHome=`expr "$javaHome" : '\(.*\)/bin'`
          JAVA_HOME="$javaHome"
          export JAVA_HOME
        fi
      fi
    

    And

    if [ -z "$JAVACMD" ] ; then
      if [ -n "$JAVA_HOME"  ] ; then
        if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
          # IBM's JDK on AIX uses strange locations for the executables
          JAVACMD="$JAVA_HOME/jre/sh/java"
        else
          JAVACMD="$JAVA_HOME/bin/java"
        fi
      else
        JAVACMD="`which java`"
      fi
    fi
    

    Actually, most of the code of the script is concerned with determining which java to use and where that java is located. However, once you run the mvn executable, that doesn't limit it to using the same jdk for compilation/running that it itself is run with. Herein is where the mvn compiler plugin comes into play, where you can specify additional jdk installations to use.

    0 讨论(0)
  • 2021-01-06 11:45

    If you go to this path, say with maven version 3.2.3

    apache-maven-3.2.3-bin\apache-maven-3.2.3\bin
    

    opne the mvn.bat, you can see below

    @REM Maven2 Start Up Batch script

    @REM Required ENV vars:

    @REM JAVA_HOME - location of a JDK home dir

    Clearly it picks JAVA_HOME, from your environment variables. Below code snippet from the file.

    @REM ==== START VALIDATION ====
    if not "%JAVA_HOME%" == "" goto OkJHome
    :OkJHome
    if exist "%JAVA_HOME%\bin\java.exe" goto chkMHome
    :chkMHome
    if not "%M2_HOME%"=="" goto valMHome
    

    for issue, javax.servlet

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    

    or whichever available

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    
    0 讨论(0)
  • 2021-01-06 11:47

    So i just got maven to pick up different Java location from my default:

    Java8

    [tomek:~] $ java -version
    java version "1.8.0_144"
    Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
    Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
    

    but maven picked up Java9

    [tomek:~] $ mvn -version
    Apache Maven 3.3.9
    Maven home: /usr/share/maven
    Java version: 9-internal, vendor: Oracle Corporation
    Java home: /usr/lib/jvm/java-9-openjdk-amd64
    

    JAVA_HOME was not set:

    [tomek:~] $ echo "Java HOME: $JAVA_HOME"
    Java HOME:
    

    So I suspect it just bring first alternative on ubuntu (ignoring users choice):

    [tomek:~] $ sudo update-alternatives --config java
    There are 3 choices for the alternative java (providing /usr/bin/java).
    
    Selection    Path                                            Priority   
    Status
    ------------------------------------------------------------
     0            /usr/lib/jvm/java-9-openjdk-amd64/bin/java       1091       auto mode
     * 1            /opt/java/jdk1.8.0_144/bin/java                  1         manual mode
       2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
      3            /usr/lib/jvm/java-9-openjdk-amd64/bin/java       1091      manual mode
    
    0 讨论(0)
提交回复
热议问题