Maven: Build Management Tool and Central Repository
Maven is a build management tool that centralizes project building and dependency management. It automates various aspects of the project lifecycle such as compiling code, running tests, packaging, and deploying applications.
Key Concepts:
Build Management:
- mvn is the command-line tool for Maven, which manages all build-related activities in a project including:
- Compile: Compiles the source code.
- Run: Runs the compiled code.
- Test: Executes tests.
- Package: Packages the code into a distributable format (e.g., JAR file).
- Install: Installs the package into the local repository.
- Deploy: Deploys the package to a remote repository.
- mvn is the command-line tool for Maven, which manages all build-related activities in a project including:
Compilation and Testing Process:
- Takes all classes from
src/test/java, compiles them to create.classfiles. - Runs tests to validate the code.
- Packages the code into a JAR file.
- Installs the JAR file into the local repository.
- Deploys the JAR file into a remote repository.
- Takes all classes from
Central Repository:
- A central repository stores all necessary JAR files (e.g., Selenium, commons-io, TestNG, apache-poi).
- These dependencies can be downloaded at runtime, eliminating the need to manually download and add JAR files to the project.
- Example repository: Maven Repository.
Maven Repository:
- Provides access to various versions of libraries and tools, which can be added to your project using Maven.
Maven Project Creation:
Creating a Maven Project:
- Through Eclipse (Preferred):
File>New>Other> Search for 'maven' > Select 'Maven Project' >Next> Select 'Create simple project' >Next.- Enter
GroupId(e.g.,com.hcl) andArtifactId(e.g.,SampleMavenProject) >Finish.
Project Structure:
- src/main/java: Contains main source code.
- src/test/java: Contains test source code.
- src/main/resources and src/test/resources: For configuration files, test data, drivers, etc.
- target: Directory where built artifacts are stored.
- pom.xml: Project Object Model file, central to Maven configuration.
pom.xml File:
The pom.xml file is where you define all dependencies, which Maven will download and add to your project automatically.
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hcl</groupId>
<artifactId>SampleMavenProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.18.0</version>
</dependency>
</dependencies>
</project>
Adding Dependencies:
Selenium:
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.18.0</version> </dependency>Commons-IO:
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.13.0</version> </dependency>TestNG:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.0.0</version> <scope>test</scope> </dependency>Apache POI:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.0.0</version> </dependency>WebDriverManager:
<dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>4.4.3</version> </dependency>Extent Reports:
<dependency> <groupId>com.aventstack</groupId> <artifactId>extentreports</artifactId> <version>4.1.0</version> </dependency>Log4j:
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
Verifying Dependency Installation:
- Check the "Maven Dependencies" folder in the project to ensure that the required JAR files have been added.
- Check the local repository (e.g.,
C:\Users\Lenovo\.m2\repository) to verify the downloaded JAR files.
Integrating Maven with Eclipse:
- Maven integrates easily with Eclipse through the installation of the Maven plugin.
- Maven plugins, like the Maven Surefire Plugin, can be used to run
testng.xmlfiles and maintain a common project folder structure.
By using Maven, you streamline the process of managing dependencies and building your Java projects, ensuring a standardized approach and reducing manual efforts.
No comments:
Post a Comment