mvn compile and target/classes
mvn compile is the shortest useful command of the default lifecycle: it runs the phases up to compile, which means the resources of the project are copied and the sources are compiled, and nothing else. No test runs, no jar is written. This article runs it twice on the same project, reads the difference, and then breaks the source on purpose to see how a compiler error looks inside a Maven log.
The project
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>calc</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
</properties>
</project>
src/main/java/com/example/calc/Calc.java
package com.example.calc;
public class Calc {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println("1 + 2 = " + add(1, 2));
}
}
The first compile
The log names two goals, and both are the defaults of the jar packaging: the resources goal runs even though the project has no resources, and says so.
mvn -B compile > build.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" build.log
status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:calc >--------------------------
[INFO] Building calc 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.4.0:resources (default-resources) @ calc ---
[INFO] skip non existing resourceDirectory <project>/src/main/resources
[INFO]
[INFO] --- compiler:3.15.0:compile (default-compile) @ calc ---
[INFO] Recompiling the module because of <REASON>.
[INFO] Compiling 1 source file with javac [debug release 21] to target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
Recompiling the module because of changed source code. is the compiler plugin deciding that the classes in target are older than the sources. javac [debug release 21] are the options it passes: debug for the line numbers, release 21 from the property.
The second compile
Nothing changed, so the second run has nothing to do and says that in one line. The build is still a success: an incremental build that compiles nothing is the normal case, not a problem.
mvn -B compile > build.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" build.log
status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:calc >--------------------------
[INFO] Building calc 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.4.0:resources (default-resources) @ calc ---
[INFO] skip non existing resourceDirectory <project>/src/main/resources
[INFO]
[INFO] --- compiler:3.15.0:compile (default-compile) @ calc ---
[INFO] Nothing to compile - all classes are up to date.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
The decision is made by timestamps, not by hashes: a source older than its class file is up to date. That has two consequences worth knowing. Touching a source without changing it compiles it again. And a build after mvn clean always compiles everything, because the folder it compares against is gone. target/maven-status keeps the list of what was compiled where, and the layout article lists that folder among the output of a build.
What is in target/classes
find target/classes -type f | sort
target/classes/com/example/calc/Calc.class
One class file, in the package folder, with the name of the class. That folder is the class path of a run until the project becomes a jar, and it is what the package article puts into one.
A compile error
The same class with a semicolon missing, and everything else as before:
src/main/java/com/example/calc/Calc.java
package com.example.calc;
public class Calc {
public static int add(int a, int b) {
return a + b
}
}
mvn -B compile > bad.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" bad.log
status: 1
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:calc >--------------------------
[INFO] Building calc 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.4.0:resources (default-resources) @ calc ---
[INFO] skip non existing resourceDirectory <project>/src/main/resources
[INFO]
[INFO] --- compiler:3.15.0:compile (default-compile) @ calc ---
[INFO] Recompiling the module because of <REASON>.
[INFO] Compiling 1 source file with javac [debug release 21] to target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] <project>/src/main/java/com/example/calc/Calc.java:[5,21] ';' expected
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.15.0:compile (default-compile) on project calc: Compilation failure
[ERROR] <project>/src/main/java/com/example/calc/Calc.java:[5,21] ';' expected
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
The error appears twice: once in the block the compiler plugin prints between its dashed lines, and once in the Failed to execute goal line that names the goal, the plugin version, the execution id and the project. Both carry the file and the position as [line,column], which every editor and every IDE reads as a link.
The status is 1 and the build stopped at the first failing goal: the phases after compile did not run at all. Putting the semicolon back and running the command again compiles the class, and the output is the one of the first compile.
Where to go next
The test phase continues from compile to test, and the compiler article configures the plugin whose two lines this page only read.
Article author: Arthur Isaev