April 27, 2017

Maven Interview Questions And Answers

What is Maven?
Apache Maven is an advanced build tool to support the developer at the whole process of a software project. Maven allows the developer to automate the process of the creation of the initial folder structure for the Java application, performing the compilation, testing, packaging and deployment of the final product.

Since Maven is implemented in Java which makes it platform-independent.



Common activities we perform while developing an application:
  1. Multiple Jars: if you are using couple of frameworks like (Spring or Hibernate etc) in your application, then you need to include all the required jars.
  2. Dependencies: When you are adding multiple jars, you might face a issue where one jar is dependent on another jar, which in turn depend on some other jar. You need to make sure, you have added matching versions of jars.
  3. Setting Project Structure:  We need to set proper project structure.
  4. Building, deploying and publishing.

Maven helps us in these 4 activities, it has very elegant solution for each activity.

What are the key features of Maven?
  • Convention over configuration: Maven tries to avoid as much configuration as possible, by choosing real world default values and supplying project templates (archtypes).
  • Dependency management: It is possible to define dependencies to other projects. During the build, the Maven build system resolves the dependencies and it also builds the dependent projects if needed.
  • Repository: Project dependencies can be loaded from the local file system, from the Internet or public repositories. The company behind the Maven project also provides a central repository called Maven Central.
  • Extensible via plug-ins: The Maven build system is extensible via plug-ins, which allows to keep the Maven core small. The Maven core does for example not know how to compile Java source code, this is handled by the compiler plug-in.
What is Maven Repository?
A maven repository is a directory of packaged JAR file with pom.xml file. Maven searches for dependencies in the repositories. There are 3 types of maven repository: Local, Central, and Remote.

Maven searches for the dependencies in the following order: Local repository then Central repository then Remote repository. If dependency is not found in these repositories, maven stops processing and throws an error.

  • Maven local repository is located in your local system. It is created by the maven when you run any maven command. By default, maven local repository is %USER_HOME%/.m2 directory.
  • Maven central repository is located on the web. It has been created by the apache maven community itself. The path of central repository is: http://repo1.maven.org/maven2/. The central repository contains a lot of common libraries that can be viewed by this url http://search.maven.org/#browse.
  • Maven remote repository is located on the web. Most of libraries can be missing from the central repository such as JBoss library etc, so we need to define remote repository in pom.xml file. e.g:
< dependencies >  
    < dependency >  
      < groupId > junit< /groupId >  
      < artifactId > junit< /artifactId >  
      < version > 4.8.2< /version >  
      < scope > test< /scope >  
    < /dependency >  
< /dependencies >  


Can you change the location of Maven Local Repository?
Yes, we can change the location of maven local repository by changing the settings.xml file, which is located in MAVEN_HOME/conf/settings.xml.
< settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" >  
  < !-- localRepository 
   | The path to the local repository maven will use to store artifacts. 
   | 
   | Default: ${user.home}/.m2/repository 
  < localRepository > /path/to/local/repo< /localRepository >  
  -- >  
 
... 
< /settings >  


If you want to change the Local Repository location to mavenlocalrepo folder in D drive then change the value in localRepository tag:
< localRepository > d:/mavenlocalrepo< /localRepository >   
What is Maven Central?
Maven Central is an open repository provided by the company Sonatype. This repository hosts libraries which can be used in your build. By default, a Maven build uses Maven Central to search for required libraries.

How can you download and install Maven?
If you are planning to use Maven only from within the Eclipse IDE, this installation is not required.

Else, download the maven from http://maven.apache.org/download.cgi

After downloading, extract the downloaded distribution to a selected folder on your computer. Add a new environment variable M2_HOME pointing to this directory and add M2_HOME/bin to the PATH variable.

To validate whether Maven is installed correctly , open a console and run the following command:
mvn -version

If you get the output then Maven is installed.

How can you build a Maven project via the command line?
To build the project you need to run 'mvn' command from the command line. This command need to be executed in the directory which contains the relevant pom file. You need to provide the mvn command with the life cycle phase or goal to execute.

The Maven tooling reads the pom file and resolves the dependencies of the project. Maven validates if required components are available in a local repository (which is found in the .m2/repository folder of the users home directory). If the dependency is not available in the build reactor or the local repo, Maven downloads the depended artifacts from the central repository or the specified ones into the local repository.

Maven executes all life cycle phases until the specified one. e.g, the 'mvn clean install' command triggers the jar packaging. This includes compiling the sources, executing the tests and packaging the compiled files in a JAR file. As last step the install phase installs the resulting artifact into the local repository, so it can be used as dependencies by other Maven builds.

Maven creates the build result in the target folder.

'mvn install' will compile, build and install the build result. 

To ensure that the build target is removed before a new build, add the clean target. run 'mvn clean install' command.

By default, Maven checks online if the dependencies have been changed. If you want to use your local repository, you can use the -o to tell Maven to work offline. To do this run, 'mvn -o clean install' command.

In how many ways, you can deal with build failure?
If you are running a complex multi-module project build, you can define how the Maven build system should react to errors in one module.
  • -fae, --fail-at-end - fails the build after all modules are build; allow all non-impacted builds to continue
  • -ff, --fail-fast - Stop at first module build failure
  • -fn, --fail-never - NEVER fail the build, regardless of module build result

The -fn and -fae options are useful to verify builds that are running within a continuous integration tool like Jenkins and to see all errors in the build

What is Maven Wrapper?
When building with Maven the same input should always result in the same output. To ensure this on different machines, every machine has to have the same version of Maven. Having the same version on different machines is bit difficult sometimes, when different projects require different versions of Maven. To overcome this issue Maven Wrapper is specified for Maven projects.

The Maven Wrapper allows to run a Wrapper, which wraps a specified version of Maven for a projects build. When having a Maven Wrapper for a project there is no need to install a certain version of Maven on a machine. Only for the inital creation of the Maven Wrapper files a local Maven installation is necessary.

How can we create a Maven Wrapper?

To create a Maven Wrapper for a project with the latest available Maven version.

C:\...project directory > mvn -N io.takari:maven:wrapper

Create a Maven Wrapper for a project with a specified Maven version by using the maven property.

C:\...project directory > mvn -N io.takari:maven:wrapper -Dmaven=3.3.0

What are the aspects does Maven Manages?
 Maven handles following activities of a developer
• Build
• Documentation
• Reporting
• Dependencies
• SCMs
• Releases
• Distribution
• Mailing list

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

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. Maven reads the pom.xml file, then executes the goal.

Before maven 2, it was named as project.xml file. But, since maven 2, it is renamed as pom.xml.

What are the elements of maven pom.xml file?

  • project:It is the root element of pom.xml file.
  • modelVersion:It is the sub element of project. It specifies the modelVersion. It should be set to 4.0.0.
  • groupId:It is the sub element of project. It specifies the id for the project group.
  • artifactId:It is the sub element of project. It specifies the id for the artifact (project). An artifact is something that is either produced or used by a project. Examples of artifacts produced by Maven for a project include: JARs, source and binary distributions, and WARs.
  • version:It is the sub element of project. It specifies the version of the artifact under given group.
  • packaging:defines packaging type such as jar, war etc.
  • name:defines name of the maven project.
  • url:defines url of the project.
  • dependencies:defines dependencies for this project.
  • dependency:defines a dependency. It is used inside dependencies.
  • scope:defines scope for this maven project. It can be compile, provided, runtime, test and system.
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.

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

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.

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

Explain how you can exclude dependency?
By using the exclusion element, dependency can be excluded

Mention the difference between Apache Ant and Maven? 
  • Ant doesn't has formal conventions, so we need to provide information of the project structure in build.xml file. Maven has a convention to place source code, compiled code etc. So we don't need to provide information about the project structure in pom.xml file.
  • Ant is procedural, you need to provide information about what to do and when to do through code. You need to provide order.    Maven is declarative, everything you define in the pom.xml file.
  • There is no life cycle in Ant.  There is life cycle in Maven.
  • Ant is a tool box, whereas Maven is a framework.
  • Ant is mainly a build tool, whereas Maven is mainly a project management tool.
  • The ant scripts are not reusable. The maven plugins are reusable.

List out what are the build phases in Maven?
Build phases in Maven are
• Validate
• Compile
• Test
• Package
• Install
• Deploy

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.

For POM what are the minimum required elements?
The minimum required elements for POM are project root, modelVersion, groupID, artifactID and version

How to build the maven project or how to package maven project?
The mvn package command completes the build life cycle of the maven project.

A Maven build lifecycle is made up of phases. Each of these build lifecycles is defined by a different list of build phases, wherein a build phase represents a stage in the lifecycle. The default lifecycle comprises of the following phases:
  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • verify - run any checks on results of integration tests to ensure quality criteria are met
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects. 
-K Himaanshu Shuklaa..

No comments:

Post a Comment