What is Maven ?
Maven is a project management and comprehension tool. It works as a build automation tool. Maven is primarily used for Java projects.
Maven is based on POM (Project Object Model). It also simplifies the build process like other tools such as ANT but maven is much more advanced and easy than ANT.
Apache Maven makes the work for developers very easy as it provides a complete build lifecycle framework.
Maven uses a default build cycle and also a standard directory for building infrastructure. So developers can very easily automate the application’s build infrastructure.
What are the important features of Maven?
Below are some of the important features of maven that has to be in the top list of maven interview questions –
- Easy to use – Maven is extremely easy-to-use. It has very easy project settings which are based on genuine practices .
- Easy to learn – The usage of maven is easy to learn. The command provided by Maven are easy to learn. Hence , even new developers can easily command the maven tool and can work using Maven in no time.
- Fast – Maven tool is a fast tool. It only takes fewer seconds to create a fresh project or module.
- Multiple Projects allowed– We can easily work on multiple projects at the same time by using the Maven tool.
- Dependency management – Maven also provides dependency management feature. It checks for automatic updates as well as transitive dependencies and manages them on its own.
- Huge Library – You will almost get every library in the Maven repository . It has a very huge and growing repository of metadata and libraries to use.
What it does ?
Maven does following tasks –
- Maven makes the build process of a project very easy.
- Maven provides all the project information such as dependencies list, cross referenced sources, log documents, mailing list , unit test reports etc.
- Maven also provides a uniform build process.
- We can share one Maven project with all the other maven projects.
- As new features and version of Maven releases, those can be easily migrated with Maven old projects as Maven is very easy to migrate.
What aspects are managed by Maven?
Below are some of the important aspects that are managed by Maven –
- Mailing list
List some of the advantages of Maven?
Below are some of the advantages of Maven –
- Maven builds and deploys the project on its own.
- Multiple project is allowed in maven but there is no need to add the jar file into each project.
- It creates the right directory structure.
- It makes development and build very easy.
- It is very easy to learn and no need to have excellence in this tool to use it.
What is Build Tool and What it does?
A build tool is simply a tool which takes care of the complete build process of a project. As Maven is also a build tool, this question has to be added in the Maven interview questions.
Build tool has following tasks –
- It will generate the source code if the auto-generated code is used in the project.
- It generates the documentation from source code.
- It also compiles the source code of the project.
- It packages the compiled code into JAR of ZIP file.
- It installs the packaged code in the repository such as local repository, server repository, or central repository
Why should we use Maven?
Maven has several advantages as we have seen in above questions. This tool helps in setting up a project very easily and quickly.
It does not have any complicated build files such as build.xml, which makes it easy to use. It only uses pom.xml file as its core.
POM.xml file is very easy to understand and to create for your project.
POM stands for Project Object Model. This xml file contains all the dependencies that you require for your project.
Whichever dependency we will add in the pom.xml file, the project will download it from web and will store it in repository.
Maven’s POM helps in building all the required Jar files into one package. This makes the deployment very light.
What will be the problem without Maven?
Maven helps in solving many problems that developers faces during the project development. Some of those problems are :
1) The right project structure – In many frameworks, the correct project structure is strictly necessary otherwise the project will never be executed. Well, This is not the case in Maven.
2) Requirement to add set of Jars in each project – Many frameworks like spring , struts etc requires the addition of jar file into each project and also the dependencies of jars. This solved by Maven.
3) Build and Deployment of the project – Maven project does not need to be built and deployed by the developer.
What are the steps for installing Maven on windows?
Maven is supported on Windows, Mac OS, Linux platforms. It’s easy to download and install Maven in any platform.
Below are the steps required for download and installing Maven on Windows –
- Download the Maven and extract it in some folder.
- Add the variables – JAVA_HOME and MAVEN_HOME in the list of environment variables in your system.
- Then Add the env path in your Maven variable.
- Finally, Check the version of Maven to verify the download.
What is the command to check the maven version?
Run the below command in the cmd prompt of your system to check the version of the downloaded Maven –
What is the difference between Ant and Maven?
Below are some of the differences between Maven and Ant tool –
|Maven is a framework.
|Ant is a toolbox.
|Maven has a lifecycle.
|Ant has no lifecycle.
|Maven is basically a Project Management tool.
|Ant is a build tool.
|Maven is a declarative tool.
|Ant is a procedural tool.
|Maven Plugins and features are reusable.
|Ant scripts can be used only once.
|Maven does have the convention to place source code etc.
|Ant tool does not have facility of formal conventions.
|Maven is more preferred.
|Ant is less preferred.
What is a Maven POM file?
The POM file stands for Project Object Model.
It is the core and fundamental unit of Maven. It is an XML file. It is created and stored in the base directory of project.
This xml file contains all the information for the projects, configurations, build details etc.
It also contains the default values for most peojects.
Below is one sample POM.xml file –
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tbs.app</groupId> <artifactId>my-app</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>Maven Quick Start Archetype</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <artifactId>junit</artifactId> <version>4.8.0</version> </dependency> </dependencies> </project>
What information does POM contain?
The POM file contains the below mentioned information about project−
- Project dependencies
- Project version
- Build profiles
- Mailing list
What is an artifact in Maven?
An artifact in Maven is simply a file. This file is usually a JAR file. An artifact in Maven is deployed in the maven repository.
The Maven build create one or more than one artifacts for project. These artifacts are simply the compiled jars and the source jars etc.
Each artifact of Maven has a three things –
1) a group ID 2) artifact ID 3) version
These three in combination defines a unique artifact of a Project.
All the Project dependencies are the artifacts.
What is the Maven Build lifecycle?
A Build Lifecycle in any tool or framework is simply described as a well defined sequence of phases.
Maven Build Lifecycle determines the order of the execution of goals for the project.
Each phase of the build lifecycle contains the sequence of goals. Suppose, One cycle is executed then all the build phases of the executing cycle will be executed.
And Suppose, If only a build phase is executed then the all build phases before it in the pre-defined sequence of build phases gets executed.
What are the 3 build lifecycle of Maven?
Below are the three build lifecycles of Maven –
clean – This build lifecycle of Maven is used to clean up the artifacts which were created by the previous builds.
default – This lifecycle is used to build the application.
site – This build lifecycle is used to generate the site documentation for the project.
Which are the phases consists in a Clean Lifecycle ?
The clean lifecycle in Maven is consists of the below mentioned phases –
Which are the phases consists in a Site Lifecycle ?
The site lifecycle in Maven is consists of the below mentioned phases –
What are the build phases in Maven?
Below are the phases of a build in Maven tool / framework –
- Validation phase.
- Compilation phase.
- Testing phase.
- Package phase.
- Integration testing phase.
- Verification phase.
- Installing phase.
- Deployment phase.
What is the command to build your Maven site?
Below command is used to build the Maven site –
− mvn site
What is the use of command mvn clean ?
The command mvn clean is used to delete the target directory along with all the build data just before starting the build process.
What is a goal in Maven terminology?
A Goal in Maven is a specific task. These tasks / goals helps in building and managing of a Project.
It may be bounded to more than on build phases or it may not be bounded to any build phase.
A Maven goal which is not bounded with any build phase can be executed outside of the build lifecycle. We can execute it by directly invoking the goal.
What is the use of command mvn clean dependency:copy-dependencies package?
The above mentioned command is used to perform below mentioned steps –
- Clean the project
- Copy the dependencies
- Package the project
What is Build Profile?
A Build profile in Maven is consists of / a set of configuration values. These values are used for overriding or setting the default values of Maven build.
We use build profiles of Maven to customize or project’s build for different environments such as Development and Production environments.
What are different types of Build Profiles?
There are three types of Build Profiles in Maven, which are :
1) Per Project − This profile is defined in the project pom.xml file.
2) Per-User − This profile is defined in Maven settings XML file.
3) Global −This profile is defined in Maven global settings xml file.
How can you activate profiles?
There are several ways to activate a profile in the Maven Project –
- We can explicitly use command console input to activate any profile.
- We can activate it through maven settings.
- By Using environment variables (User/System variables).
- With OS Settings
- Or by Present/missing files.
What is a Maven Repository?
In Maven, A Repository is a place or a directory which contains all the library files, project jars, project artifacts, plugins.
You can find all these stored information in the Maven Repository and can easily use it.
What types of Maven repository?
There are three types of Maven repositories exists –
The Local repository of Maven present in your machine. It is basically a folder present at your local machine.
This repository or folder is created at the time when we run our first maven command.
Maven Local repository is a repository where all the project dependencies are stored. Such as the jars, plugins etc.
The Central Repository of Maven is provided by the Maven community and it is not present in your machine.
The Central Repository is responsible for containing the huge collection of all the commonly used libraries.
In case, Maven does not find a dependency in your local repository then it looks for that dependency in the Central Repository of Maven .
Below is the URL of Central Repository of Maven –
Maven sometimes cannot locate the required dependency in the local as well as central repository of Maven. In such cases, the build process can be stopped and the error can be displayed as Output.
For solution to this problem, Maven comes up with the Remote Repository. This Repository is the custom repository of developer, which contains the required libraries and the project jars etc.
What is the default location of local repository?
The default location of local repository in machine is –
What is the command to install JAR file in local repository?
Below command is used to install any jar file in the local repository –
What are the uses of Maven Plugins?
Maven is simply a framework which is a collection of multiple Plugins.
Plugins are the main areas where almost every work of Maven is done. Maven Plugins are basically used for
- Creating the jar file.
- Creating the war file.
- Compiling code files.
- Performing unit test on code.
- Creating the documentation of project.
- Creating the project reports.
What are the types of Maven Plugins?
There are two types of Maven Plugins exists , those are :
Build plugins : Build Plugins of Maven are used in the build process. These plugins are configured in the pom.xml file under the <build/> element.
Reporting plugins : Reporting plugins are executed at the time of site generation. These plugins are configured in pom.xml file under the element <reporting/>.
What is a Maven dependency?
Maven dependencies can be simply seen as libraries required for the projects.
Dependency in Maven is another archive such as JAR, ZIP etc. These archives are required for the project in order to complete the project whole process such as build, test, run etc.
All the dependencies are mentioned in the pom.xml file under the tag <dependencies>.
Whenever we run a build or whether executing a maven goal the the mentioned dependencies are resolved and gets loaded from the local repository.
If not present in local repository then these are downloaded from central or remote repositories. We can also manually install the dependencies of project.
What does Maven dependency plugin do?
Apache Maven Dependency Plugin is used for manipulating the artifacts.
We can use this plugin to copy artifacts from local or remote repositories to any location.
Where are Maven dependencies downloaded to?
All the dependencies, jars, libraries, and other files etc exists in the Maven Local repository (if downloaded automatically be Maven).
The default Maven Local repository location is – ~/m2./repository
We can manually copy the jar directly into location where it is required.
What is an archetype in Maven?
An Archetype in Maven is a project templating toolkit.
An archetype can also be seen as a Maven plugin which is responsible for creating a project structure as per its template.
It is like an original pattern or model from which other things of the same kind are created.
What is the command to create a new project based on an archetype?
Below mentioned command is used in maven for above purpose –
What is SNAPSHOT in Maven?
In Maven , A SNAPSHOT is a special version which indicates a current development copy.
It is not like the regular versions as Maven specifically checks for a new SNAPSHOT version in its remote repository.
This process is done for each and every build by Maven.
What is the command to package maven project?
Below is the command used for packaging any Maven Project –
What is the fully qualified artifact name of maven project?
Below is the fully qualified name of the Maven Project-
Why is the use of the profile required in Maven?
Profiles in Maven are used to provide the Probability to the Project.
What is the syntax for offline project creation?
Below is the syntax to create the project offline in Maven –
mvn o packg
What is the use of the exclusion element?
The exclusion element in Maven is used for excluding any dependencies for any project.
How is the propagation of plugins to child POMs stopped?
Below syntax is used to stop the propagation of the plugins to the child POMs –
set<inherited> to false
What is a maven assembly?
The Assembly Plugin for Maven is basically used for allowing the users to aggregate the output of the project along with the project modules, dependencies, site documentations etc.
It aggregates all these things into one single distributable archive file.
What is meant by Mojo in Maven?
Mojo in Maven stands for Maven plain Old Java Object.
Maven Mojo is simply an executable goal in Maven and a plugin is considered as a distribution of one or more related mojos.
Mojo is a maven goal which is used to extend the functionality which is not found in maven.
What is the use of Maven clean?
The Maven Clean is a Plugin. It is used to clean the directories, files etc during the build of Project in Maven.
Well there are some plugins that generate additional files so the Clean Plugin always assumes that these files are generated inside the target directory.
Where are Maven dependencies stored?
All the dependencies of Maven is stored in the Local Repository of Maven.
The default location of Maven Local repository is – ~/m2./repository
What are the Maven advantages over Ant?
Below are some of advantages of Maven over Ant tool –
1. Convention over Configuration
Maven framework uses a very distinctive approach for project layouts and startups etc so it is very easy to quickstart a project.
It usually takes the checkout and the maven command only to get the artifacts of any project in Maven.
2. Project Modularization
It is always recommended to modularize the project. Maven has this advantage.
So Projects are forcefully divided into smaller sub components. Hence It’s easy to debug and manage the project.
3. Dependency Management and Project Lifecycle
Dependency Management is extremely easy in Maven because of its repositories and configurations etc.
It has Project Lifecycle containing the versions, releases etc which also makes it more advantageous over Ant.
How do you tell Maven to use the latest version of a dependency?
We setup the dependencies in Maven something like below –
<dependency> <groupId>...</groupId> <artifactId>.../artifactId> <version>...</version> </dependency>
Now, Suppose you are working with libraries which have frequent releases so it will be difficult to constantly changing the <version> tag whenever the new version is released.
So Is there any way to tell Maven that always uses the latest available version of a dependency?
Yes, There is.
In the Maven version 2, we have two keywords which can be used to tell Maven to always use the newest version of dependency.
Below are those two keywords –
A square bracket ( [ & ] ) means “closed” (inclusive).
A parenthesis ( ( & ) ) means “open” (exclusive).
Let’s understand this with Example –
Consider below as metadata exists in Maven repository for artifact my-app –
<?xml version=”1.0″ encoding=”UTF-8″?><metadata> <groupId>com.tbs</groupId> <artifactId>my-app</artifactId> <version>2.0.0</version> <versioning> <release>1.1.1</release> <versions> <version>1.0</version> <version>1.0.1</version> <version>1.1</version> <version>1.1.1</version> <version>2.0.0</version> </versions> <lastUpdated>20190824130000</lastUpdated> </versioning> </metadata>
Now , you have below options –
To declare an exact version to use :
To declare an explicit version ( this will always resolve to 1.0.1 (as per above metadata) ) :
To declare a version range for all 1.x ( for above example it will currently resolve to 1.1.1 ):
To declare an open-ended version range ( It will resolve to version 2.0.0 ):
What is force maven update?
Below command can be used for force updating maven –
mvn clean install -U -U means force update of the snapshot dependencies. Release dependencies are not updated this way.
How do you host a Maven repository on github?
Below are the steps to follow to host a Maven Repository in Github –
- Create any branch in Github to host your maven artifacts.
- Use the maven plugin available for github : site-maven-plugin. It will push your artifacts to the github.
- Finally, Configure maven to use your remote repository (created in Github) as a maven repository.
The basic way you deploy artifacts to a remote maven repo is to use command – mvn deploy.
What is a transitive dependency in Maven?
Transitive dependency in Maven is nothing but a way in which we avoid the need to discover and specify the libraries that your project’s already defined /own dependencies require.
Maven automatically resolves these dependencies.
What is a system dependency?
System dependency in Maven is simply a dependency which has scope as system.
These dependencies are always available and also these are not looked up in the repository.
These dependencies usually used for telling the Maven about the dependencies which are provided by the JDK or the VM.
Therefore, The system dependencies are mainly used for resolving dependencies on artifacts that JDK normally provides.
How to run the clean plugin automatically during the build?
If we place the clean plugin of Maven inside the execution tag in pom.xml file, then it will be automatically be run by the Maven during the build.
What is the use of optional dependency?
We can specifically mark any transitive dependency in Maven as optional dependency.
It can be done using the “optional” element.
For example, Suppose X depends upon Y and Y depends upon Z. Now if we mark Z as optional.
Then X will not use Z.
What are the benefit of storing external dependencies in the local repository instead of a remote ?
- Less Storage is needed.
- CheckOut of Project is Quicker.
- No need for additional versioning of Jars.
List the elements in POM that a profile can freely modify when specified in the POM?