Lifecycle and scopes
What you will learn
A Maven phase is a milestone in a lifecycle; a goal is a task implemented by a plugin. Dependency scopes determine which classpaths a dependency belongs to and how it can propagate.
- Predict the phases reached by test, package and verify.
- Select compile, provided, runtime or test scope for a dependency.
- Separate unit tests from integration tests and their verification.
Prerequisite: Java packages, classpath and basic terminal compilation. Maven CLI examples are static and not executed in this batch. The project is local and fictional; use only reviewed source and approved cached dependencies.
Compare phase requests
# Run in a reviewed disposable project, with approved local cache.mvn -o testmvn -o packagemvn -o verifyIllustrative Maven commands, not executed. test should run the unit-test phase, package should produce the JAR, and verify should also reach integration verification if bound. No claim of discovered tests.
The default lifecycle
The default lifecycle progresses through validation, compilation, tests, packaging and verification, then installation and deployment. Asking for package includes earlier phases; asking for test does not package the project. Clean is a different lifecycle. The packaging and plugin bindings determine what work runs. A goal can be invoked directly without running the default lifecycle through a phase; this is not equivalent to running all previous phases.
Dependency scope and transitivity
Compile is the default: main compilation, tests and runtime. Provided is available for compilation and tests, with runtime expected from the environment, and is not transitive. Runtime is absent from the main compile classpath but available at runtime and tests. Test is limited to test compilation/execution and not transitive. System resembles provided but uses a local path, making portability and trust harder; avoid it. Import is special: only a POM dependency in dependencyManagement, not a classpath scope. Transitive resolution combines the scopes of dependency edges; inspect the actual tree rather than assuming every reachable artifact is included.
Test boundaries
Surefire conventionally runs unit tests during test. Failsafe can bind integration-test and verify; use verify so post-integration-test cleanup has a chance to run before test failures are reported. Merely declaring a plugin version does not bind arbitrary goals. Configure a compatible test framework, actual test sources and naming rules (for example *Test and *IT), and assert the resulting reports. A successful build without discovered tests proves nothing about those tests. Do not use install or deploy for these exercises; install writes the local repository, deploy writes a remote repository.
Declare scoped dependencies
<dependencies> <!-- Fictional artifacts: not a downloadable dependency list. --> <dependency> <groupId>example.bytemind</groupId> <artifactId>warehouse-core</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>example.bytemind</groupId> <artifactId>test-fixtures</artifactId> <version>1.0.0</version> <scope>test</scope> </dependency></dependencies>Fictional fragment for a reviewed project; do not resolve these names remotely. warehouse-core defaults to compile, while test-fixtures is excluded from production runtime and compilation.
Bind integration-test goals
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.5.4</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions></plugin>Illustrative version-pinned Failsafe configuration, not executed. It belongs within build/plugins. A compatible test framework and integration tests must also be supplied; run verify rather than stopping at integration-test.
Common mistakes
A runtime dependency cannot supply a class imported by main source during compilation. A test dependency cannot be relied on by the deployed application. Calling integration-test directly may skip cleanup and result verification; use verify for the configured lifecycle.
Practice
A warehouse app imports its own core API, loads a driver at runtime without importing driver types, and uses a test assertion library. Choose scopes and the earliest standard command that includes configured integration-test verification. Explain why compile alone is insufficient.
Use the manual practice hints; explain your reasoning before consulting the private review solution.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Lifecycle and scopes — knowledge check
Maven 3 model and build engineering knowledge check; source-reviewed, not Maven-executed.
Which request reaches earlier default phases through package?
Which scope is appropriate for a library used only by tests?
Which scope makes a driver available at runtime but not main compile?
Which command includes Failsafe verification if the goals are configured?
Is a goal the same as a lifecycle phase?