maven interview questions and answers

What are the build tools in java?

A build tool is utility program to automate the process of repetitive tasks like compiling source code and creating files etc. A build tool can be executed from the command line. Note: Apache Ant, Maven and Gradle are the commonly used building tools in java.

Why build tools are used?

Build tools are used to automate the process of repetitive tasks like source code compilation etc and take this burden away from the developers.  

What is Maven?

Apache maven is an advanced project management tool for java software projects which is based on POM (project object model). It uses POM (project object model) file to manage project’s build, dependency and documentation.

The most powerful futures of maven are to download the project dependency libraries automatically and to create the right project structure

What are the aspects Maven manages?

Maven helps to manage the following: Builds Documentation Reporting Dependencies SCMs Releases Distribution  

What are the advantages of Maven?

  • Simple project setup that follows best practices.
  • Consistent usage across all projects.
  • Dependency management: Superior dependency management including automatic updating.
  • Ability to easily work with multiple projects at the same time.
  • Repository: A large and growing repository of libraries and metadata. Project dependencies can be loaded from repository.
  • Extensible via plug-ins: Extensible, with the ability to easily write plugins in Java or scripting languages.
  • Instant access to new features with little or no extra configuration.
  • Model based builds: Maven is able to build any number of projects into predefined output types such as a JAR, WAR and metadata.
  • Coherent site of project information: Using the same metadata as for the build process, Maven is able to generate a web site or PDF including any documentation.
  • Release management and distribution publication: Without much additional configuration, Maven will integrate with your source control system like Subversion or Git and manage the release of a project based on a certain tag.
  • Backward Compatibility: We can easily port the multiple modules of a project into Maven 3 from older versions of Maven.
  • Better Error and Integrity Reporting: Maven improved error reporting, and it provides a link to the Maven wiki page where we will get full description of the error.

How to check the maven version in windows?

Apache maven is an advanced project management tool for java software projects which is based on POM (project object model). Command to check the maven version in windows

c:\> mvn –version

 

How to check the maven version in linux?

Apache maven is an advanced project management tool for java software projects which is based on POM (project object model). Command to check the maven version in linux

$ mvn –version

 

How to check the maven version in mac?

Apache maven is an advanced project management tool for java software projects which is based on POM (project object model). Command to check the maven version in mac

machine:~ joseph$ mvn –version

What is POM?

POM refers to Project Object Model. It is an XML file which contains the information about the project and various configuration detail used by Maven to build the project like build directory, source directory, dependencies, test source directory, plugin, goals etc. The POM file should be in the project’s root directory.

  • Maven reads the pom.xml file.
  • Downloads dependencies into local repository.
  • Execute life cycles, build phases and goals.
  • Execute plugins.

What information does pom contain?

 

Element Description
project This is the root element of pom.xml file.
modelVersion This is the sub element of project which specifies the modelVersion. Model version should be 4.0.0.
groupId This is the sub element of project which specifies the id for the project group.
artifactId This is the sub element of project which specifies the id for the project. This is generally refers to the name of the project. The artifact ID is also used as part of the name of the JAR, WAR or EAR file produced when building the project.
version This is the sub element of project which specifies the version of the project.
packaging It is used to define the packaging type such as jar, war etc.
name It is used to define the name of the maven project.
url It is used to define the url of the project.
dependencies It is used to define the dependencies for this project.
dependency It is used to define a dependency. It is used inside dependencies element.
scope It is used to define the scope for this maven project. It can be compile, provided, runtime, test and system.

What is Maven Build Lifecycle?

Maven build life cycle is divided into build phases, and the build phases are divided into build goals.

Explain the 3 build lifecycle of Maven.

  • clean: Itcleans up artifacts created by prior builds.
  • default (or build): It is used to build the application.
  • site:It generates site documentation for the project.

What are the maven lifecycle phases?

Phase Description
prepare-resources This phase is used to can be customize the resource copying.
validate  Validate the project is correct and all necessary information is available.
compile It compiles the source code of the project.
Test Tests the compiled source code using a suitable testing framework.
package This phase take the compiled code and creates the JAR/WAR package as mentioned in the packaging in POM.xml.
install This phase installs the package in local maven repository.
Deploy This phase copies the final package to the remote repository.

What is a goal in maven?

Build goals are the finest steps in the Maven build process which represents a specific task. A goal can be bound to none, one or more build phases. If a goal is not bound to any build phase, we can only execute it by passing the goals name to the mvn command.

What is Build Profile?

Maven build profiles provides the facility to build project using different configurations. Instead of creating two separate POM files, we can just specify a profile with the different build configuration and build the project with this build profile when needed.

The profiles element is used to specify the maven build profiles inside the POM file. Each build profile is nested inside a profile element. The elements inside the profile element will override the values of the elements with the same name further up in the POM. The activation element inside the profile element is used to describe the condition that triggers the build profile to be used. We can use settings.xml file to tell maven which profile is to be executed. Another way is to add -P profile-name to the Maven command line.

What is a maven repository?

Maven repositories are directories of packaged JAR files with extra meta-data. The meta-data is represented by POM files. A repository contains all the project jars, library jar, plugins and any other project specific artifacts.

Types of maven repository:

  1. Local Repository
  2. Central Repository
  3. Remote Repository

What are the types of repository in maven?

Maven repositories:

Maven repositories are directories of packaged JAR files with extra meta-data. The meta-data is represented by POM files. A repository contains all the project jars, library jar, plugins and any other project specific artifacts.

Types of maven repository:

  1. Local Repository
  2. Central Repository
  3. Remote Repository

Explain Maven repository search order.

Maven repositories are directories of packaged JAR files with extra meta-data. The meta-data is represented by POM files. A repository contains all the project jars, library jar, plugins and any other project specific artifacts.

Maven repository search order

Local repository then Central repository then Remote repository.

Note: Maven stops processing and throws an error if dependency is not found in these repositories.

What is local repository in maven?

Maven local repository is a directory on the developer’s machine. It gets created when we run any maven command for the first time. It contain all the dependencies (downloaded by maven) like library jars, plugin jars etc.

Default location of maven local repository is user-home/.m2 directory. We can change the default location of maven local repository by changing the settings.xml file. It is located in MAVEN_HOME/conf/settings.xml.

<settings>
    <localRepository>
        //Set desired location
    </localRepository>
</settings>

What is central repository in maven?

Maven central repository is created by the apache maven community itself. It contains a lot of commonly used libraries. By default Maven looks in this central repository for any dependencies needed but not found in your local repository. Maven central repository path: http://repo1.maven.org/maven2/.

What is remote repository in maven?

Maven remote repository is a repository on a web server. A remote repository can be located anywhere on the internet or inside a local network. We can configure a remote repository in the POM file. We have to put the following XML elements right after the element:

<repositories>
   <repository>
       <id>w3spoint.code</id>
       <url>https://maven.w3spoint.com/maven2/lib</url>
   </repository>
</repositories>

What is maven plugin used for?

A maven plugin represents a set of goals which provides the facility to add your own actions to the build process.

Syntax to execute a plugin:

mvn [plugin–name]:[goal–name]

Maven plugins can be used to:

  • create jar file.
  • create war file.
  • compile code files.
  • unit testing of code.
  • create project documentation.
  • create project reports.

What are the types of maven plugins?

Maven plugin types:

  1. Build Plugins
  2. Reporting Plugins

Build Plugins: Build plugins are executed at the build time. Build plugins should be declared inside the element. Reporting Plugins: Reporting plugins are executed at site generation time. Reporting plugins should be declared inside the element.

What is archetype in maven?

Archetype is a Maven plugin which is used to create a project structure as per its template. Example:

Maven-archetype-webapp

How profiles are specified in maven?

Maven build profiles provides the facility to build project using different configurations. We can just specify a profile with the different build configuration and build the project with this build profile when needed.

Profiles are specified using a subset of the elements available in the POM itself.

How can you build your project offline?

Use the below command to build your project offline.

mvn o package

How to exclude dependency in maven?

We can exclude dependency in maven by using the exclusion element.

Difference between Ant and Maven?

 

Ant Maven
Ant doesn’t has formal conventions such as a common project directory, so we need to provide information of the project structure in build.xml file. Maven consists of conventions 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, we have to specifically order what should have to be done. Maven is declarative, it takes care of all the directories once the files are stored in the pom.xml file.
Ant does not have a life cycle Maven has a life cycle.
Ant is a tool box. Maven is a framework.
Ant is mainly a build tool. Maven is mainly a project management tool.
The scripts in Ant are not reusable. The maven plugins are reusable.
Ant is less preferred than Maven. Maven is more preferred than Ant.

 

Please follow and like us:
Content Protection by DMCA.com