Top 21 Maven Interview Questions Answers for Java Developers

Hello guys, for a Java developer, Maven in one of the essential tool and its used to build your Java application. Since most of the Java projects used Maven for building, it is expected from a Java developer that he knows Maven and knows it well. That's why Maven questions are quite common on Java developer interviews but I have seen many Java programmers coming to these interview unprepared and not able to answer most of the questions on tools like Maven, Gradle or even git. When I asked one candidate why don't you prepare for Maven before your Java interviews, he said, I wasn't able to find any list of Maven questions. 

Well that amused me a bit but as a blogger, I took that up as a challenge and create this list of common Maven interview question for Java developers.

In this list, I have not just shared frequently asked Maven questions but also tried to cover as many important Maven concepts as possible so that it not just prepare your for interview but also open doors for learning Maven better.

 

21 Maven Interview Questions with Answer for Java Developers

Here are the common Maven questions from Java interviews which every Java developer should be aware of. They also test and cover key Maven concept including why Maven and What Maven offer to Java developers, including dependency management. 

1. What is Maven?

Maven is a build tool for Java applications. It provides a standard directory structure for Java program and also automatically manage the dependent JAR files. It introduces concept of repository which can be local or remote and downloads dependent JAR from there. Maven also provides a maven central library which host all open source library artifacts e.g. JAR files for all versions.


2. What is meaning of Convention over configuration in Maven?

As I said in previous questions, Maven provides a standard directory structure to organize your Java project. So you don't to configure everything, Maven uses decent default values to find things on there own. If you use Maven in your project any Java developer having worked in Maven knows where to find the source and tests.


3. What is difference between ANT and Maven?

Though both are popular build tools to build Java application, Maven is much more than ANT as it provides standard directory structure but ANT needs you to define your own. Also Maven is goal based but ANT is task based. Maven provide automatic dependency management, AND doesn't provide that. See here for more difference between Maven and ANT.


4. What are advantage of using Maven in Java Project?

-automatic dependency management

- easy way to build and deploy your application

- standard directory structure


5. What is goal in Maven?

A goal is like a task you can run on Maven. For example, you can have a maven goal to compile your project, one goal to build JAR, another goal to upload artifacts on repository and so on. 


6. From where does Maven download dependency JAR?

From configured local and remote repository. You can configure those in Maven's configuration file settings.xml, which you can find in M2_HOME/conf directory.


7. Which command is used to clean old builds in Maven?

You can use $ mvn clean to cleanup the target directory created by previous build.


8. Can we use Maven with Eclipse?

Yes, You can use M2Eclipse plugin to integrate Maven with Eclipse.


9. What is difference between local and remote repository in Maven?

The local repository sits on user's local machine while remote repository is usually on some other host. Your remote repository could be Nexus or Maven central repository.


10. How do you find maven version you are running?

By using mvn -version command


11. What is pom.xml in Maven?

This is the main file which given instruction to maven about build the project, required dependency and other configuration details. It stands for Project object model but a similar file build.xml required by ANT. You can see the pom.xml at root directory of your project.


12. What is archetype in Maven?

Archetype is the maven plugin which is used to create project structure like you would have separate archetype for core Java project and Java web application because you need to organize web content as well.


13. How do you build Maven project from command line?

You can run following command to build your maven project form command line

$ mvn clean install

Tthis will clean the target directory of the project, compile the source code and executes the tests and finally install the created artifacts e.g. JAR, WAR or EAR into the user's local maven repository. 


14. How do you build Maven project from Eclipse?

If you have M2Eclipse plugin installed in your Eclipse then you can build the Maven project by just right click and Run as Maven Install command.


15. What is difference between mvn build and mvn install?

The mvn build command will just build the artifact e.g. JAR or WAR file but mvn install also copies it to local maven repository and can copy to Nexus or remote maven repository depending upon your configuration.


16. What is the Maven's configuration file?

The settings.xml is Maven's configuration file, which is usually place in the user's home directory under the ".m2" folder, or you can also find it inside M2_HOME/conf directory where M2_HOME is Maven's installation directory e.g. C:\maven

Top 21 Maven Interview Questions Answers for Java Developers


17. Can you build a Maven project without running all the tests?

Yes, you can exclude tests by using skpTests or maven.test.skip property e.g.

$ mvn install -DskipTest=true 

will just compile the source code for test classes but doesn't run the tests, if you don't want to compile the tests then you can use maven.test.skip property as shown below :

$ mvn install -Dmaven.skip.test=true


18. What is difference between SkipTest and maven.skip.test System property in Maven?

Both are used to not run test during build but SkipTest compiles the test class and maven.skip.test doesn't compile them.


19. Where does Maven generate output e.g. JAR files, classes etc?

Every time you build a Maven project, it creates a target directory, you can find all compiled class files and JAR file there.


20. What is transitive dependency in Maven?

transitive dependency are JAR files required by your required dependent libraries or framework. For example, if you add Spring as dependency then all the JAR file Spring needs e.g. logging JARs becomes transitive dependency.


21. How do you release project using Maven?

By using mvn release:prepare and mvn release:perform, first one creates release second one actually does the release.


That's all about 21 Maven interview questions for Java developers. As I said, Maven is one of the essential tool for Java development as its used to build and install your application. You should also prepare Maven questions before you go for any Java interview to showcase your Maven skills and also be able to answer any question interviewer ask on Maven or build tools in Java. 


No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.