DevOps Interview Questions – Here are some of the following Maven Interview Questions and Answers that are generally asked in DevOps interviews.
Maven is a project management and comprehension tool. Maven provides developers a complete build lifecycle framework. Development team can automate the project’s build infrastructure in almost no time as Maven uses a standard directory layout and a default build lifecycle.
Maven provides developers ways to manage following −
POM stands for Project Object Model. It is fundamental Unit of Work in Maven. It is an XML file. It always resides in the base directory of the project as pom.xml. It contains information about the project and various configuration details used by Maven to build the project(s).
POM contains the some of the following configuration information − project dependencies plugins goals build profiles project version developers mailing list
An artifact is a file, usually a JAR that gets deployed to a Maven repository. A Maven build produces one or more artifacts, such as a compiled JAR and a “sources” JAR. Each artifact has a group ID (usually a reversed domain name, like com.example.foo), an artifact ID (just a name), and a version string. The three together uniquely identify the artifact. A project’s dependencies are specified as artifacts.
In Maven, the portability of a build is the measure of how easy it is to take a particular project and build it in different environments. A build that does not require any custom configuration or customization of properties files is more portable than a build that requires a lot of custom work to build it from scratch. Open source projects from Apache Commons are one of the most portable projects. These builds can work just out of the box.
Maven build lifecycle is defined by a list of build phases, where a build phase represents a stage in the lifecycle. The default lifecycle comprises of the following phases. validate – validates the project is correct and all necessary information is available. compile – compile the source code of the project. test – tests the compiled source code using a suitable unit testing framework. These tests does not require the code to be packaged or deployed. package – take the compiled code and package it to its distributable format, for example, JAR. verify – runs any checks on results of integration tests to ensure desired quality criteria are met. install – installs the package into the local repository, for using it as a dependency in other projects locally. deploy – performed in the build environment, copies the final package to the remote repository for sharing and collaboration with other members of the team and projects.
The phases in Site Lifecycle are − pre-site site post-site site-deploy
Maven Plugins are used to − create jar file. create war file. compile code files. unit testing of code create project documentation. create project reports.
Maven provides following two types of Plugins − Build plugins − They execute during the build and should be configured in the <build/> element of pom.xml Reporting plugins − They execute during the site generation and they should be configured in the <reporting/> element of the pom.xml
Dependency with scope system are always available and are not looked up in repository, they are usually used to tell Maven about dependencies which are provided by the JDK or the VM. Thus, system dependencies are especially useful for resolving dependencies on artifacts which are now provided by the JDK.
In Maven, jar:jar goal creates a jar file in the Maven build directory. Jar file is create with the name format ${project.id}-${project.currentVersion}.jar. The id and currentVersion are mentioned in the project.xml of the project being built. jar:jar does not recompile sources. It just creates a jar from already compiled classes.
Maven local repository is a folder location on your machine. It gets created when you run any maven command for the first time. Maven local repository keeps your project’s all dependencies (library jars, plugin jars etc).
It is repository provided by Maven community. It contains a large number of commonly used libraries. When Maven does not find any dependency in local repository, it starts searching in central repository using following URL: http://repo1.maven.org/maven2/.
Sometimes, Maven does not find a mentioned dependency in central repository as well then it stops the build process and output error message to console. To prevent such situation, Maven provides concept of Remote Repository which is developer’s own custom repository containing required libraries or other project jars.
A SNAPSHOT version in Maven is the one that has not been released. Before every release version there is a SNAPSHOT version. Before 1.0 release there will be 1.0-SNAPSHOT. If we download 1.0-SNAPSHOT today then we may get different set of files than the one we get on downloading it yesterday. SNAPSHOT version can keep getting changes in it since it is under development. But release version always gives exactly same set files with each download.
Maven supports following dependency scopes: compile: This is the default dependency scope in Maven. The compile level dependencies are available in all classpaths of a project. These dependencies are also propagated to dependent projects. provided: This scope is similar to compile. But in this scope we expect the JDK or a container to provide the dependency at runtime. E.g. While building a web application for the Java Enterprise Edition, we can set the dependency on the Servlet API and related Java EE APIs to scope provided. The web container will provide these classes at runtime to our application.This scope is only available on the compilation and test classpath, and is not transitive. runtime: The dependency in this scope is not required for compilation. It is required for execution. It is available in the runtime and test classpaths. It is not present in the compile classpath. test: This scope is used for dependencies that are required for test compilation and execution phases. This scope is not transitive. system: This scope is same as provided scope, except that you have to provide the JAR that contains it explicitly. In this case, he artifact is always available. There is no need to look it up in a repository. import: This scope is only used on a dependency of type pom in the section. In this case, the specified POM has to be replaced with the dependencies in that POM’s section. This scope is only available in Maven 2.0.9 or later.
In Maven, we can create a parent project that will pass its values to its children projects. A multi-module project is created to manage a group of other sub-projects or modules. The multi-module relationship is like a tree that starts from the topmost level to the bottom level. In a multi-module project, we specify that a project should include the specific modules for build. Multi-module builds are used to group modules together in a single build. Whereas in Inheritance, the parent-child project relationship starts from the leaf node and goes upwards. It deals more with the definition of a specific project. In this case a child’s pom is derived from its parent’s pom.
Gradle is nowadays getting more popular. Google uses it for Android development and release. Companies like LinkedIn also use Gradle. Gradle is based on Domain Specific Language (DSL). Maven is based on XML. Gradle gives more flexibility to do custom tasks similar to ANT. Maven scripts have predefined structure. So it is less flexible. Maven is mainly used for Java based systems. Gradle is used for a variety of languages. It is a Polyglot build tool.
Maven has a long list of advantages for Software development. Some of the main advantages are: Common Project Structure: By using Maven, every developer has a common project structure that helps in understanding the code as well as developing new features in a new project. Modular Design: Maven promotes modular design that divides a complex project into multiple modules that are easier to manage. By using Maven, it is easier to manage multiple modules for build, test, release etc. Centralized Dependency Management: With Maven, each developer does not have to include the jars separately in each project or module. Maven provides a centralized dependency management that can help improve efficiency of software development. Fewer Decisions: With Maven a developer has to make fewer decisions about things unrelated to software development work. The project structure comes ready with Maven, dependency management is a uniform approach and build/release is handled by Maven. So a developer can focus on core work of developing software.
What is Maven?
What are the aspects Maven managing?
What is POM?
What information does POM contain?
What is Maven artifact?
What is Build portability in Maven?
Explain the difference phases in Maven build Lifecycle.
What phases does a Site Lifecycle consist?
Why are Maven Plugins used?
What are the types of Maven Plugins?
What is a system dependency?
What is the result of jar:jar goal in Maven?
What is local repository?
What is Central Repository?
What is Remote Repository?
What is the difference between a Release version and SNAPSHOT version in Maven?
What are the different dependency scopes in Maven?
What is the difference between Inheritance and Multi-module in Maven?
What are the differences between Gradle and Maven?
What are the main advantages of Maven?