FAQs
Frequently Asked Question on Maven
How do we rename a maven project?
1) Rename the project using Eclipse or other IDE.
2) Update the artifactId in our pom.xml
Can we use different name for POM.xml?
Yes. We could mention file name using the -f option.
mvn -f parent-pom.xml
What are the minimum requirements for a POM?
At a bare minimum, the POM must include the following elements:
Project Root: The root element of the POM file
Model Version (
modelVersion
): Specifies the model version for the POM. The current model version is4.0.0
.Group ID (
groupId
): Specifies the group that the project belongs to. This is typically a reversed domain name (e.g.,com.example
).Artifact ID (
artifactId
): The unique identifier for the project within the group.Version (
version
): Specifies the version of the project.
Example of a minimal pom.xml
file:
pom.xml
file:Explanation of Each Element
<project>
: The root element of the POM file.xmlns
andxsi:schemaLocation
: These attributes define the XML namespace and schema location for the POM.<modelVersion>
: Indicates the version of the POM model, which should be4.0.0
.<groupId>
: A unique identifier for the project’s group. This is typically based on the project's package structure or the domain of the organization.<artifactId>
: The name of the project. This ID must be unique within the group.<version>
: The version of the project. This can follow semantic versioning (e.g.,1.0.0
,1.0.1
,1.1.0
, etc.).
What is Maven artifact?
An artifact is a JAR (for example), that gets deployed to a Maven repository. Each artifact has a group ID , an artifact ID (artifact name) and a version string.
What is MOJO?
Maven plain Old Java Object. Each mojo is an executable goal in Maven.
How do I skip the tests?
Include the parameter -Dmaven.test.skip=true or -DskipTests=true in the
How can I run a single unit test?
Use the parameter -Dtest=MyTestClassName at the command line.
What is Maven plugin?
Plugin is a distribution of one or more related mojos.
What makes a fully qualified name for the artifact?
Three properties group ID, artifact ID and the version string together identifies the artifact.
Where do we find .class files of a Maven project?
Under the folder ${basedir}/target/classes/.
What is Super POM?
The Super POM is the ultimate parent POM for all Maven projects. It defines default configurations and settings that apply to every Maven project, providing a set of shared, base configurations. When we create a Maven project, our project's pom.xml
implicitly inherits from this Super POM unless we explicitly specify a different parent.
Location
The Super POM is provided by the Maven installation and is located in the maven-model-builder
library.
Key Aspects
Inheritance: Every
pom.xml
file in Maven implicitly inherits from the Super POM.Defaults: It defines default behaviors, properties, and configurations that can be overridden by your project's
pom.xml
.Extensibility: By extending the Super POM, Maven projects benefit from standardized build processes and settings.
Example of the Super POM
Here is a simplified version of what the Super POM contains:
Default Settings from the Super POM
Here are some of the default configurations provided by the Super POM:
Repositories:
The central repository (
https://repo.maven.apache.org/maven2
) is defined as the default repository.
Plugin Repositories:
The central plugin repository is also defined.
Build Plugins:
Common build plugins and their versions are specified, such as
maven-clean-plugin
,maven-compiler-plugin
, andmaven-surefire-plugin
.
Dependency Management:
Default dependencies and their versions are managed.
Overriding the Super POM
We can override or extend the settings provided by the Super POM in our project's pom.xml
. For example:
How to View the Super POM
You can view the Super POM by running the following Maven command:
This command generates the effective POM for our project, which includes the merged configurations from your pom.xml
and the Super POM.
Add a New POM as Parent
To add a new POM as a parent to our Maven project, we need to specify the parent POM in our project's pom.xml
file. This is done using the <parent>
element. The parent POM can provide shared configurations, dependencies, and plugins for all child projects.
Steps to Add a New POM as Parent
Define the Parent POM: Create a parent POM with the common configurations that you want to share across multiple projects.
Add the Parent POM to the Child Project: In your child project's
pom.xml
, specify the parent POM using the<parent>
element.
How It Works
Inheritance: The child project inherits the configurations from the parent POM, including dependency management, plugins, properties, and other settings.
Override: The child project can override any configuration from the parent POM by redefining it in its own
pom.xml
.Dependency Management: The child project will use the versions of dependencies specified in the parent POM's
<dependencyManagement>
section unless overridden in the child POM.
Benefits
Centralized Configuration: Common configurations are centralized in the parent POM, making it easier to manage and update them.
Consistency: Ensures consistency across multiple projects by sharing the same configurations.
Reduced Redundancy: Reduces redundancy in configuration by avoiding duplication of common settings in each child project.
Difference between compile and install.
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.
How do I specify packaging/distributable format in Maven?
The packaging for our 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.
What is Transitive Dependency?
In Maven, a transitive dependency is an indirect dependency that a project inherits from its direct dependencies. This means if Project A depends on Project B, and Project B depends on Project C, then Project A will also have Project C as a dependency. Transitive dependencies allow for automatic inclusion of necessary dependencies without explicitly specifying them in the pom.xml
file.
Example
Project A pom.xml
:
Project B pom.xml
:
In this case, Project A will have both Project B and Project C as dependencies.
Difference Between maven compile
and maven install
maven compile
and maven install
mvn compile
:Purpose: Compiles the source code of the project.
Lifecycle Phase: Executes up to the
compile
phase.Outcome: Generates
.class
files in thetarget/classes
directory.Usage: Used when you only need to compile the code and not perform further actions like testing or packaging.
mvn install
:Purpose: Installs the package into the local repository, for use as a dependency in other projects locally.
Lifecycle Phase: Executes all phases in the default lifecycle up to
install
(includes compile, test, package, etc.).Outcome: Installs the resulting artifact (e.g., JAR, WAR) into the local Maven repository (
~/.m2/repository
).Usage: Used when you want to build the project and make it available for other local projects to use as a dependency
What is Maven Snapshot ?
A snapshot in Maven represents a version of the project that is currently in development and may change. Snapshots are used to indicate that the version is not stable and is still under active development.
Snapshot Versioning: Snapshot versions are denoted with
-SNAPSHOT
at the end of the version number (e.g.,1.0.0-SNAPSHOT
).Updating: Maven checks for updates to snapshot dependencies on each build, ensuring the latest development version is used.
Storage: Snapshots are stored in a special directory in the repository to keep track of different builds.
Example
Snapshot Dependency in pom.xml
:
What is Maven's order of inheritance?
Parent POM:
Description: The parent POM is the first source of inherited configurations. If our project specifies a parent POM using the
<parent>
element, all configurations, properties, dependency management, plugins, and plugin management defined in the parent POM are inherited by the child POM.Inheritance: Elements from the parent POM are inherited first and can be overridden by the child POM.
Example:
Project POM:
Description: The project POM (
pom.xml
) is the main configuration file for our Maven project. It contains the project's own configurations, dependencies, properties, and build settings.Precedence: Configurations in the project POM override those inherited from the parent POM.
Example:
Settings:
Description: The settings file (
settings.xml
) contains user-specific configurations for Maven, including repository locations, proxies, and user credentials. There are twosettings.xml
files: one global (located inMAVEN_HOME/conf
) and one user-specific (located in~/.m2
).Precedence: Settings in
settings.xml
can override configurations in the project POM, particularly for repository settings and user-specific configurations.Example (
~/.m2/settings.xml
):
CLI Parameters:
Description: Command Line Interface (CLI) parameters provided when invoking Maven commands. These include system properties, profiles, and other command-line options.
Precedence: CLI parameters have the highest precedence and can override configurations specified in both the settings file and the project POM. They are useful for temporary configurations and for passing arguments dynamically.
Example:
Order of Precedence
CLI Parameters: Highest precedence, overrides all other configurations.
Settings: User-specific configurations that override project POM settings.
Project POM: Main project configurations that override parent POM settings.
Parent POM: Base configurations inherited by the project.
How Maven uses Convention over Configuration?
Maven's features and plugins are initialized with default conventions and the basic functionality of Maven requires minimum or no configuration.
How do I install a 3rd party jar (for example) into the Maven repository?
To install a third-party JAR into your local Maven repository, you can use the mvn install:install-file
command. This command allows you to specify the file path of the JAR and the necessary Maven coordinates such as groupId
, artifactId
, version
, and packaging
. Here is a step-by-step guide on how to do this:
Step-by-Step Guide
Prepare the JAR File: Make sure you have the JAR file you want to install locally on your system. Note the path to this file.
Run the Maven Command: Use the following command to install the JAR into your local Maven repository:
-Dfile
: Path to the JAR file you want to install.-DgroupId
: The group ID you want to assign to the JAR.-DartifactId
: The artifact ID you want to assign to the JAR.-Dversion
: The version number you want to assign to the JAR.-Dpackaging
: The packaging type (usuallyjar
).
Example
Let's assume you have a JAR file located at /path/to/external-library-1.0.0.jar
and you want to install it with the following Maven coordinates:
Group ID:
com.example
Artifact ID:
external-library
Version:
1.0.0
Packaging:
jar
The command would be:
Using a POM File (Optional)
If you have a POM file for the JAR, you can include it using the -DpomFile
parameter:
Verifying the Installation
After running the command, the JAR file should be installed in your local Maven repository, typically located at ~/.m2/repository/com/example/external-library/1.0.0/
.
You can verify the installation by checking if the following files are present in the directory:
Adding the Dependency to Your Project
Once the JAR is installed in your local Maven repository, you can add it as a dependency in your project's pom.xml
:
List out the dependency scope in Maven?
Compile: It is the default scope, and it indicates what dependency is available in the classpath of the project Provided: It indicates that the dependency is provided by JDK or web server or container at runtime Runtime: This tells that the dependency is not needed for compilation but is required during execution Test: It says dependency is available only for the test compilation and execution phases System: It indicates you have to provide the system path Import: This indicates that the identified or specified POM should be replaced with the dependencies in that POM’s section
List out the build phases in Maven?
Validate: validate the project is correct and all necessary information is offered. Compile: compile the source code of the project. Test: test the compiled source code employing an appropriate unit testing framework and these tests should not require the code deployed or packaged. Package: take the compiled code and package it in its distributable format like a JAR. Integration-Test: process and deploy the package if needed to run integration tests Verify: run any tests to verify the package is still valid and meets quality requirements. Install: install the package into the native repository, to be used as a dependency in alternative projects regionally. Deploy: copies the final package to the remote repository for sharing with alternative projects and developers.
How to run a Spring Boot application directly from the command line without any packaging file (e.g. JAR file)?
The mvn spring-boot:run
command is used in Maven to run a Spring Boot application directly from the command line without the need to package it as a JAR file first. This is particularly useful for development, as it allows to quickly start up the application and see changes in real-time. We can pass additional properties or profiles as parameters.
Last updated
Was this helpful?