Maven / Maven Interview questions
types are,
- local.
- central.
- and remote.
It is the Maven established repository. For example, your POM specify the dependencies and it is not available in the configured local and the remote repository then maven looks for the resource in Maven Central. Maven provides most of the generic dependency resources at this remote location.
This goal when executed copies all the project's dependencies along with the transitive dependencies to the specified folder.
1) Rename the project using Eclipse or other IDE.
2) Update the artifactId in your pom.xml
Yes. You could mention file name using the -f option.
mvn -f parent-pom.xml
The minimum requirement for a POM are the following,
project root
modelVersion
groupId
artifactId
version
Test directory is src/main/test that has all the unit testing codes.
It creates the jar files from the target/classes directory without recompiling any source classes.
target folder holds the compiled unit of code as part of the build process.
Source folder usually src/main/java holds java source codes.
Test directory is src/main/test that has all the unit testing codes.
An artifact is a JAR, that gets deployed to a Maven repository.
Each artifact has a group ID , an artifact ID (artifact name) and a version string.
Apache Maven is a software project management and comprehension tool. A build automation tool that helps managing the software build lifecycle.
There are 6 build phases.
Validate.
Compile.
Test.
Package.
Install.
Deploy.
Maven plain Old Java Object. Each mojo is an executable goal in Maven.
Include the parameter -Dmaven.test.skip=true or -DskipTests=true in the command line.
Use the parameter -Dtest=MyTestClassName at the command line.
plugin is a distribution of one or more related mojos.
POM (Project Object Model) is the fundamental unit of work. It is an XML file which holds the information about the project and configuration details used to build a project by Maven along with its dependencies.
The
Compile
default scope;it indicates what dependency is available in the classpath of the project.
Provided
indicates that the dependency is provided by JDK or web server or container at runtime.
Runtime
the dependency is not needed for compilation but is required during execution.
Test
indicates that the dependency is not required for normal usage of the application however it is only available for the test compilation and execution phases.
system
You need to provide the JAR which contains it explicitly.
import
Only used on the dependency of type pom in the . indicates that the specified POM gets replaced or imported with the dependencies in that POM.
Three properties group ID, artifact ID and the version string together identifies the artifact.
The location where all the project jars, library jars, plugins or any other project related artifacts that are stored and can be easily used by Maven.
• Create a jar file
• Create war file
• Compile code
• Unit testing of code
• Documenting projects
• Reporting
Under the folder ${basedir}/target/classes/.
All models implicitly inherit from a super-POM. All Maven project POMs extend the Super POM, which defines a set of default configurarion shared by all projects.
This feature eliminates necessity to discover and specify the libraries that your own dependencies require and includes them automatically.
1. parent pom
2. project pom
3. settings
4. CLI parameters
mvn --version
compile
compiles the source code of the project whereas install
installs the package into the local repository, for use as a dependency in other projects locally.
using command,
mvn dependency:tree
<groupId>:<artifactId>:<version>
Local -> Remote -> Maven Central
Identifies a project uniquely across all projects.
it becomes the name of the jar file.
A snapshot version in Maven is not a real version and that will not been released. The same version may get many updates. Usually, snapshot dependencies should only exist during development and no released version (non-snapshot) should have a dependency on a snapshot version.
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 packaging for your project can be specified via the POM element <packaging>.
Some of the valid packaging values are jar, war, ear and pom. If no packaging value has been specified, it will default to jar.
<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"> ... <packaging>war</packaging> ... </project>
Maven searches first for a dependency JAR in local repository. If found it is used else maven looks st the remote repository and download the corresponding version of JAR file and then stores it into local repository.
Maven's features and plugins are initialized with default conventions and the basic functionality of Maven requires minimum or no configuration.
Maven uses Surefire Plugin to execute unit tests. By default, the Surefire Plugin will automatically include all test classes with the wildcard patterns containing "Test".
Run tests using mvn test
command.
During development, you may run a single test class repeatedly. To run this through Maven, set the test property to a specific test case. Run the mvn test
command with test property set to name of the test class you want to run.
mvn -Dtest=TestClass test
junit 4.x provides support for running specific test methods. You must use the following syntax by specifying the test class and method name. This also accepts patterns for method names.
mvn -Dtest=TestClass#mytest test
you may also select multiple methods using the below syntax.
mvn -Dtest=TestClass#testMOne+testMTwo test
Gradle uses domain-specific language based on the programming language Groovy, while Apache Maven uses XML for its project configuration.
Gradle is based on a graph of task dependencies while Maven is based on a fixed and linear model of phases.
Gradle allows incremental builds because it checks which tasks are updated or not.
Use the below command to install external dependency into the maven repository.
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \ -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
Dependency management is simple. Maven automatically takes care of the transitive dependency for your direct dependencies.
Easy Source management. Maven does not require dependencies to be tracked in source control that makes the project simple and easy to maintain.
Powerful builds and easy build management.
Better colaboration.
Learning Maven consumes time.
Remote repositories are not safe if it is not maintained propertly.
Integration required if the project's dependent jar is not mavenized.