Apache Maven Tutorial
Apache Maven is a powerful build automation and project management tool primarily used for Java projects. It helps developers manage project builds, dependencies, and documentation in a consistent and standardized way. Maven uses a Project Object Model (POM) file to define project details, dependencies, and build configurations.
Why Use Maven?
Some of the uses of Maven build tool are as follows:
- Automates the build process
- Manages dependencies efficiently
- Provides standardized project structure
- Supports plugins for additional functionality
- Improves collaboration in large teams
Install Maven on Windows
Steps to install the Apache Maven tool on the Windows operating system:
Maven Setup
Setting up the MAVEN_HOME environment variable on the Windows 10 operating system. Install Maven Plugins.
Understanding POM (Project Object Model)
The pom.xml
file is the core of a Maven project. It defines project information, dependencies, plugins, and build settings.
Common Maven Commands
Command | Description |
---|---|
mvn clean |
Removes all files generated by the previous build |
mvn compile |
Compiles source code of the project |
mvn test |
Runs the tests for the project |
mvn package |
Packages the compiled code into a distributable format (e.g., JAR, WAR) |
mvn install |
Installs the package into the local repository for use in other projects |
mvn deploy |
Copies the package to a remote repository |
Maven Plugins
Maven Repositories
Maven Project
Maven tool provides project templates that can be created with Maven’s Archetype Plugin.
Maven Project Structure
my-app/
|-- src/
| |-- main/
| | |-- java/
| | | `-- com/
| | | `-- example/
| | | `-- App.java
| | |-- resources/
| |-- test/
| |-- java/
| | `-- com/
| | `-- example/
| | `-- AppTest.java
| |-- resources/
|-- target/
|-- pom.xml
- src/main/java: Contains Java source files.
- src/main/resources: Contains resource files, configuration files, etc.
- src/test/java: Contains test source files.
- src/test/resources: Contains resources for testing.
- target/: Directory where Maven puts the compiled files and packaged artifacts.
- pom.xml: The Project Object Model file that defines the project configuration.