- Maven is a build automation tool used primarily for Java projects.
- It addresses two aspects of building software: first, it describes how software is built, and second, it describes its dependencies.
- Simplifies the build process: Maven uses a standardized project structure and a consistent build process.
- Dependency Management: Automatically downloads and manages project dependencies from central repositories.
- Reproducible Builds: Ensures consistent build results across different environments.
- Easy Integration: Works seamlessly with various IDEs and CI/CD tools.
- Extensibility: Can be extended with plugins to perform a variety of tasks.
- Efficient Project Management: Organizes project structure and simplifies build lifecycle.
- Dependency Management: Handles dependencies, reducing conflicts and ensuring compatibility.
- Consistent Builds: Provides a standard way to build projects, ensuring consistency across development teams.
- Simplifies Project Configuration: Uses a simple XML file (pom.xml) to manage project configurations.
- Community Support: Extensive community and plugin support for various needs and integrations.
- Command:
mvn clean
- Explanation: Removes all files generated by the previous build, ensuring a fresh start for the next build process.
- Command:
mvn validate
- Explanation: Checks if the project is correct and all necessary information is available. This phase ensures that the project is in a valid state before proceeding to the next phases.
- Command:
mvn compile
- Explanation: Compiles the source code of the project.
- Command:
mvn test
- Explanation: Runs the tests using a suitable unit testing framework. The tests should be located in the
src/test/java
directory.
- Command:
mvn package
- Explanation: Packages the compiled code into a distributable format, such as a JAR or WAR file.
- Command:
mvn verify
- Explanation: Runs any checks to verify the project is valid and meets quality standards.
- Command:
mvn install
- Explanation: Installs the package into the local repository, making it available for other projects on the same machine.
- Command:
mvn deploy
- Explanation: Copies the final package to the remote repository for sharing with other developers and projects.
- To clean and build the project:
mvn clean install
- To compile and run tests:
mvn compile test
The pom.xml
file is the heart of a Maven project, managing dependencies, plugins, and project configurations.
- groupId: Defines the group or organization that the project belongs to. It follows the standard Java package naming rules (e.g.,
com.example
). - artifactId: A unique name to identify the project (e.g.,
my-project
).
<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.anshulmaventutorial</groupId>
<artifactId>FirstMavenProject</artifactId>
<version>1.0-SNAPSHOT</version>
<name>FirstMavenProject</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>