Difference between mvn test-compile and mvn compile
The mvn test-compile
and mvn compile
commands in Maven serve different purposes in the build lifecycle, focusing on compiling different sets of source code. Here’s a detailed comparison of the two:
mvn compile
- Purpose: This command compiles the main source code of your project.
- Scope: It processes the source code located in the
src/main/java
directory and generates the compiled classes in thetarget/classes
directory. - Usage: Run this command when you want to compile the main application code.
- Typical Command:
mvn compile
mvn test-compile
- Purpose: This command compiles the test source code of your project.
- Scope: It processes the source code located in the
src/test/java
directory and generates the compiled classes in thetarget/test-classes
directory. This command implicitly includes thecompile
phase, so it compiles both the main and the test source code. - Usage: Run this command when you want to compile the test code in preparation for running tests.
- Typical Command:
mvn test-compile
Detailed Comparison
Aspect | mvn compile | mvn test-compile |
---|---|---|
Primary Focus | Compiles main source code | Compiles test source code |
Source Directory | src/main/java | src/test/java |
Output Directory | target/classes | target/test-classes |
Lifecycle Phases | Executes up to the compile phase | Executes up to the test-compile phase (includes compile ) |
Dependencies | Main dependencies | Main and test dependencies |
When to Use | When you need to compile the application code | When you need to compile test code (and application code if not already compiled) |
Maven Build Lifecycle
Understanding the Maven build lifecycle can clarify why these commands exist:
- Default Lifecycle Phases:
validate
: Validate the project is correct and all necessary information is available.initialize
: Initialize build state, such as setting properties.generate-sources
: Generate any source code for inclusion in compilation.process-sources
: Process the source code, for example, filtering any values.generate-resources
: Generate resources for inclusion in the package.process-resources
: Copy and process the resources into the destination directory, ready for packaging.compile
: Compile the source code of the project.process-classes
: Post-process the generated files from compilation, for example, to do bytecode enhancement on Java classes.generate-test-sources
: Generate any test source code for inclusion in compilation.process-test-sources
: Process the test source code.generate-test-resources
: Create resources for testing.process-test-resources
: Copy and process the test resources into the test destination directory.test-compile
: Compile the test source code into the test destination directory.process-test-classes
: Post-process the generated test files from compilation.test
: Run tests using a suitable unit testing framework. These tests should not require the code to be packaged or deployed.prepare-package
: Perform any operations necessary to prepare a package before the actual packaging occurs. This may involve performing tests on the compiled and processed code.package
: Take the compiled code and package it in its distributable format, such as a JAR.verify
: Run any checks to verify the package is valid and meets quality criteria.install
: Install the package into the local repository, for use as a dependency in other projects locally.deploy
: Copy the final package to the remote repository for sharing with other developers and projects.
Practical Implications
- When Running Tests: Use
mvn test-compile
to ensure both main and test code are compiled. This is typically followed bymvn test
to execute the tests. - When Building the Project: Use
mvn compile
to compile only the main code if you are not running tests immediately.
Published on: Jun 23, 2024, 06:44 AM