0% found this document useful (0 votes)
34 views14 pages

Mastering Maven Commands

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views14 pages

Mastering Maven Commands

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Mastering Maven Commands: A

Beginner-Friendly Guide with


Step-by-Step Examples
Apache Maven is a powerful build automation tool used for Java projects. This guide
provides a complete and detailed walkthrough of all essential Maven commands, their
purposes, and practical usage with step-by-step code examples. By the end, you'll be able
to integrate Maven effectively into your projects and understand its commands
thoroughly.

Table of Contents

1. Maven Commands Overview


2. Command Details with Code and Examples
○ mvn clean
○ mvn compile
○ mvn test
○ mvn package
○ mvn verify
○ mvn install
○ mvn deploy
○ mvn site
○ mvn dependency:tree
3. Mini Project: Selenium Java JUnit4 Framework with Maven
4. summary

1. Maven Commands Overview

Maven commands help automate various stages of the software development lifecycle.
Below is the list of key commands:

1. mvn clean
2. mvn compile
3. mvn test
4. mvn package
5. mvn verify
6. mvn install
7. mvn deploy
8. mvn site
9. mvn dependency:tree

2. Maven Commands in Detail

a. mvn clean

Purpose:
Deletes the target directory where Maven stores all compiled code, packaged files, and
reports, ensuring a fresh build.

Usage:

mvn clean

Code Example:

Project Structure:

my-project/
|-- src/main/java/HelloWorld.java
|-- target/
|-- pom.xml

HelloWorld.java:

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, Maven!");
}
}

Execution Steps:
1. Before running mvn clean, the target/ directory exists.
2. Run:

mvn clean

3. The target/ directory is removed.

Explanation of Output:

[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ my-project ---


[INFO] Deleting /path/to/your/project/target
[INFO] BUILD SUCCESS

● The maven-clean-plugin deletes the target directory to start fresh.

b. mvn compile

Purpose:
Compiles the source code located in the src/main/java directory into .class files.

Usage:

mvn compile

Code Example:

HelloWorld.java:

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, Maven Compilation!");
}
}

Execution Steps:
1. Run:

mvn compile

2. The compiled .class file is placed in target/classes.

Explanation of Output:

[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @


my-project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /path/to/your/project/target/classes
[INFO] BUILD SUCCESS

● The maven-compiler-plugin compiles the source files.

c. mvn test

Purpose:
Executes unit tests defined in the src/test/java directory.

Usage:

mvn test

Code Example:
HelloWorldTest.java:

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class HelloWorldTest {


@Test
public void testMessage() {
assertEquals("Hello, Maven!", "Hello, Maven!");
}
}
Execution Steps:

1. Write test cases.


2. Run:

mvn test

3. Test results are saved in target/surefire-reports.

Explanation of Output:

[INFO] --- maven-surefire-plugin:3.0.0-M7:test (default-test) @ my-project


---
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

● The maven-surefire-plugin executes the tests and generates reports.

d. mvn package

Purpose:
Packages the compiled code into a distributable format (e.g., JAR or WAR).

Usage:

mvn package

Code Example:

Project Configuration (pom.xml):

<build>
<finalName>my-app</finalName>
</build>

Execution Steps:
1. Run:

mvn package

2. The output JAR is saved in the target/ directory as my-app-1.0-SNAPSHOT.jar.

Explanation of Output:

[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ my-project ---


[INFO] Building jar: /path/to/your/project/target/my-app-1.0-SNAPSHOT.jar
[INFO] BUILD SUCCESS

● The maven-jar-plugin creates the JAR file.

e. mvn verify

Purpose:
Validates the project by running integration tests and performing additional checks.

Usage:

mvn verify

Code Example:

Integration Test (IntegrationTest.java):

import org.junit.Test;
import static org.junit.Assert.assertTrue;

public class IntegrationTest {


@Test
public void testIntegrationScenario() {
assertTrue(true);
}
}

Execution Steps:
1. Define integration tests.
2. Run:

mvn verify

3. Integration tests are executed.

Explanation of Output:

[INFO] --- maven-failsafe-plugin:3.0.0-M7:verify (default) @ my-project ---


[INFO] BUILD SUCCESS

● The maven-failsafe-plugin runs integration tests.

f. mvn install

Purpose:
Installs the packaged artifact into the local Maven repository (~/.m2/repository).

Usage:

mvn install

g. mvn deploy

Purpose:
Deploys the packaged artifact to a remote Maven repository for team use.

Usage:

mvn deploy
h. mvn site

Purpose:
Generates a site with project documentation and reports.

Usage:

mvn site

i. mvn dependency:tree

Purpose:
Displays the project’s dependency tree.

Usage:

mvn dependency:tree
Mini Project: Selenium Java JUnit4 Framework with Maven

This section walks through setting up a Selenium Java JUnit4 Framework using Maven,
implementing meaningful test scenarios, and executing the tests with detailed
explanations.

1. Project Setup

a. Maven Configuration (pom.xml)

Include the necessary dependencies for Selenium and JUnit4.

<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>selenium-junit-example</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.12.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
</plugins>
</build>
</project>

Explanation:

● The junit dependency enables writing and executing test cases.


● The selenium-java dependency allows browser automation.
● The maven-surefire-plugin ensures test execution during the mvn test.

2. Test Scenario: Login Validation

We will automate the login functionality for a sample website, validate successful login,
and handle an invalid login scenario.

3. Writing the Test Code

a. Create Test Class: LoginTest.java

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import static org.junit.Assert.assertTrue;

public class LoginTest {

private WebDriver driver;

@Before
public void setUp() {
System.setProperty("webdriver.chrome.driver",
"path/to/chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://example.com/login");
}

@Test
public void testValidLogin() {
WebElement username = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.id("password"));
WebElement loginButton = driver.findElement(By.id("loginButton"));

username.sendKeys("validUser");
password.sendKeys("validPassword");
loginButton.click();

String welcomeMessage =
driver.findElement(By.id("welcomeMessage")).getText();
assertTrue(welcomeMessage.contains("Welcome, validUser"));
}

@Test
public void testInvalidLogin() {
WebElement username = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.id("password"));
WebElement loginButton = driver.findElement(By.id("loginButton"));

username.sendKeys("invalidUser");
password.sendKeys("invalidPassword");
loginButton.click();

String errorMessage =
driver.findElement(By.id("errorMessage")).getText();
assertTrue(errorMessage.contains("Invalid credentials"));
}

@After
public void tearDown() {
driver.quit();
}
}
Explanation:

1. Setup Method (@Before):

○ Sets the chromedriver path and initializes the browser.


○ Open the login page.
2. Valid Login Test (@Test):

○ Simulates entering valid credentials.


○ Verifies the welcome message to confirm successful login.
3. Invalid Login Test (@Test):

○ Simulates entering invalid credentials.


○ Verifies the error message.
4. Tear Down (@After):

○ Closes the browser after each test to free resources.

4. Running the Tests

You can run the tests using Maven commands:

a. Running Tests Locally

mvn test

Step-by-Step Execution:

1. Maven invokes the maven-surefire-plugin to execute test cases.


2. Both testValidLogin and testInvalidLogin are executed.
3. Results are saved in target/surefire-reports.

Expected Output (Console):

[INFO] --- maven-surefire-plugin:3.0.0-M7:test (default-test) @


selenium-junit-example ---
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed:
10.243 s
[INFO] BUILD SUCCESS

Report Location:
target/surefire-reports/TEST-com.example.LoginTest.xml

5. Packaging the Project

To generate a JAR file:

mvn package

Step-by-Step Execution:

1. Maven compiles the source code and packages it into a JAR.


2. The JAR is located at target/selenium-junit-example-1.0-SNAPSHOT.jar.

Expected Output (Console):

[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @


selenium-junit-example ---
[INFO] Building jar:
/path/to/project/target/selenium-junit-example-1.0-SNAPSHOT.jar
[INFO] BUILD SUCCESS

6. Verifying the Build

Run:

mvn verify

This executes additional integration tests (if defined) and validates the packaged artifact.
7. Deployment

To deploy the artifact to a local or remote repository:

mvn deploy

Summary

This mini-project demonstrates how to set up and execute a Selenium-based test


framework using Maven, covering the entire lifecycle from setup to deployment. This
framework can be expanded to include additional tests, advanced reporting, and
continuous integration. By running commands like mvn clean, mvn test, mvn package,
and mvn deploy, you'll automate the testing and deployment of your Java projects
effectively.

You might also like