Maven Tutorial
Maven in IDE
Maven has a set of pre-defined build lifecycles to perform specific tasks. The most common lifecycles are the default, clean, and site lifecycles. This tutorial will provide an overview of each lifecycle and demonstrate their usage in a simple project.
The default lifecycle is responsible for building and deploying a project. It consists of several phases, such as compile, test, package, install, and deploy. When you invoke a specific phase, Maven executes all the preceding phases in the lifecycle.
Here's a brief overview of some important phases:
The clean lifecycle is responsible for cleaning the project's working directory by removing the build artifacts (e.g., compiled classes, generated JARs).
It consists of the following phases:
The site lifecycle is responsible for creating the project's site documentation, including reports and static files.
Key phases include:
Example: Using Maven Lifecycles in a Simple Project
Create a new Maven project (assuming Maven is installed and configured) with the following command:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command generates a basic Java project with a "pom.xml" file and sample Java class with a test case.
Navigate to the "my-app" directory and run the following Maven commands to understand the lifecycle usage:
mvn clean
This command deletes the "target" directory, cleaning up the previous build.
mvn package
This command compiles the source code, runs tests, and creates a JAR file in the "target" directory.
mvn site
This command generates a site with reports and documentation in the "target/site" directory.
To summarize, Maven's clean, default, and site lifecycles provide a structured way to build, clean, and document projects. Each lifecycle consists of phases that can be executed individually or together to automate and standardize project tasks.