You build code with Maven by running mvn from the directory containing your pom.xml, letting Maven read that file and produce the artifact in target/. The key condition is that Maven must find exactly one POM at the project root, and your JAVA_HOME must point at a JDK compatible with the project's maven.compiler.release or source/target settings. If both hold, mvn compile compiles main sources and mvn package compiles, tests, and bundles the result. Nearly every "Maven won't build" problem traces back to a missing POM, a wrong JDK, or a repository that can't be reached.
The rest of this guide walks the order that actually works: verify the toolchain, pick the right lifecycle phase for what you're trying to get, handle tests and dependencies deliberately, and read failures by their cause rather than by their noise.
What Maven Means by "Build"
Maven is a build tool driven by conventions. It assumes your sources live in src/main/java, your tests in src/test/java, and your output goes to target/. The pom.xml describes the project's coordinates (groupId, artifactId, version), its dependencies, plugins, and build settings. When you run mvn, Maven reads that description, resolves the plugin set it needs, and executes lifecycle phases in order.
That sequencing matters for the how-to. The build lifecycle has three independent tracks—clean, default, and site—and the default track runs phases one after another:
1: validate 2: compile 3: test 4: package 5: verify 6: install 7: deploy
If you ask for package, Maven runs validate, compile, test, and then package. You never need to invoke earlier phases manually; asking for a later phase pulls the earlier ones in. This is why the "right" command depends on what you're trying to produce, not on a fixed recipe.
Before You Run Anything
Four prerequisites prevent most wasted debugging cycles.
A JDK, not just a JRE. Maven needs a compiler. Check with mvn -v. The output lists the Maven version, the Java version it's running under, and the JAVA_HOME path. If JAVA_HOME is unset or points at a JRE, compilation fails with errors that look like code problems but are toolchain problems.
Version compatibility. Maven's own version and the JDK version interact: older Maven releases can't run on newer JDKs, and vice versa. Verify that the Maven version you have supports the JDK you set. Your project's POM may also declare a compiler release—often via the maven.compiler.release property or the compiler plugin configuration—and if that release is newer than your JDK, the build fails with an "invalid target release" message. Match them.
Exactly one POM at the root. If you're rebuilding a specific module of a multi-module project, either cd into that module's directory (where its own pom.xml lives) or build from the aggregator root with mvn -pl -am. Running mvn in a directory with no POM gives "There is no POM in this directory." Running it in a directory with multiple POMs gives "The goal you specified requires a project to execute but there is no POM in this directory" or an ambiguity error depending on layout. One POM, one project root.
Network access to repositories. Maven downloads plugins and dependencies into your local repository (usually ~/.m2/repository) on first use. Without access to Central or a configured mirror, first builds fail at dependency resolution. The error names the artifact and the repository it tried, which tells you whether it's a credentials problem, a proxy problem, or a typo in coordinates.
Run mvn -v first, then read the project's declared Java version, then decide your command.
Choosing the Right Command
The temptation is to memorize mvn clean install and run it always. That works, but slowly and noisily. Picking the phase that matches your goal gets you a faster feedback loop.
mvn compile compiles main sources only. Use it while iterating on production code and you don't care about tests yet. It fails on syntax errors and missing dependencies. Success means target/classes now contains .class files.
mvn test-compile compiles main and test sources. Use it when you've changed tests and want to know they at least compile before running them.
mvn test compiles both and runs the test suite with the configured test framework, usually JUnit via the Surefire plugin. It fails on test failures, and by default a test failure stops the build before packaging. This is the loop you want during test-driven work.
mvn package does everything above and produces the artifact. For a jar-packaged project that's target/-.jar. For a war, the war file. For a pom-packaged aggregator, packaging is essentially a no-op but still validates the reactor.
mvn verify runs any integration checks bound to the integration-test and verify phases—Failsafe tests, for example. Use it when the project has slower tests separate from unit tests.
mvn install packages and copies the artifact into your local repository so other local projects can depend on it. Use it when building a library that a sibling project consumes on your machine.
mvn deploy pushes to a remote repository—typically a release or snapshot repository. Don't run this casually; it publishes. It needs credentials configured in settings.xml.
mvn clean deletes target/. Combine it with a later phase when stale output is suspect: mvn clean package.
A sensible order of operations for a first build of an unfamiliar project: run mvn -v, run mvn clean package once to see the whole pipeline, then narrow to mvn compile or mvn test for the iteration loop.
A Complete First Build, Step by Step
This sequence assumes a project checked out locally and no prior build output.
1: Confirm the toolchain. Run mvn -v. Confirm Maven's version and that JAVA_HOME points at a JDK matching the project's required release. If the POM declares a compiler release higher than your JDK, install a newer JDK before continuing. 2: Open a terminal at the project root. The directory must contain pom.xml. For a multi-module project the root pom.xml is the aggregator; that's the right place to start. 3: Optionally clean. Run mvn clean if target/ exists from a prior build with different settings. On a fresh checkout this is unnecessary but harmless. 4: Run the full build once. Run mvn package (or mvn clean package). This resolves dependencies, compiles main and test sources, runs tests, and packages. Watching the output tells you which plugin is doing what and where time goes. 5: Check the result. Look for BUILD SUCCESS at the end and confirm the artifact exists under target/. For a jar project, target/*.jar should be present; for a war project, the war. If tests failed, the summary lists the failing tests and the build stopped before packaging. 6: Iterate with a narrower command. For day-to-day changes run mvn compile or mvn test rather than the full pipeline. Run mvn package again before you hand off or ship. 7: Install locally if dependents need it. Run mvn install so sibling projects resolve the new artifact from your local repository.
Success is BUILD SUCCESS plus the expected file in target/. Failure is a non-zero exit code and a BUILD FAILURE block that names the plugin or phase where it stopped. The exit code matters for CI: a script that ignores mvn's exit status can look green when the build failed.
Reading the Output When It Fails
Maven's failure output is verbose, but the useful part is structured. Find the BUILD FAILURE block and read its [ERROR] lines in order. The first error is usually the cause; later errors are often cascades.
Compilation errors come from the compiler plugin and cite file and line. Fix them as you would any compiler error. If every file fails with "package X does not exist," the dependency is either missing from the POM or wasn't downloaded—check the dependency tree and the repository log above the compiler errors.
Dependency resolution errors name the artifact coordinates and the repository. A Could not resolve or Could not transfer message points at a network, credentials, or coordinate problem. Verify the coordinate exists in the declared repository; verify your proxy or mirror settings in settings.xml if you have one.
Test failures appear in the Surefire report with the failing class and method. They're not build-system problems; they're the tests doing their job. Fix the code or the test, and note that Surefire writes reports to target/surefire-reports, which are easier to read than the console tail.
"No compiler is provided in this environment" means you're running Maven under a JRE. Fix JAVA_HOME.
"Invalid target release" means the compiler release the POM requests is newer than your JDK. Upgrade the JDK or lower the declared release if the project supports it.
"The goal you specified requires a project to execute but there is no POM in this directory" means you're in the wrong directory. cd to the directory containing the relevant POM.
When you're unsure why a dependency was or wasn't selected, mvn dependency:tree prints the resolved graph and includes/exclusions. When you want to know which plugins a phase runs, mvn help:effective-pom shows the POM as Maven actually sees it, after inheritance and profile activation.
Tests, Profiles, and Dependencies
A few deliberate choices make builds more predictable without requiring you to memorize plugin configuration.
Skip tests only when you mean it. mvn package -DskipTests compiles tests but doesn't run them; -Dmaven.test.skip=true skips compiling them too. Use these when you're producing an artifact for a known-good revision, not as a default. The habit of skipping tests to make a build pass hides real failures.
Profile-aware builds. Projects often gate environment-specific settings (repositories, compiler releases, integration tests) behind profiles. mvn help:active-profiles lists active profiles; -P activates one explicitly. If a build behaves differently on CI than locally, compare active profiles first.
Dependency scopes. compile is the default, test is test-only, provided is supplied by the runtime environment, runtime isn't needed to compile but is needed to run. Misdeclared scopes show up as either missing classes at runtime or bloated artifacts. mvn dependency:analyze flags declared-but-unused and used-but-undeclared dependencies, with the usual caveat about false positives from reflection.
Repository configuration belongs in settings.xml. If you must use a mirror, proxy, or credentials, put them in the user-level settings.xml rather than editing the POM. That keeps the POM shareable and keeps secrets out of version control.
Reproducible plugin versions. Pinning plugin versions in pom.xml (via pluginManagement or explicit version elements) keeps a build from silently changing behavior when a plugin's latest version updates. Projects that omit plugin versions are relying on defaults that can drift.
Offline builds. If dependencies are already in your local repository, mvn -o package runs without network access. It fails fast on anything missing, which is a useful check before building in a restricted environment.
Multi-Module Projects and Special Cases
In a multi-module project, the root POM aggregates modules. Running mvn package at the root builds all modules in dependency order, which is the common case. Use -pl to limit to a module and -am to also build the modules it depends on: mvn -pl service -am package builds service plus whatever it needs. -rf resumes from a module after a failure when earlier modules succeeded.
Other common situations:
War projects: mvn package produces a war under target/. Container integration for testing usually requires a separate plugin; without it, the war is the deliverable.
Spring Boot projects: the Spring Boot plugin repackages the jar into an executable one at the package phase. If you get a plain jar when you expect an executable one, check that the plugin's repackage goal is bound.
JDK version mismatches in CI: pin the JDK in CI to the release the project declares. The most common "works on my machine" failure for Maven builds is a CI runner with a different default JDK.
Generated sources: annotation processors can generate sources during compile. If your IDE sees generated classes but mvn compile doesn't, check that the processor's dependency is declared in the right scope and that generated-source directories are configured.
Common Mistakes That Waste Time
Running mvn install first instead of mvn package treats every local iteration as if it were being published, which slows the loop and can mask a packaging failure behind an install step. Start with package.
Editing the POM's repository section to work around a network problem is a temporary fix that breaks for everyone else—fix settings.xml or the environment instead.
Assuming a failing build means the code is bad. The first error in the output often names the real cause, and it might be a wrong JDK, a downstream artifact that hasn't been built, or a test environment issue.
Ignoring warnings about deprecated plugin configuration. They don't fail the build today, but they signal configuration that will break on a future Maven or plugin upgrade.
Skipping clean after changing the POM's compiler release or switching JDKs. Stale class files compiled against a different target can produce confusing errors; mvn clean package removes the ambiguity.
When to Escalate
If the build fails inside a plugin you didn't configure, or fails only in a CI environment you can't reproduce locally, stop and involve whoever owns the build configuration. If credentials for a private repository are missing, that's an environment or access issue, not a code issue. If a build has been failing since an infrastructure change—a repository moved, a JDK was upgraded on the runner—the fix belongs with the environment owners rather than with your local commands.
FAQ
Do I need to run mvn clean every time? No. Run it when output may be stale—after changing compiler settings, switching JDKs, or when weird errors suggest mixed class files. For routine iteration, mvn compile or mvn test is enough.
What's the difference between mvn package and mvn install? package puts the artifact in target/. install also copies it into your local repository so other local projects can depend on it. Use install only when a sibling project needs it.
Why does my build fail with "invalid target release" when the same code built yesterday? The JDK on your path or in JAVA_HOME changed, or the project's declared compiler release changed. Run mvn -v and compare the JDK's version with the project's declared release.
Can I run a single phase without its predecessors? Not on the default lifecycle. Asking for a later phase runs the earlier phases by design. If you want to skip a phase, use the plugin's skip options—for example, -DskipTests—rather than trying to reorder the lifecycle.