LinuxCommandLibrary

mvn-compile

Compile Maven project source code

TLDR

Compile the project's source code

$ mvn compile
copy

Clean compiled files and recompile
$ mvn clean compile
copy

Compile a specific module in a multi-module project
$ mvn compile [[-pl|--projects]] [module_name]
copy

Skip tests while compiling
$ mvn compile [[-D|--define]] skipTests
copy

SYNOPSIS

mvn [<options>] compile

PARAMETERS

-D<property>=<value>
    Defines a system property that can influence the build. For example, -Dmaven.compiler.source=11 sets the source compatibility for compilation.

-P <profiles>
    Activates one or more specific build profiles defined in the pom.xml or Maven settings.

-U, --update-snapshots
    Forces a check for updated releases and snapshots from remote repositories, ensuring the latest dependencies are used.

-B, --batch-mode
    Runs Maven in non-interactive batch mode, suppressing user prompts. Ideal for continuous integration environments.

-e, --errors
    Produces execution error messages, providing more detailed stack traces for debugging.

-X, --debug
    Generates extensive debug output for the entire Maven execution, useful for deep troubleshooting.

-q, --quiet
    Suppresses non-error messages, producing minimal output.

-fn, --fail-never
    Allows the build to continue and finish even if some modules or phases fail.

-pl, --projects <arg>
    Builds only the specified reactor projects (modules) instead of all projects in a multi-module build.

-T, --threads <arg>
    Builds projects concurrently, specifying the number of threads (e.g., -T 4C for 4 CPU cores, -T 2.5C for 250% of CPU cores, or -T 4 for 4 threads).

DESCRIPTION

The command mvn compile executes the compile phase within the Maven default build lifecycle. Its primary function is to transform a project's source code, typically Java .java files found in src/main/java, into compiled bytecode .class files. These generated class files are then placed in the target/classes directory within the project's root.

Before executing the compile phase, Maven automatically runs all preceding phases in the default lifecycle, which include validate, initialize, generate-sources, process-sources, generate-resources, and process-resources. This ensures that all necessary preliminary steps, such as resource copying and source generation, are completed.

The mvn compile command relies heavily on the project's pom.xml for configuration, including dependency declarations and the setup of the maven-compiler-plugin. It is a fundamental step in building any Java application with Maven, as subsequent phases like package or install require the compiled classes to be present. It's important to note that this command specifically compiles main project sources and does not handle test sources, which are processed by the test-compile goal.

CAVEATS

The command requires a pom.xml file to be present in the current directory or a parent directory. A Java Development Kit (JDK) must be installed and correctly configured in the system's PATH. If the source files have not changed since the last compilation, Maven might not recompile them unless a clean goal is executed first or the compiler plugin is explicitly configured to force recompilation. Large projects with many dependencies can experience slow compilation times, especially during the initial download of dependencies.

DEFAULT COMPILER PLUGIN

Maven uses the maven-compiler-plugin by default to handle the compilation process. The version and specific configurations (like source/target Java versions, encoding, etc.) of this plugin can be declared and customized within the <build> section of the project's pom.xml file.

DEPENDENCY RESOLUTION

During the compile phase, Maven automatically resolves and downloads all project dependencies declared in the pom.xml from configured Maven repositories (local and remote). These dependencies are then added to the compilation classpath, ensuring that the source code can correctly reference external libraries.

HISTORY

Maven, originally released in 2004, emerged from the Apache Turbine project as a build automation tool. It aimed to simplify and standardize the build process for Java projects, moving away from more complex, imperative build scripts like those used in Ant. The compile goal has been a foundational part of Maven's lifecycle since its early versions, representing the core step of source code compilation. Its design principles, focusing on convention over configuration and a well-defined lifecycle, have remained consistent, making mvn compile a stable and essential command in the Java development ecosystem.

SEE ALSO

mvn clean, mvn test-compile, mvn package, mvn install, javac(1), java(1), gradle build

Copied to clipboard