Maven Tutorial
Apache Maven is a build automation and project management tool primarily for Java projects. It uses an XML file, pom.xml, to manage dependencies, configure build parameters, and control project structure.
1. Installing Mavenβ
Step 1: Download Mavenβ
-
Go to the Maven website and download the latest Maven release.
-
Unzip the file and add Maven's
bindirectory to your system's PATH.
Step 2: Verify Installationβ
Check if Maven is installed by running:
mvn -version
2. Creating a Maven Projectβ
Maven can create a new project from a standard template.
mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This creates a basic project structure with the following files:
my-app
βββ pom.xml
βββ src
βββ main
β βββ java
β βββ com/example/App.java
βββ test
βββ java
βββ com/example/AppTest.java
3. Understanding pom.xmlβ
The pom.xml file (Project Object Model) is the core of a Maven project, describing dependencies, plugins, and settings.
Example pom.xmlβ
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- Define dependencies here -->
</dependencies>
</project>
4. Adding Dependenciesβ
Add dependencies to pom.xml within the <dependencies> section.
Example: Adding JUnitβ
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Run mvn install to download and install dependencies.
5. Maven Lifecycleβ
Maven has a lifecycle with specific phases:
- compile: Compiles source code.
- test: Runs tests.
- package: Packages code into a JAR or WAR file.
- install: Installs package to local repository.
- deploy: Deploys package to a remote repository.
Example Commandsβ
mvn compile # Compile source code
mvn test # Run tests
mvn package # Package into JAR/WAR
mvn install # Install to local repository
mvn deploy # Deploy to remote repository
6. Plugins in Mavenβ
Plugins extend Mavenβs capabilities. Add plugins in the <build> section of pom.xml.
Example: Maven Compiler Pluginβ
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
7. Multi-Module Projectsβ
Maven supports multi-module projects, where multiple modules are managed under one parent project.
Example Parent pom.xmlβ
In the parent projectβs pom.xml, add module paths:
<modules>
<module>module1</module>
<module>module2</module>
</modules>
Each module should have its own pom.xml but inherit from the parent project.
8. Building and Running a Maven Projectβ
-
Build: Run
mvn packageto create a JAR/WAR file intarget/directory. -
Run: Execute the JAR file with:
java -jar target/my-app-1.0-SNAPSHOT.jar
Summaryβ
This tutorial covered the basics of Maven:
- Setting up Maven and creating a project.
- Understanding
pom.xmlstructure. - Adding dependencies and plugins.
- Using Maven commands for building, testing, and deploying.
Maven simplifies Java project management, automating dependency management and providing a structured approach to build and deployment.
Content Reviewβ
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewerβ
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.