Maven
Course Index
Index

Maven Interview Questions

1. What is Maven, and what are its advantages?

Maven is a build automation tool used primarily for Java projects. It simplifies project builds, dependency management, and deployment processes. Key advantages include:

  • Automated dependency management.
  • Standardized project structure.
  • Easy integration with CI/CD tools.
  • Plugin-based architecture for extensibility.
  • Simplified project configuration via pom.xml.

2. What is the purpose of pom.xml in Maven?

pom.xml (Project Object Model) is the configuration file in a Maven project. It defines dependencies, build configurations, plugins, repositories, and other settings required to build the project.

3. What is the difference between mvn clean, mvn verify, and mvn test?

  • mvn clean: Removes previously compiled artifacts and target directory.
  • mvn test: Runs unit tests using the testing framework (e.g., JUnit, TestNG).
  • mvn verify: Executes all lifecycle phases up to verification, ensuring the project meets specified criteria.

4. What are Maven lifecycle phases?

Maven has three built-in lifecycles: default, clean, and site. The default lifecycle has key phases such as:

  • validate: Validates the project structure.
  • compile: Compiles source code.
  • test: Runs unit tests.
  • package: Packages compiled code into a JAR/WAR file.
  • verify: Verifies integration tests.
  • install: Installs the package into the local repository.
  • deploy: Deploys the artifact to a remote repository.

5. How does Maven manage dependencies?

Maven manages dependencies using the section in pom.xml. It retrieves dependencies from configured repositories (local, central, or remote). It supports transitive dependency resolution and dependency scopes (compile, provided, runtime, test, system).

6. What are Maven repositories, and what are their types?

Maven repositories store project dependencies and artifacts. Types include:

  • Local Repository: Stored on the developer's machine (.m2 folder).
  • Central Repository: Official repository managed by Maven (https://repo.maven.apache.org/maven2).
  • Remote Repository: Third-party repositories defined in pom.xml.

7. How do you create a Maven project?

  • Using the command : mvn archetype:generate -DgroupId=com.example -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
  • This generates a basic project structure with a predefined pom.xml.

8. How do you skip tests in Maven?

  • Answer: Use -DskipTests or -Dmaven.test.skip=true in the command:
    • Example: mvn clean install -DskipTests

9. What is the purpose of Maven plugins?

Maven plugins enhance functionality during the build process. Common plugins include:

  • maven-compiler-plugin: Compiles Java code.
  • maven-surefire-plugin: Runs unit tests.
  • maven-jar-plugin: Creates JAR files.
  • maven-dependency-plugin: Manages dependencies.

10. How can you deploy a Maven project to a repository?

Use mvn deploy with a configured section in pom.xml. Artifacts are deployed to remote repositories like Nexus or Artifactory.

11. What is the difference between SNAPSHOT and RELEASE versions in Maven?

  • SNAPSHOT: Represents a work-in-progress version. Maven checks for updates frequently.
  • RELEASE: A stable, final version that does not change.

12. What is the difference between dependency scopes in Maven?

Dependency scopes define how dependencies are used in a project:

  • compile: Default scope, available in all phases.
  • provided: Required for compilation but not included in the final package.
  • runtime: Available at runtime but not during compilation.
  • test: Used only for testing.
  • system: Requires manually provided JAR files.
  • import: Used with dependency management for importing BOMs (Bill of Materials).

13. What is a BOM in Maven?

A Bill of Materials (BOM) is a special pom.xml that manages dependency versions centrally. It prevents version conflicts by importing consistent versions of dependencies.

14. How do you create a multi-module Maven project?

Create a parent pom.xml and multiple child modules inside the same project directory. The parent pom.xml should define modules using:

XML
Copy
<modules>
    <module>module1</module>
    <module>module2</module>
</modules>

15. What is the difference between mvn package and mvn install?

  • mvn package: Compiles and packages the project into a distributable format (JAR/WAR).
  • mvn install: Packages the project and installs it in the local repository for reuse.

16. How do you override a dependency version in Maven?

Use the section in pom.xml to enforce a specific dependency version across modules.

17. What is the use of .m2/settings.xml?

The settings.xml file stores user-specific configurations like remote repositories, authentication credentials, and proxy settings.

18. How do you force Maven to update dependencies?

Run mvn clean install -U to force Maven to check for updated dependencies.

19. How do you create a custom Maven plugin?

A custom plugin can be created using a Maven project with a specific packaging type:
maven-plugin
Then implement a Mojo class extending AbstractMojo.

20. What are some best practices for using Maven?

  • Define dependency versions explicitly to avoid conflicts.
  • Use a BOM for consistent dependency management.
  • Keep pom.xml organized and modularized.
  • Use multi-module projects where applicable.
  • Regularly clean and update the local repository to prevent corruption.