Maven
Maven is an open-source build automation and project management tool maintained by the Apache Software Foundation, used primarily for Java projects. It uses an XML file called pom.xml (Project Object Model) to define project structure, dependencies, plugins, and build lifecycle — making project builds standardized and repeatable across teams.
Core Concepts
| Concept | Description |
|---|---|
| POM | pom.xml — the heart of every Maven project; defines dependencies, plugins, and goals |
| Lifecycle | Ordered sequence of phases (e.g., compile → test → package → install → deploy) |
| Phase | A single step in the build lifecycle (e.g., compile, test, package) |
| Goal | A specific task provided by a plugin (e.g., maven-compiler-plugin:compile) |
| Repository | Local (~/.m2) or remote (Maven Central) store of downloaded dependencies |
| Artifact | The output of a build (e.g., .jar, .war) identified by groupId:artifactId:version |
Prerequisites
Install Java
Maven requires Java to run. Install the default JRE on Ubuntu/Debian:
sudo apt update
sudo apt install default-jre -y
# Verify
java --version
Install Maven
sudo apt install maven -y
# Verify
mvn --version
Maven Commands
1. mvn clean
Deletes the target/ directory, removing all previously compiled classes, packaged artifacts, and test reports — giving you a clean slate before the next build.
mvn clean
2. mvn install
Downloads all declared dependencies from Maven Central (or a configured repository) into the local ~/.m2 cache, and installs the built artifact locally so other projects on the same machine can reference it.
mvn install
3. mvn compile
Compiles the project’s main Java source files under src/main/java/ and places the .class files into target/classes/ — does not run tests or produce a packaged artifact.
mvn compile
4. mvn test
Downloads dependencies, compiles source and test code, then executes all unit tests using the configured test framework (e.g., JUnit/TestNG) and generates reports under target/surefire-reports/.
mvn test
5. mvn package
Compiles the code, runs tests, and then bundles the output into a distributable artifact such as a .jar or .war file, placed inside the target/ directory.
mvn package
6. mvn deploy
Performs a full build and then uploads (publishes) the final artifact to a remote repository such as Nexus or JFrog Artifactory, making it available for other teams or CI/CD pipelines to consume.
mvn deploy
7. mvn clean package
Combines clean and package — removes old artifacts first and then repackages from scratch, preventing stale .class files or outdated JARs from polluting the build output.
mvn clean package
8. mvn clean install
Wipes the target/ directory, performs a full build including tests, and installs the resulting artifact into the local ~/.m2 repository — the most common command used in CI pipelines.
mvn clean install
9. mvn validate
Checks that the pom.xml is well-formed and that all required project information (groupId, artifactId, version) is present before any build phase begins.
mvn validate
10. mvn verify
Runs the full build lifecycle up through integration tests and quality checks, verifying the packaged artifact is valid — without publishing it to any repository.
mvn verify
11. mvn dependency:tree
Prints the full dependency tree of the project, including transitive dependencies, helping you identify version conflicts or unwanted libraries being pulled in.
mvn dependency:tree
12. mvn dependency:resolve
Resolves and lists all project dependencies without performing a full build, useful for quickly checking which versions of libraries will be used.
mvn dependency:resolve
13. mvn -DskipTests
Compiles and packages the project while skipping the execution of unit tests (tests are still compiled), useful when you want a fast build during development.
mvn package -DskipTests
14. mvn -Dmaven.test.skip=true
Skips both the compilation and execution of tests entirely, producing the fastest possible build at the cost of no test validation whatsoever.
mvn package -Dmaven.test.skip=true
15. mvn site
Generates a full project documentation website under target/site/, including dependency reports, test results, and plugin information — useful for project reporting.
mvn site
16. mvn versions:display-dependency-updates
Scans the pom.xml and reports which declared dependencies have newer versions available in the remote repository, making dependency maintenance easier.
mvn versions:display-dependency-updates
17. mvn help:effective-pom
Displays the fully resolved and merged POM, showing all inherited settings from parent POMs and active profiles — useful for debugging unexpected build behavior.
mvn help:effective-pom
18. mvn -X
Enables full DEBUG-level logging output, printing every internal Maven decision and plugin execution — use this when standard output doesn’t reveal why a build is failing.
mvn clean install -X
19. mvn -pl <module>
Runs Maven only for a specific module in a multi-module project, skipping all other modules — significantly faster when you’re iterating on one subproject.
mvn -pl api-module clean package
20. mvn -o
Runs Maven in offline mode, using only locally cached dependencies from ~/.m2 and making no network calls — ideal when working without internet access.
mvn clean install -o
Build a Sample Java Application
Clone and build the Spring PetClinic sample application using Maven:
git clone https://github.com/spring-projects/spring-petclinic.git
cd spring-petclinic
mvn clean package
The built JAR will be available at:
target/spring-petclinic-*.jar
Run it directly:
java -jar target/spring-petclinic-*.jar
Deploy as a Linux Systemd Service
Create the service unit file:
sudo vi /usr/lib/systemd/system/spring.service
Paste the following service definition:
[Unit]
Description=Spring PetClinic
Documentation=https://github.com/spring-projects/spring-petclinic
[Service]
User=root
Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk"
ExecStart=java -jar /home/azureuser/spring-petclinic/target/spring-petclinic-4.0.0-SNAPSHOT.jar
[Install]
WantedBy=multi-user.target
Alias=spring.service
Systemctl Commands
Manage the Spring Boot service using systemctl:
sudo systemctl start spring.service # Start the service
sudo systemctl restart spring.service # Restart the service
sudo systemctl stop spring.service # Stop the service
sudo systemctl enable spring.service # Enable on boot
sudo systemctl daemon-reload # Reload systemd after editing the unit file
