Pages

Thursday, November 7, 2013

Maven in 15 Minutes - Maven Top Interview Questions

What is Maven?

In simple terms, Maven is a build tool

How Maven is different from other build tools?

Maven makes building a project very simple. The complex part of the build process is gathering all the required jar files which maven takes care automatically through a concept called dependency management. Also Maven comes up with pre-defined packaging mechanisms for Jar,War,Ear which makes the build process really simple.

What is a POM file?

Maven needs a configuration file called POM (Project Object Model) to know the information about the project to be build. Maven proposes standard way of storing the source code, test code and configuration files. If the project is structured this way then lot of things are automatically taken care by Maven.

What are the basic elements in a POM file?

POM file is a XML file. The basic POM file contains the GroupId, ArtifactId, Version

<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.ravi.sample</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0</version>
</project>

What is the standard maven project structure?

-src
--main
---java
--test
---java
--resources

What is a Maven Plug-in?

Each of the activities in the build process is done through a different components. These components are called plug-ins in Maven terms. For example compiling is done through a plug-in called compiler plug-in.
Creating a war file is done through maven war plug-in.

What is dependency management?

Any java code need or depend on additional Jar files apart from what is availble in JDK. Maven makes building a project really easy by automatically downloading these jar files once the required Jar files are declared in the POM file.

What are the mostly used Maven commands on a day to day basis

Mvn install - Compiles the projects, Run the tests if there are any and generates the build artifacts.

Mvn clean - Cleans the project and removes all the build artifacts created.

How to integrate Maven with Source control system

The SCM tag in the pom file.

<scm>
    <connection>scm:svn:http://somerepository.com/svn_repo/trunk</connection>
    <developerConnection>scm:svn:https://somerepository.com/svn_repo/trunk</developerConnection>
    <url>http://somerepository.com/view.cvs</url>
<scm>

Mvn Release:prepare - Prepares to release the project.

Mvn Release:perform - Uploades the artifacts to the repository

What are the best practices in Maven setup

1) Use Parent POM with out exception and define all the common things there.
Common things here are

- properties
- dependency management
- scm
- distribution management
- repositories

2) Dont explicitly mention the version numbers of the dependency jar files in the POM files
except in the parent pom files. Mention version numbers in the parent POMs.

In the parent pom file the dependency management tag is used to define all the dependency jar files with
version numbers.  If this parent pom file is used in all the other pom files in the project then
you dont have to change the version number in each and every pom file.

No comments:

Post a Comment

 
Blogger Templates