Mastering Maven Commands
Mastering Maven Commands
Table of Contents
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
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:
Execution Steps:
1. Before running mvn clean, the target/ directory exists.
2. Run:
mvn clean
Explanation of Output:
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:
Execution Steps:
1. Run:
mvn compile
Explanation of Output:
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;
mvn test
Explanation of Output:
d. mvn package
Purpose:
Packages the compiled code into a distributable format (e.g., JAR or WAR).
Usage:
mvn package
Code Example:
<build>
<finalName>my-app</finalName>
</build>
Execution Steps:
1. Run:
mvn package
Explanation of Output:
e. mvn verify
Purpose:
Validates the project by running integration tests and performing additional checks.
Usage:
mvn verify
Code Example:
import org.junit.Test;
import static org.junit.Assert.assertTrue;
Execution Steps:
1. Define integration tests.
2. Run:
mvn verify
Explanation of Output:
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
<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:
We will automate the login functionality for a sample website, validate successful login,
and handle an invalid login scenario.
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;
@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:
mvn test
Step-by-Step Execution:
Report Location:
target/surefire-reports/TEST-com.example.LoginTest.xml
mvn package
Step-by-Step Execution:
Run:
mvn verify
This executes additional integration tests (if defined) and validates the packaged artifact.
7. Deployment
mvn deploy
Summary