Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does maven find Java compiler?

Tags:

java

maven

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 (mvn install) find Java compiler (javac) installed on my machine? This POM file doesn't specify JDK in any way. Thanks for explanation.

EDIT: As a comparison, I am following next example in the book and this time, I received a compilation error because it can't find javax.My java file is in, as suggested in the book: src/main/java/org/sonatype/mavenbook/web/SimpleServlet.java

Now as I run mvn compile, and i got error message below. Why now maven can't find the compiler:

ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project simple-webapp: Compilation failure: Compilation failure:
[ERROR] /home/abigail/study/simple-webapp/src/main/java/org/sonatype/mavenbook/web/SimpleServlet.java:[4,1] package javax.servlet does not exist

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                         http://maven.apache.org/maven-    v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.simpleweb</groupId>
<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <finalName>simple-webapp</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>
like image 743
ling Avatar asked Aug 28 '14 05:08

ling


People also ask

How does Maven determine Java version?

Before moving on, we can check the default JDK version of Maven. Running the mvn -v command will show the Java version in which Maven runs.

Does Maven call javac?

Maven is not a java compiler. Maven runs the java compiler for you and configures all the paths. When you run mvn -X compile you will see the compiler details.

What compiler does Maven use?

The maven-compiler-plugin uses javac by default, and binds by default to the compile lifecycle phase.

Does Maven compile Java?

Build Java code This will run Maven, telling it to execute the compile goal. When it's finished, you should find the compiled . class files in the target/classes directory. The package goal will compile your Java code, run any tests, and finish by packaging the code up in a JAR file within the target directory.


2 Answers

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>
like image 87
Ankur Singhal Avatar answered Sep 21 '22 15:09

Ankur Singhal


Maven uses the environment variable JAVA_HOME to find the JDK.

like image 33
Koraktor Avatar answered Sep 21 '22 15:09

Koraktor