Maven build lifecycle in Selenium Java - Build an Automation Script
import org.testng.Assert; import org.testng.annotations.Test; import java.io.File; import java.io.BufferedReader; import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class MavenBuildLifecycleTest { private String runCommand(String command, String workingDir) throws Exception { ProcessBuilder builder = new ProcessBuilder(); builder.command(command.split(" ")); builder.directory(new File(workingDir)); Process process = builder.start(); StringBuilder output = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { String line; while ((line = reader.readLine()) != null) { output.append(line).append(System.lineSeparator()); } } int exitCode = process.waitFor(); Assert.assertEquals(exitCode, 0, "Command failed: " + command); return output.toString(); } @Test public void testMavenBuildLifecycle() throws Exception { String projectDir = System.getProperty("user.dir"); Path targetDir = Paths.get(projectDir, "target"); // 1. mvn clean runCommand("mvn clean", projectDir); Assert.assertFalse(Files.exists(targetDir), "Target directory should be deleted after clean"); // 2. mvn compile String compileOutput = runCommand("mvn compile", projectDir); Assert.assertTrue(compileOutput.contains("BUILD SUCCESS"), "Compile phase should succeed"); // 3. mvn test String testOutput = runCommand("mvn test", projectDir); Assert.assertTrue(testOutput.contains("BUILD SUCCESS"), "Tests should run and pass"); Assert.assertTrue(testOutput.contains("Tests run:"), "Test results should be reported"); // 4. mvn package String packageOutput = runCommand("mvn package", projectDir); Assert.assertTrue(packageOutput.contains("BUILD SUCCESS"), "Package phase should succeed"); // Verify jar file exists File jarFile = targetDir.toFile().listFiles((dir, name) -> name.endsWith(".jar"))[0]; Assert.assertNotNull(jarFile, "Jar file should be created in target directory"); Assert.assertTrue(jarFile.exists(), "Jar file should exist"); } }
This test class automates the verification of Maven build lifecycle phases for a Selenium Java project.
The runCommand method runs Maven commands in the project directory and captures output. It asserts the command exits successfully.
The test method testMavenBuildLifecycle runs the lifecycle phases in order: clean, compile, test, and package. After each phase, it asserts expected results:
- After
clean, thetargetdirectory should be deleted. - After
compile, the build output should contain "BUILD SUCCESS". - After
test, tests should run and pass, verified by output containing "Tests run:" and "BUILD SUCCESS". - After
package, a jar file should be created in thetargetdirectory.
This approach uses ProcessBuilder to run Maven commands, captures output for assertions, and uses TestNG assertions to validate each step. It is simple, clear, and follows best practices for external process execution and test verification.
Now add data-driven testing to run the Maven lifecycle commands on three different Selenium Java projects located in different directories.
Practice
Solution
Step 1: Understand Maven lifecycle phases
Maven has phases like clean, compile, test, and package, each with a specific role.Step 2: Identify the phase for compiling source code
The compile phase compiles the Java source code into bytecode.Final Answer:
compile -> Option DQuick Check:
Compile phase compiles code = compile [OK]
- Confusing 'test' phase with compiling code
- Thinking 'package' compiles code
- Assuming 'clean' compiles code
Solution
Step 1: Recall Maven commands and their purposes
mvn clean deletes the target directory, removing previous build files.Step 2: Match command to cleaning action
Only mvn clean removes old build artifacts before a fresh build.Final Answer:
mvn clean -> Option AQuick Check:
Clean command deletes target folder = mvn clean [OK]
- Using mvn package to clean project
- Confusing mvn test with cleaning
- Thinking mvn compile cleans files
mvn test on a Maven project that has not been compiled yet?Solution
Step 1: Understand Maven lifecycle phase dependencies
Running mvn test triggers all previous phases including compile automatically.Step 2: Predict behavior when code is not compiled
Maven compiles code before running tests to ensure tests run on compiled classes.Final Answer:
Maven will compile the code first, then run tests -> Option BQuick Check:
Test phase runs compile first = compile then test [OK]
- Assuming tests run without compiling
- Thinking Maven skips compile phase
- Confusing clean with test phase
mvn package but get an error saying tests failed. What is the best way to fix this without skipping tests?Solution
Step 1: Understand the error cause
Tests failed during package phase, so build stopped.Step 2: Choose a fix that cleans and rebuilds including tests
Running mvn clean package removes old files and rebuilds fresh, running tests again.Final Answer:
Run mvn clean package to clean and rebuild -> Option CQuick Check:
Clean then package reruns tests = mvn clean package [OK]
- Skipping tests instead of fixing them
- Only compiling without packaging
- Manually deleting target folder is error-prone
Solution
Step 1: Understand Maven lifecycle chaining
Running a later phase runs all previous phases automatically.Step 2: Identify the shortest command that runs all phases
mvn clean package runs clean, compile, test, and package in order.Final Answer:
mvn clean package -> Option AQuick Check:
Package phase runs all before it = mvn clean package [OK]
- Listing all phases explicitly is unnecessary
- Skipping clean phase
- Running test package without clean
Ready to prove it?
Pass the test and this pattern moves unknown
