Maven Interview Questions

Share Embed Donate


Short Description

Download Maven Interview Questions...

Description

1. What are the aspects Maven manages? Answer: Build, Documentation, Reporting, Dependencies, SCMs, Releases, Distribution.

2. What is a Maven Arch-type? Answer: An arch-type is defined as an original model or pattern from which all other things of the same kind are made. In Maven, an arch-type is a template of a project which is combined with some user input to get the desired outcome.

3. What is the command to create a new project based on an archtype? Answer: mvn archetype:generate

4. What makes up an arch-type? Answer: Archtypes are packaged up in Jar file and they consist of the archtype metadata which describes the content of the archtype and a set of Velocity templates which make up a prototype project. 5. What is POM? Answer: A Project object Model or POM is the fundamental unit of work in Maven. It is an xml file that contains information about the project and configuration details used to build a project by Maven. It contains default value for most projects. Like, the build directory is „target‟.

6. Name the build, source and test source directory for POM in Maven. Answer: build=target, source=src/main/java,test=src/main/test.

7. How do you know the version of mvn you are using? Answer: mvn --version

8. Name the 3 build lifecycle of Maven Answer: default, clean and site.

9. Name default phases of a build lifecycle in Maven. Answer: validate, compile, test, package, integration test, verify, install deploy.

10. What would this command do “mvn clean dependency:copy-dependencies package” ? Answer: This command will clean the project, copy the dependencies and execute all phases up to and including project.

11. What is Maven? Answer: Maven is an attempt to apply patterns to a project‟s build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices. Maven is essentially a project management and comprehension tool.

12. Can Installation and user configuration be used to add shared project information? Answer: No. For this, you should have your projects inherit from a company-wide parent pom.xml

13. How many artifacts are downloaded at a time from different groups by default? Answer: 5. To only download single artifacts at a time, do mvn –Dmaven.artifact.thread=1 clean install To set this option permanently use MAVEN_OPTS environment variable to Dmaven.artifact.threads=3 14. How do you compile your source code in Maven? Answer: Change to the directory where pom.xml is created by archetype:create and execute the following command to compile your application sources. mvn compile

15. When you compile a Maven project, where do you find the class files? Answer: ${basedir}/target/classes/

16. What command should you use if you simply want to compile your test sources ( but not execute the tests? Answer: mvn test-compile

17. How do you compile my test sources and run my unit tests? Answer: mvn test

18. What is the Maven command to make a JAR File? Answer: mvn package. 19. How does Maven know to make JAR file when you do „mvn package‟ command? Answer: from the packaging element in pom.xml file. 20. What is the default location for your local repository? Answer: ~/m2./repository 21. What is the command to install JAR file in local repository? Answer: mvn install 22. What is the command to quickly build your Maven site? Answer: mvn site 23. What would ‘mvn clean’ do ? Answer: it will remove the target directory with all the build data before starting so that it is fresh. 24. What would the „jar:jar’ goal do? Answer: jar:jar will not recompile sources – it wills imply just create a JAR from the target/classes directory, under the assumption everything else had already been done. 25. How do you use plugin? Answer: Add element to pom.xml file.

STANDARD Directory layout src/main/java Application/Library sources src/main/resources Application/Library resources src/main/filters Resource filter files src/main/assembly Assembly descriptors src/main/config Configuration files src/main/webapp Web application sources src/test/java Test sources src/test/resources Test resources src/test/filters Test resource filter files src/site Site LICENSE.txt Project's license NOTICE.txt Notices and attributions required by libraries that the project depends on README.txt Project's readme

26. How can you override directory structure? Answer: via the project descriptor. 27. How can you add resources to your JAR file? Answer: place the resource (directory or files) under ${basedir}/src/main/resources The simple rule applied by Maven is this: any directories or files place within the ${basedir}/src/main/resources directory are packaged in the JAR with the exact same structure starting at the base of the JAR. 28. How you add resources to the classpath for your unit tests? Answer: To add resources to the classpath for your unit tests, you follow the same pattern as you do for adding resources to the JAR except the directory you place resources in is ${basedir}/src/test/resources. 29. How can you have Maven filter resources when copying? Answer set filtering to true for the resource directory in your pom.xml file. 30. What is the default value of filtering element in pom.xml file? Answer: false. 31. How do you reference a property defined in your pom.xml file? Answer: To reference a property defined in your pom.xml, the property name uses the names of the XML element that define the value, with “pom” being allowed as an alias for the project(root) element. So ${pom.name} refers to the name of the project, ${pom.version} refers to the version of the project, ${pom.build.finalName} refers to the final name of the file created when the built project is packaged, etc. The default values don‟t need to be explicitly defined to be available via referencing. Similarly, values in the user‟s settings.xml can be referenced using property names beginning with “setting”. 32. What does „mvn process-resources’ command do? Answer: it copies and filter resources and replace reference with actual value in properties file under resources directory.

33. How to reference a property defined in an external file? Answer: add a reference of that file in pom.xml and use 31.

34. What is the other alternative of using an external file? Answer: include the properties in your pom.xml under properties tag. Another alternative is to get values from system properties; either the system properties built into Java ( like

java.version or user.home) or poerties defined on the command line using the standard Java –D parameter. mvn process-resources "-Dcommand.line.prop=hello again" 35. How do you specify dependency? Answer; using the dependency tag in pom.xml

36. What are the things you need to define for each external dependency? Answer: groupID, artifactID,version, and scope (compile, test, runtime)

37. Where does Maven reference dependency from? Answer: Maven looks in your local repository (~/m2/repository).

38. How to search for a dependency (e.g. log4j)/ Answer: search for “site:www.ibiblio.org maven2 log4j” in google.

39. Run the command that will in a multi-module scenario where it will traverse into all the subprojects and run clean, then install. Answer: mvn clean install

40. What is a goal in building Maven? Answer: A goal represents a specific task ( finer than a build phase ) which contributes to the building and managing a project. It regulates how a build phase would carry out his duty.

41. What would this command do “ mvn clean dependency:copy-dependencies package” Answer: The clean phase will be executed first ( meaning it will run all preceeding phases of the clean lifecycle, plus the clean phase itself), and then the dependency:copy_dependencies goal, before finally executing the package phase ( and all its proceeding build phases of the default lifecycle).

42. What are the default values for packaing element? If there is no packaing element defined? What is the default value for that?

Answer: jar, war, ear, pom. JAR.

43. What is the value for packaging element in pom for a project that is purely meta-data? Answer: pom.

44. What must be done when adding plugins to make it usable? Answer: Add goals and specify the build phases the goals will be applied.

45. What is the use of execution element in pom file ? Answer: The execution element is there so that you can run the same goal multiple times with different configuration if needed. Separate execution can also be given an ID so that during inheritance or the application of profiles you can control whether goal configuration is merged or turned into an additional execution.

46. What are the minimum required elements for POM? Answer: project root, modelVersion, groupID, artifactID, version.

47. What is a project‟s fully qualified artifact name? Answer: ::

48. If you do not define any information, where does your pom inherits that information from? Answer: Super POM.

49. What are the elements in the POM that are merged? Answer: dependencies, developers and contributors, plugin lists ( including reports), plugin executions with matching ids, plugin configuration, resources. 50. how do you override the value of a parent POM ? Answer: do not include the element in child POM.

51. By Default parent pom.xml is one directory higher than that of the module‟s pom.xml . what if the parent is not yet installed and if the directory structure is different? Answer: we would have to add < relativePath> element to your parent section.

52. What is project Agreegation? Answer: Project Aggregation is similar to Project Inheritance. But instead of specifying the parent POM from the module, it specifies the modules from the parent POM. By doing so, the parent project now knows its modules, and if a maven command is invoked against the parent project, that maven command will then be executed to the parent‟s modules as well.

53. How can you do project aggregation? Answer: Change the parent POMs packaging to the value “pm” and specify in the parent POM the directories of its modules (child poms).

54. During project aggregation, how do you handle different directory structure? Answer: ../my-module

55. How can you achieve both Project Aggregation and Project Inheritance? Answer: Specify in every child POM who their parent POM is. Change the parent POMs packaging to POM. Specify in the parent POM the directories of its modules (children POM).

56. How maven processes variables? Answer: variables are preceded after inheritance as outlined above. This means that if a parent project uses a variable, then its definition in the child, not the parent, will be the one eventually used.

57. How can you customize the build timestamp fromat? Answer: by declaring the in proerpty element the maven.build.timestamp.format element.

58. How profiles are specified in Maven? Answer: Profiles are specified using a subset of the elements available in the POM itself.

59. What are the different types of profile in Maven? Answer: per project, per user, global, profile descriptor.

60. What is the option to specify a profile to be invoked? Answer: Profiles can be explicitly using the –P CLI option. mvn groupId:artifactId:goal –P profile-1,profile-2 No profile other than those specified in the option argument will be activated. 61. How can you activate a profile using Maven settings. Answer: profiles can be activated in the maven settings via the elements, each containing a profile-id inside.

62. How do you activate a profile when the system property “environment” is specified with value “prod”? Answer: mvn groupId:artifactId:goal –Denvironment=test 63. How can you activate a profile when a file is missing or existing? Answer: using the or tag in under /profiles/profile/activation xpath.

64. Can you interpolate properties and values defined in POM itself in settings.xml/profile.xml? Answer: No.

65. What special about using element? Answer: The profile will automatically be active for all builds unless another profile in the same POM is activated using settings.xml/profiles.xml All profiles that are active by default are automatically deactivated when a profile in the POM is activated on the command line or through its activation config.

66. How to deactivate a profile? Answer: using the command line by prefixing their identifier with either the character „!‟ or „-„ as shown below.

mvn groupId:artifactId:goal -P !profile-1,!profile-2

67. What are the elements in POM that a profile can modify when specified in the POM? Answer: , ,,,, ,,.

68. Why profile is used in Maven? Answer: to give portability to projects ( e.g. windows, linux etc).

69. What is the use of repositories in Maven? Answer: A repository in Maven is used to hold build artifacts and dependencies of varying types. There are two type of repositories: local and remote.

70. What are the benefit of storing JARS/external dependencies in local repository instead of remote one? Answer: It uses less sorage, it makes checking out project quicker, non need for versioning JAR files.

71. How can you download artifacts from a remote repository? Answer: Downloading in Maven is triggered by a project declaring a dependency that is not present in the local repository. By default, Maven will download from the central repository (http://repo1.maven.org/maven2/). To override this, you need to specify a mirror in settings.xml or pom.xml

72. How can you build your project offline? Answer: mvn –o package.

73. What is transitive dependency in Maven? Answer: Transitive dependency means to avoid needing to discover and specify the libraries that your own dependencies require, and including them automatically.

74. How Maven handles and determines what version of dependency will be used when multiple version of an artifact are encountered? Answer: Maven 2.0 supports only using the “nearest definition” which means that it will use the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it in POM. If two dependency version are at the same depth in the dependency tree, in Maven 2.0.9 it is the order in the declaration that counts. This is called dependency mediation.

75. How do you exclude dependency? Answer: using the exclusion element.

76. Name all the dependency scope. Answer: compile (default), provided, runtime, test, system, import.

77. What does dependency management mean in the context of transitive dependency? Answer: Dependency management mean to allow project authors to directly specify the versions of artifacts to be used when they are encountered in transitive dependencies or in dependencies where no version has been specified. A project can include a dependency in its dependencyManagement section and directly control which version is used.

78. What does import scope means in the context of dependency scope? Answer: This scope is only used on a dependency of type pom in the section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of dependency.

79. What is the minimal set of information for matching a dependency references against a dependencyManagement section ? Answer: {groupId,artifactId,type,classifier}.

80. What is a BOM “bill of materials”? Answer: The root of the project is the BOM pom. It defines the version so f all the artifacts that will be created in the library. Other projects that wish to use the library should import this pom into the dependencyManagement section of their pom. It is basically the parent of the parent.

81. Does Maven handle circularity ( circular reference) in terms of declaring a pom as a parent? Answer: No

82. What is a system dependency? Answer: 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.

83. What is the use of optional dependency? Answer: Optional dependencies are used when it is not really possible to split a project up into sub-modules. The idea is that some of the dependencies are only used for certain features in the project, and will not be needed if that feature isn‟t used. It is not only important to declare optional dependencies in order to save space/memory/etc. it is vital to control the list of actual dependencies a person needs in order to use aproject.

84. How do you use optional tag? Answer: setting true in your dependency declaration.

85. What is the difference between optional dependency and dependency exclusion? Answer: improvise

86. What does maven-clean-plugin do? Answer: it is responsible for removing the target directory of Maven 2 project. The clean plugin is used when you want to remove the files generated at build-time in a project‟s directory.

87. How can you run the clean plugin automatically during the build? Answer: you can put the clean plugin inside the execution tag in pom.xml file.

88. Can you delete files other than that in target directory using clean plugin? Answer: Yes. Using pom.xml file.

89. What is the minimal required information for a plugin to be configured? Answer: groupId, artifactId, version.

90. Where can you configure your plugins? Answer: in the configuration section in pom.xml

91. ******How do you see all the parameters for a given goal?***** Answer: Recent Maven plugins have generally an help goal to have in the command line the description of the plugin, with their parameters and types. To understand the javadoc goal, call: Mvn javadoc:help –Ddetail –Dgoal=javadoc.

92. How can you bind a mojo to a phase/goal? Answer: using the @goal,@phase annotation. It can be overridden by phase tag in pom.xml file under plugin configuration.

93. How to stop the propagation of plugins to child POMs? Answer: set to false.

94. If your plugin has a dependency and you want to use the latest version of that artifact, how can you avail this? Answer: using the dependency tag inside plugin.

95. What can be used to exclude all reports? Answer:

96. What is the convention for making plugin prefix? Answer: maven-$prefix-plug for plugins from the apache maven project. $prefix-maven-plugin for plugins from other sources.

97. When Maven determines plugin information? Answer: during build time.

98. Why is it illegal to specify a mojo using its plugin-prefix when dealing with lifecycle mappings? Answer: this helps Maven avoid the case where the „jar‟ lifecycle behaves differently on two user‟s machines, simply due to their local Maven configuration.

99. How do Maven resolves plugin‟s version? Answer: Project‟s POM, plugin registry, latest version metadata, release version metadata.

100. What is the packaging type of archtype? Answer: JAR.

101. What is the command to install 3rd party JARs in local repository? Answer: mvn install:install-file -Dfile= -DgroupId= \ -DartifactId= -Dversion= -Dpackaging=

102. Which plugin you would use in Maven to call your class? Answer: exec:java

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF