Apache Maven Interview Questions and Answers

Apache Maven Interview Questions and Answers:

Maven Interview Questions: 1) Explain what is Maven? How does it work?

Maven is a project management tool. It provides the developer a complete build lifecycle framework. On executing Maven commands, it will look for POM file in Maven; it will run the command on the resources described in the POM.

2) List out what are the aspects does Maven Manages?

Maven handles following activities of a developer

  • Build
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution
  • Mailing list

3) Mention the three build lifecycle of Maven?

  • Clean: Cleans up artifacts that are created by prior builds
  • Default (build): Used to create the application
  • Site: For the project generates site documentation

4) Explain what is POM?

In Maven, POM (Project Object Model) is the fundamental unit of work. It is an XML file which holds the information about the project and configuration details used to build a project by Maven.

5) Explain what is Maven artifact?

Usually an artifact is a JAR file which gets arrayed to a Maven repository. One or more artifacts a maven build produces such as compiled JAR and a sources JAR.

Each artifact includes a group ID, an artifact ID and a version string.

6) Explain what is Maven Repository? What are their types?

A Maven repository is a location where all the project jars, library jars, plugins or any other particular project related artifacts are stored and can be easily used by Maven.

Their types are local, central and remote

7) Why Maven Plugins are used?

Maven plugins are used to

  • Create a jar file
  • Create war file
  • Compile code files
  • Unit testing of code
  • Documenting projects
  • Reporting

8) List out the dependency scope in Maven?

The various dependency scope used in Maven are:

  • Compile: It is the default scope, and it indicates what dependency is available in the classpath of the project
  • Provided: It indicates that the dependency is provided by JDK or web server or container at runtime
  • Runtime: This tells that the dependency is not needed for compilation but is required during execution
  • Test: It says dependency is available only for the test compilation and execution phases
  • System: It indicates you have to provide the system path
  • Import: This indicates that the identified or specified POM should be replaced with the

dependencies in that POM’s section

9) Mention how profiles are specified in Maven?

Profiles are specified in Maven by using a subset of the elements existing in the POM itself.

10) Explain how you can exclude dependency?

By using the exclusion element, dependency can be excluded

11) Mention the difference between Apache Ant and Maven?

Apache Ant Maven

  • Ant is a toolbox – Maven is a framework
  • Ant does not have formal conventions like project directory structure – Maven has conventions
  • Ant is procedural; you have to tell to compile, copy and compress – Maven is declarative ( information on what to make & how to build)
  • Ant does not have lifecycle; you have to add sequence of tasks manually – Maven has a lifecycle
  • Ant scripts are not reusable – Maven plugins are reusable

12) In Maven what are the two setting files called and what are their location?

In Maven, the setting files are called settings.xml, and the two setting files are located at

  • Maven installation directory: $M2_Home/conf/settings.xml
  • User’s home directory: ${ user.home }/ .m2 / settings.xml

13) List out what are the build phases in Maven?

Build phases in Maven are

  • Validate
  • Compile
  • Test
  • Package
  • Install
  • Deploy

14) List out the build, source and test source directory for POM in Maven?

  • Build = Target
  • Source = src/main/java
  • Test = src/main/test

15) Where do you find the class files when you compile a Maven project?

You will find the class files ${basedir}/target/classes/.

16) Explain what would the “jar: jar” goal do?

jar: jar will not recompile sources; it will imply just create a JAR from the target/classes directory considering that everything else has been done

17) List out what are the Maven’s order of inheritance?

The maven’s order of inheritance is

  • Parent Pom
  • Project Pom
  • Settings
  • CLI parameters

18) For POM what are the minimum required elements?

The minimum required elements for POM are project root, modelVersion, groupID, artifactID and version

19) Explain how you can produce execution debug output or error messages?

To produce execution debug output you could call Maven with X parameter or e parameter

20) Explain how to run test classes in Maven?

To run test classes in Maven, you need surefire plugin, check and configure your settings in setting.xml and pom.xml for a property named “test.”

21) What is the best practice configuration usage for files – pom.xml or settings.xml?

The best practice guideline between settings.xml and pom.xml is that configurations in settings.xml must be specific to the current user and that pom.xml configurations are specific to the project.

22)How can I change the default location of the generated jar when I command “mvn package”?

By default, the location of the generated jar is in ${project.build.directory} or in your target directory. We can change this by configuring the outputDirectory of maven-jar-plugin.

23)What is Maven’s order of inheritance?

1. parent pom
2. project pom
3. settings
4. CLI parameters

24)What is a Mojo?

A mojo is a Maven plain Old Java Object. Each mojo is an executable goal in Maven, and a plugin is a distribution of one or more related mojos.

25) How do I determine which POM contains missing transitive dependency?

run “mvn -X”

26)How would you see the version of Maven ?

mvn –version

27)Where do we configure repositories in Maven ?

Within settings.xml in either MAVEN_HOME or .M2 directory.

28)What are different type of repositories in Maven ?

Local and Remote.

29)How does Maven looks for a dependency or resource ?

It refers to the settings.xml to look for the repositories to look for the resource. First It looks into the configured local repository, then it looks into the configured Remote repositories. If the resource is still not found , it looks it within maven repository central i.e repo1.maven.org. If its still not found, it throws the exception saying “Unable to find resource in repository central”.

30)What is maven repository central ?

Its the repository provided by Maven. In case your POM specify the dependencies and its not available in the configured local and the remote repository. It then looks for the resource in Maven Central. Maven provides most of the generic dependency resources at this remote location.

31)What would you do if you have to add a jar to the project using Maven ?

If its already there in Maven local repository, We can add that as a dependency in the project pom file with its Group Id, Artifact Id and version.

We can provide additional attribute SystemPath if its unable to locate the jar in the local repository.

If its not there in the local repository, we can install it first in the local repository and then can add it as dependency.

32)Have you ever had problem getting your projects in eclipse refreshed after you made changes in the Pom files ?

Yes, It happens many times but I would usually perform mvn eclipse:eclipse and this would resolve the project refresh problems.

33)What is the difference between compile and install ?

Compile compiles the source code of the project. whereas,

Install installs the package into the local repository, for use as a dependency in other projects locally

34)How can we see Dependencies for the project and where exactly they are defined ?

Ans. Using  mvn dependency:tree

35)What is a transitive dependency ? Can we override Transitive Dependency version and If Yes, how ?

Transitive dependency is the dependencies not defined directly in the current POM but the POM of the dependent projects.  Transitive dependencies allows to avoid specifying the libraries that are required by the project which are specified in other dependent projects – Remote or Local.

Yes we can override transitive dependency version by specifying the dependency in the current POM.

36)What is a cyclic dependency ?

A has dependency of B, B has dependency of C and C has dependency of A.

With Maven 2 , came transitive dependency wherein in above scenario, C will acts as a dependency of A as if this dependency has been defined directly in A but the negative side is that if it leads to cyclic dependency , it creates problems.

Leave a reply:

Your email address will not be published.

Site Footer