{"id":1720,"date":"2022-11-10T07:16:58","date_gmt":"2022-11-10T07:16:58","guid":{"rendered":"https:\/\/spacican.com\/notes\/?p=1720"},"modified":"2024-08-10T06:50:48","modified_gmt":"2024-08-10T06:50:48","slug":"maven-notes","status":"publish","type":"post","link":"https:\/\/spacican.com\/notes\/maven-notes\/","title":{"rendered":"Maven Notes"},"content":{"rendered":"\n<p>Maven is a build and dependency management tool for Java applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Maven Terminologies<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Project Object Model (pom.xml) file<\/h3>\n\n\n\n<ul>\n<li>Contains all of the details about the current project including, configurations, plugins, dependencies, goals. It also describes build process of the project.<\/li>\n\n\n\n<li>pom.xml file also describes the co-ordinate of the project (<a href=\"https:\/\/maven.apache.org\/repositories\/artifacts.html\" data-type=\"link\" data-id=\"https:\/\/maven.apache.org\/repositories\/artifacts.html\">artifact<\/a>) so that it can be addressed remotely by maven.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Repositories<\/h3>\n\n\n\n<ul>\n<li>Stores build artifacts and dependencies for Maven projects, there are two kind of repositories:\n<ul>\n<li>Local Repository: Local repository resides inside user&#8217;s computer (inside maven&#8217;s installed directory). Maven saves dependencies and build artifacts in local repository as cache so that those can be used in other local maven projects.<\/li>\n\n\n\n<li>Remote repository: These repositories are present remotely. &nbsp;repo.maven.apache.org is one of the remote maven repository.\n<ul>\n<li>By default, Maven uses its central repository (located at <em>https:\/\/repo.maven.apache.org\/maven2<\/em>) to serve the dependencies. <\/li>\n\n\n\n<li>An alternative remote repository can be used by maven by specifying repository details in the <code>&lt;repositories&gt;<\/code> tag in pom.xml file<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Build Lifecycle<\/h3>\n\n\n\n<ul>\n<li>Build lifecycles are series of <strong>phases<\/strong> which help building the project. Maven provide three built-in build lifecycles: <code>default<\/code>, <code>clean<\/code> and <code>site<\/code> <\/li>\n\n\n\n<li>As an example, the <code>default<\/code> lifecycle comprise of following phases: (<a href=\"https:\/\/maven.apache.org\/guides\/introduction\/introduction-to-the-lifecycle.html\" data-type=\"link\" data-id=\"https:\/\/maven.apache.org\/guides\/introduction\/introduction-to-the-lifecycle.html\">source<\/a>)\n<ul>\n<li><strong>validate<\/strong> &#8211; validate the project is correct and all necessary information is available<\/li>\n\n\n\n<li><strong>compile<\/strong> &#8211; compile the source code of the project<\/li>\n\n\n\n<li><strong>test<\/strong> &#8211; test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed<\/li>\n\n\n\n<li><strong>package<\/strong> &#8211; take the compiled code and package it in its distributable format, such as a JAR.<\/li>\n\n\n\n<li><strong>verify<\/strong> &#8211; run any checks on results of integration tests to ensure quality criteria are met<\/li>\n\n\n\n<li><strong>install<\/strong> &#8211; install the package into the local repository, for use as a dependency in other projects locally<\/li>\n\n\n\n<li><strong>deploy<\/strong> &#8211; done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Any of the phase mentioned above can be directly executed through command line using <code>mvn [phase-name]<\/code><\/li>\n\n\n\n<li>An another commonly used phase is <code>clean<\/code> phase from the <code>clean<\/code> lifecycle. calling <code>mvn clean<\/code> executes the <code>pre-clean<\/code> and <code>clean<\/code> phase. which results in cleaning the files that were generated by previous build.<\/li>\n\n\n\n<li>A full list of build-in maven phases can be seen <a href=\"https:\/\/maven.apache.org\/guides\/introduction\/introduction-to-the-lifecycle.html#Lifecycle_Reference\" data-type=\"link\" data-id=\"https:\/\/maven.apache.org\/guides\/introduction\/introduction-to-the-lifecycle.html#Lifecycle_Reference\">here<\/a>.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-create-block-note sz-note yellow\">\n<p><strong>Note: executing a phase executes all of the previous phases of that build lifecycle automatically.<\/strong><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-create-block-note sz-note\">\n<p>Following is an example image where <code>install<\/code> phase (of <code>default<\/code> lifecycle) is called using command line<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"747\" height=\"238\" src=\"https:\/\/spacican.com\/notes\/wp-content\/uploads\/2024\/06\/image-1.png\" alt=\"maven install command line\" class=\"wp-image-1750\"\/><\/figure>\n<\/div>\n\n\n\n<ul>\n<li><strong><mark>Goals and Plugins<\/mark><\/strong>\n<ul>\n<li>Goals and Plugins are fundamental part of Maven&#8217;s core framework.<\/li>\n\n\n\n<li><strong><mark>Goals<\/mark><\/strong> are specific tasks\/actions which help in building process. Goals are bound to phases of build lifecycle with the help of POM file configurations (or build-in configurations)<\/li>\n\n\n\n<li>A <strong><mark>Plugin<\/mark><\/strong> is a group of goals. As an example the <code>Compiler Plugin<\/code> compiles the source code. The compiler plugin has two goals:&nbsp;<code>compile<\/code>&nbsp;to compile main source files, and&nbsp;<code>testCompile<\/code>&nbsp;to compile test source files.<\/li>\n\n\n\n<li>Note that individual goals of same plugin can be bound to different phases.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Maven CLI structure<\/h2>\n\n\n\n<p><code>mvn [options] [&lt;goal(s)&gt;] [&lt;phase(s)&gt;]<\/code><\/p>\n\n\n\n<p>example:<\/p>\n\n\n\n<ul>\n<li>Execute <strong>install<\/strong> phase of default life-cycle:\n<ul>\n<li><code>mvn install<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Execute <strong>install<\/strong> phase using myProfile profile defined in pom.xml\n<ul>\n<li><code>mvn -P myProfile install<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Execute <strong>generate<\/strong> goal of <strong>archetype<\/strong> plugin:\n<ul>\n<li><code>mvn archetype:generate<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Maven File Structure<\/h2>\n\n\n\n<p>Following are most commonly used directories in maven project. Full list of directories\/files can be seen <a href=\"https:\/\/maven.apache.org\/guides\/introduction\/introduction-to-the-standard-directory-layout.html\" data-type=\"link\" data-id=\"https:\/\/maven.apache.org\/guides\/introduction\/introduction-to-the-standard-directory-layout.html\">here<\/a>.<\/p>\n\n\n\n<ul>\n<li><mark><strong>src<\/strong><\/mark>: Stores java source\n<ul>\n<li><strong><mark>main<\/mark><\/strong>\n<ul>\n<li><strong><mark>java<\/mark><\/strong>: Store java application source code\n<ul>\n<li><strong><mark>package<\/mark><\/strong>: root package<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><mark>resources<\/mark><\/strong>: Stores resources used by application. (ex: configuration\/properties files used by application)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><mark>test<\/mark><\/strong>\n<ul>\n<li><strong><mark>java<\/mark><\/strong>: Store source code used during test phase<\/li>\n\n\n\n<li><strong><mark>resources<\/mark><\/strong>: Store resources used during test phase<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><mark>target<\/mark><\/strong>: This is an output folder. It contains all of the built output files.<\/li>\n\n\n\n<li><strong><mark>pom.xml<\/mark><\/strong>: Contains all of the information and configurations of maven project.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-create-block-note sz-note\">\n<p>If a non-default structure of files and directories has to be used, information about the structure should be provided in the <code>&lt;build&gt;<\/code> tag of <code>pom.xml<\/code> file<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007700\">&lt;build&gt;<\/span>\n    <span style=\"color: #007700\">&lt;sourceDirectory&gt;<\/span>mySrc<span style=\"color: #007700\">&lt;\/sourceDirectory&gt;<\/span>\n    <span style=\"color: #007700\">&lt;directory&gt;<\/span>myTarget<span style=\"color: #007700\">&lt;\/directory&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/build&gt;<\/span>\n<\/pre><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">POM inheritance<\/h2>\n\n\n\n<p>Much like java class inheritance, POM inheritance mostly follows similar concepts.<\/p>\n\n\n\n<ul>\n<li>A POM file can inherit another POM file, inheriting all of the properties\/configurations defined in the parent POM file to the children POM file.<\/li>\n\n\n\n<li>All POM files (including the root POM file) inherit a super POM file. The super POM file is present in Maven&#8217;s installation directory. All of the default configurations (e.g.. The file structure, default remote repository, default package type etc.) are present in that super POM file.<\/li>\n\n\n\n<li><code>mvn help:effective-pom<\/code> command can be used to see the final interpreted pom resulting by all of the inherited configurations from parent (including global) pom files. <\/li>\n\n\n\n<li>Any configuration specified in child pom file overrides same configuration specified in parent pom file.<\/li>\n\n\n\n<li>To make a pom file inherit from parent pom file, specify <code>artifactId<\/code>, <code>groupId<\/code>, <code>version<\/code> inside the <code>&lt;parent&gt;<\/code> tag. Note that the parent pom file should be present in the maven repository before using its coordinates to inherit in child pom.xml<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007700\">&lt;parent&gt;<\/span>\n    <span style=\"color: #007700\">&lt;groupId&gt;<\/span>com.khansajid<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n    <span style=\"color: #007700\">&lt;artifactId&gt;<\/span>maven-examples-proj<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n    <span style=\"color: #007700\">&lt;version&gt;<\/span>1.0<span style=\"color: #007700\">&lt;\/version&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/parent&gt;<\/span>\n<\/pre><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Profiles<\/h2>\n\n\n\n<p>Maven allows creation of different profiles within the pom xml file. A profile is a group of elements\/configurations which override the default elements\/configurations defined in pom file. A profile contains <code>&lt;id&gt;<\/code> tag for identification of the profile. A pom file can contain multiple profiles.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\">    <span style=\"color: #007700\">&lt;profiles&gt;<\/span>\n        <span style=\"color: #888888\">&lt;!-- development profile --&gt;<\/span>\n        <span style=\"color: #007700\">&lt;profile&gt;<\/span>\n            <span style=\"color: #007700\">&lt;id&gt;<\/span>development<span style=\"color: #007700\">&lt;\/id&gt;<\/span>\n            <span style=\"color: #007700\">&lt;build&gt;<\/span>\n                <span style=\"color: #888888\">&lt;!-- save the built project to targetDev folder --&gt;<\/span>\n                <span style=\"color: #007700\">&lt;directory&gt;<\/span>targetDev<span style=\"color: #007700\">&lt;\/directory&gt;<\/span>\n            <span style=\"color: #007700\">&lt;\/build&gt;<\/span>\n        <span style=\"color: #007700\">&lt;\/profile&gt;<\/span>\n        \n        <span style=\"color: #888888\">&lt;!-- production profile --&gt;<\/span>\n        <span style=\"color: #007700\">&lt;profile&gt;<\/span>\n            <span style=\"color: #007700\">&lt;id&gt;<\/span>production<span style=\"color: #007700\">&lt;\/id&gt;<\/span>\n            <span style=\"color: #007700\">&lt;build&gt;<\/span>\n                <span style=\"color: #888888\">&lt;!-- save the built project to targetProd folder --&gt;<\/span>\n                <span style=\"color: #007700\">&lt;directory&gt;<\/span>targetProd<span style=\"color: #007700\">&lt;\/directory&gt;<\/span>\n            <span style=\"color: #007700\">&lt;\/build&gt;<\/span>\n        <span style=\"color: #007700\">&lt;\/profile&gt;<\/span>\n    <span style=\"color: #007700\">&lt;\/profiles&gt;<\/span>\n<\/pre><\/div>\n\n\n\n<p>To run a maven phase\/goal with configuration defined in a specific profile, use <code>mvn -P [profile-id (comma delimited for multiple profiles)] [phase-name\/goal]<\/code><\/p>\n\n\n\n<p>To automatically use a specific profile based on some defined environment property (or based on some other defined means), the <code>&lt;activation&gt;<\/code> tag is used. Whenever the condition defined in <code>&lt;activation&gt;<\/code> tag is matched, that profile gets activated and therefore is used by maven to run phase\/goals. Multiple profiles can be activated during a phase based on the matched conditions.<\/p>\n\n\n\n<p>As an example, in following pom file, the <code>development<\/code> profile is activated\/used whenever the <code>USE_PROFILE<\/code> environment variable is set to <code>DEV<\/code> in the system<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007700\">&lt;profile&gt;<\/span>\n    <span style=\"color: #007700\">&lt;id&gt;<\/span>development<span style=\"color: #007700\">&lt;\/id&gt;<\/span>\n    <span style=\"color: #007700\">&lt;activation&gt;<\/span>\n        <span style=\"color: #007700\">&lt;property&gt;<\/span>\n            <span style=\"color: #007700\">&lt;name&gt;<\/span>env.USE_PROFILE<span style=\"color: #007700\">&lt;\/name&gt;<\/span>\n            <span style=\"color: #007700\">&lt;value&gt;<\/span>DEV<span style=\"color: #007700\">&lt;\/value&gt;<\/span>\n        <span style=\"color: #007700\">&lt;\/property&gt;<\/span>\n    <span style=\"color: #007700\">&lt;\/activation&gt;<\/span>\n    <span style=\"color: #007700\">&lt;build&gt;<\/span>\n        <span style=\"color: #007700\">&lt;directory&gt;<\/span>targetDev<span style=\"color: #007700\">&lt;\/directory&gt;<\/span>\n    <span style=\"color: #007700\">&lt;\/build&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/profile&gt;<\/span>\n<\/pre><\/div>\n\n\n\n<p>Other available conditions that can be used in <code>&lt;activation&gt;<\/code> tag are:<\/p>\n\n\n\n<ul>\n<li><code>&lt;activeByDefault&gt;<\/code> : If used, the profile is activated\/used by default.<\/li>\n\n\n\n<li><code>&lt;property&gt;<\/code> : Activate\/use the profile based on value of specified system variable.<\/li>\n\n\n\n<li><code>&lt;file&gt;<\/code>: Activate\/use the profile based on presence of specified file.<\/li>\n\n\n\n<li><code>&lt;jdk&gt;<\/code>: Activate\/use the profile based on jdk version.<\/li>\n\n\n\n<li><code>&lt;os&gt;<\/code>: Activate\/use the profile based on operating system.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Dependencies<\/h2>\n\n\n\n<p>Coordinates of dependency libraries can be specified in pom.xml inside <code>&lt;dependencies&gt;<\/code> tag. The Maven fetches the dependencies from remote repository if those are not already present in the local repository. <strong>Transient dependencies<\/strong> (dependencies of dependencies) are also automatically downloaded by Maven.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007700\">&lt;dependency&gt;<\/span>\n    <span style=\"color: #007700\">&lt;groupId&gt;<\/span>org.slf4j<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n    <span style=\"color: #007700\">&lt;artifactId&gt;<\/span>slf4j-api<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n    <span style=\"color: #007700\">&lt;version&gt;<\/span>2.0.13<span style=\"color: #007700\">&lt;\/version&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/dependency&gt;<\/span>\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Dependency Scope<\/h3>\n\n\n\n<p> Scope of a dependency is specified using &lt;scope&gt; tag. It is used to determine when a dependency should be included in class path. Some important dependency scopes are:<\/p>\n\n\n\n<ul>\n<li><strong>compile<\/strong>: This scope is used by default when not specified. This scope ensures that the dependency is included in every class path. This scope tells maven that the dependency is required to build, test as well as run the project.<\/li>\n\n\n\n<li><strong>test<\/strong>: This scope tells maven that the dependency is required only for testing of the project.<\/li>\n\n\n\n<li><strong>provided<\/strong>: This scope indicates that the dependency is expected to be provided by the container or system environment during runtime. This means Maven won&#8217;t include the JAR file containing the dependency&#8217;s classes in the project&#8217;s final build<\/li>\n<\/ul>\n\n\n\n<p>Example: In following pom.xml file, <code>junit<\/code> dependency is included using <code>test<\/code> scope<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007700\">&lt;dependency&gt;<\/span>\n    <span style=\"color: #007700\">&lt;groupId&gt;<\/span>junit<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n    <span style=\"color: #007700\">&lt;artifactId&gt;<\/span>junit<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n    <span style=\"color: #007700\">&lt;version&gt;<\/span>4.13.2<span style=\"color: #007700\">&lt;\/version&gt;<\/span>\n    <span style=\"color: #007700\">&lt;scope&gt;<\/span>test<span style=\"color: #007700\">&lt;\/scope&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/dependency&gt;<\/span>\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Optional Dependencies and Dependency Exclusion<\/h3>\n\n\n\n<p>Explained with the help of following project structure:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\">Project<span style=\"color: #333333\">-<\/span>A\n   <span style=\"color: #333333\">-&gt;<\/span> Project<span style=\"color: #333333\">-<\/span>B\n        <span style=\"color: #333333\">-&gt;<\/span> Project<span style=\"color: #333333\">-<\/span>D\n              <span style=\"color: #333333\">-&gt;<\/span> Project<span style=\"color: #333333\">-<\/span>E\n              <span style=\"color: #333333\">-&gt;<\/span> Project<span style=\"color: #333333\">-<\/span>F\n   <span style=\"color: #333333\">-&gt;<\/span> Project C\n<\/pre><\/div>\n\n\n\n<p>In above project structure, the project A has dependency on Project B and C (All dependencies defined with the help of POM.XML). Project B depends on D, and then D further depends on Project E and F.<\/p>\n\n\n\n<p>Suppose, Project D adds some minor features to project B, however project B can work even if Project D is not present.<\/p>\n\n\n\n<p>Let&#8217;s say That project A do not require the minor feature provided by Project D, however since project B has defined dependency on Project D, Maven will download the Project D (and all it&#8217;s sub-dependencies) even for project A.<\/p>\n\n\n\n<p>This can be avoided in two ways:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Optional Dependency <\/h4>\n\n\n\n<p>One way is to specify Project D as an optional dependency in Project B&#8217;s POM.XML file.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">&lt;!-- Project_B\/pom.xml --&gt;<\/span>\n<span style=\"color: #007700\">&lt;dependencies&gt;<\/span>\n  <span style=\"color: #007700\">&lt;dependency&gt;<\/span>\n    <span style=\"color: #007700\">&lt;groupId&gt;<\/span>Project_D<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n    <span style=\"color: #007700\">&lt;artifactId&gt;<\/span>Project-D<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n    <span style=\"color: #007700\">&lt;optional&gt;<\/span>true<span style=\"color: #007700\">&lt;\/optional&gt;<\/span>\n  <span style=\"color: #007700\">&lt;\/dependency&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/dependencies&gt;<\/span>\n<\/pre><\/div>\n\n\n\n<p>In That way, all of the projects which have dependency on Project B, will download Project D <strong>only if<\/strong> the dependency is included explicitly.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">&lt;!-- Project_A\/pom.xml --&gt;<\/span>\n<span style=\"color: #007700\">&lt;dependencies&gt;<\/span>\n  \n  <span style=\"color: #007700\">&lt;dependency&gt;<\/span>\n    <span style=\"color: #007700\">&lt;groupId&gt;<\/span>Project_B<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span> <span style=\"color: #888888\">&lt;!-- Including B alone would not alo download D --&gt;<\/span>\n    <span style=\"color: #007700\">&lt;artifactId&gt;<\/span>Project-B<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n  <span style=\"color: #007700\">&lt;\/dependency&gt;<\/span>\n  \n  <span style=\"color: #007700\">&lt;dependency&gt;<\/span>\n    <span style=\"color: #007700\">&lt;groupId&gt;<\/span>Project_D<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span> <span style=\"color: #888888\">&lt;!-- Have to include D explicitly --&gt;<\/span>\n    <span style=\"color: #007700\">&lt;artifactId&gt;<\/span>Project-D<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n  <span style=\"color: #007700\">&lt;\/dependency&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/dependencies&gt;<\/span>\n<\/pre><\/div>\n\n\n\n\n<h4 class=\"wp-block-heading\">Dependency Exclusion<\/h4>\n\n\n\n<p>Let&#8217;s say, the optional tag was not present on Project D in the Project B&#8217;s pom.xml, in that case, Project A can still exclude Project D from getting downloaded by specifying it in <code>exclusion<\/code> tag:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">&lt;!-- Project_A\/pom.xml --&gt;<\/span>\n<span style=\"color: #007700\">&lt;dependencies&gt;<\/span>\n  \n  <span style=\"color: #007700\">&lt;dependency&gt;<\/span>\n    <span style=\"color: #007700\">&lt;groupId&gt;<\/span>Project_B<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n    <span style=\"color: #007700\">&lt;artifactId&gt;<\/span>Project-B<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n    <span style=\"color: #007700\">&lt;exclusions&gt;<\/span>\n      <span style=\"color: #007700\">&lt;exclusion&gt;<\/span>\n        <span style=\"color: #007700\">&lt;groupId&gt;<\/span>Project_D<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span> <span style=\"color: #888888\">&lt;!-- Project D will not be included --&gt;<\/span>\n        <span style=\"color: #007700\">&lt;artifactId&gt;<\/span>Project-D<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n      <span style=\"color: #007700\">&lt;\/exclusion&gt;<\/span>\n    <span style=\"color: #007700\">&lt;\/exclusions&gt;<\/span>\n  <span style=\"color: #007700\">&lt;\/dependency&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/dependencies&gt;<\/span>\n<\/pre><\/div>\n\n\n\n\n<h2 class=\"wp-block-heading\">Common Maven Commands<\/h2>\n\n\n\n<p>Note: Running a phase of a lifecycle automatically runs all it&#8217;s preceding phases of the same lifecycle.<\/p>\n\n\n\n<p>Marked in red are <span style=\"color:#d14\">phases<\/span>, and marked in blue are <span style=\"color:#08c;\">plugin:goal<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Command<\/td><td>Description<\/td><\/tr><tr><td><code>mvn <span style=\"color:#d14;\">clean<\/span><\/code><\/td><td>clean up previous build (target directory)<\/td><\/tr><tr><td><code>mvn <span style=\"color:#d14;\">compile<\/span><\/code><\/td><td>Compile all java source code present in source directory into java class files.<\/td><\/tr><tr><td><code>mvn <span style=\"color:#d14;\">test-compile<\/span><\/code><\/td><td>Compile test source codes into the test destination directory.<\/td><\/tr><tr><td><code>mvn <span style=\"color:#d14;\">package<\/span><\/code><\/td><td>This takes the compiled code and packages those in a distributable format (For eg. jar format).<\/td><\/tr><tr><td><code>mvn <span style=\"color:#d14;\">install<\/span><\/code><\/td><td>Save the package (created in package phase) to the local repository so that our project can be used as dependency (or for POM inheritance) for other projects locally<\/td><\/tr><tr><td><code>mvn <span style=\"color:#d14;\">deploy<\/span><\/code><\/td><td>Done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.<br><strong>Note:<\/strong> Simply calling this phase, wouldn&#8217;t deploy the artifact to remote repository. The location of the remote repository has to be provided in pom.xml file in order to tell maven where to upload the artifact (see more details below)<\/td><\/tr><tr><td><code>mvn <span style=\"color:#08c;\">help:describe<\/span> -Dcmd=[phase name]<\/code><br><code>mvn <span style=\"color:#08c;\">help:describe<\/span> -Dplugin=[plugin name]<\/code><\/td><td>Get information about specified phase or plugin<\/td><\/tr><tr><td><code>mvn <span style=\"color:#08c;\">dependency:copy-dependencies<\/span><\/code><\/td><td>Download all dependencies from remote repository to local repository<\/td><\/tr><tr><td><code>mvn <span style=\"color:#08c;\">help:effective-pom<\/span><\/code><\/td><td>Show the effective pom.xml which is used by maven after resolving all of the pom inheritance and active profiles.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Deploying artifact to remote repository<\/h2>\n\n\n\n<p>The details of remote repository can be specified in <code>&lt;distributionManagement&gt;<\/code> tag inside <code>pom.xml<\/code> file. Calling <code>mvn deploy<\/code> then uploads the artifact to specified remote repository.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007700\">&lt;distributionManagement&gt;<\/span>\n    <span style=\"color: #007700\">&lt;repository&gt;<\/span>\n        <span style=\"color: #888888\">&lt;!-- id and name can be anything chosen by dev --&gt;<\/span>\n        <span style=\"color: #007700\">&lt;id&gt;<\/span>myRepositoryId<span style=\"color: #007700\">&lt;\/id&gt;<\/span>\n        <span style=\"color: #007700\">&lt;name&gt;<\/span>myRepositoryName<span style=\"color: #007700\">&lt;\/name&gt;<\/span>\n        <span style=\"color: #007700\">&lt;url&gt;<\/span>https:\/\/example.com\/url-of-my-repository<span style=\"color: #007700\">&lt;\/url&gt;<\/span>\n    <span style=\"color: #007700\">&lt;\/repository&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/distributionManagement&gt;<\/span>\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Combining multiple Modules (Multi Module project)<\/h2>\n\n\n\n<p>To combine multiple modules, a base module can be created which contains all of the modules as it&#8217;s submodule (specified using pom.xml of base module). Following is the file structure of a multi module project. Note that in the following example screenshot, a root folder directly contains base module as well as multiple sub modules, however, it is also possible to create multiple sub modules inside the base module folder.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"396\" src=\"https:\/\/spacican.com\/notes\/wp-content\/uploads\/2024\/07\/image.png\" alt=\"\" class=\"wp-image-1859\" style=\"width:308px;height:auto\"\/><\/figure>\n\n\n\n<p>The sub modules (module1 and module2) are nothing different but a normal maven projects. The sub modules are combined together with the help of base-maven-module using the pom.xml file present in base-maven-module. Following are the important configurations of pom.xml of base module:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\">...\n    <span style=\"color: #007700\">&lt;packaging&gt;<\/span>POM<span style=\"color: #007700\">&lt;\/packaging&gt;<\/span>\n    <span style=\"color: #007700\">&lt;modules&gt;<\/span>\n        <span style=\"color: #888888\">&lt;!-- relative path of sub modules --&gt;<\/span>\n        <span style=\"color: #007700\">&lt;module&gt;<\/span>..\/module1<span style=\"color: #007700\">&lt;\/module&gt;<\/span>\n        <span style=\"color: #007700\">&lt;module&gt;<\/span>..\/module2<span style=\"color: #007700\">&lt;\/module&gt;<\/span>\n    <span style=\"color: #007700\">&lt;\/modules&gt;<\/span>\n...\n<\/pre><\/div>\n\n\n\n<p>Executing a maven command on the base module directory executes the command for each sub modules as well. As an example, calling <code>mvn package<\/code> creates the package files for both sub modules and places the package files in the target folder of each of the sub modules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Plugin Configuration\/Parameters<\/h2>\n\n\n\n<p>List out all of the configurable parameters of the plugin can be seen either at plugin&#8217;s documentation page, or by using <code>mvn help:describe -Dcmd=[plugin:goal] -Ddetail<\/code> command<\/p>\n\n\n\n<p>Parameters of plugin can be changed using either <code>mvn [plugin:goal] -D[property]=[value]<\/code>, or using pom.xml file as shown in example code below<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><table><tr><td><pre style=\"margin: 0; line-height: 125%\"> 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12<\/pre><\/td><td><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007700\">&lt;build&gt;<\/span>\n  <span style=\"color: #007700\">&lt;plugins&gt;<\/span>\n    <span style=\"color: #007700\">&lt;plugin&gt;<\/span>\n      <span style=\"color: #007700\">&lt;groupId&gt;<\/span>groupId.of.plugin<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n      <span style=\"color: #007700\">&lt;artifactId&gt;<\/span>artifactId-of-plugin<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n      <span style=\"color: #007700\">&lt;version&gt;<\/span>1.0<span style=\"color: #007700\">&lt;\/version&gt;<\/span>\n      <span style=\"color: #007700\">&lt;configuration&gt;<\/span>\n        <span style=\"color: #007700\">&lt;parameterName&gt;<\/span>value<span style=\"color: #007700\">&lt;\/parameterName&gt;<\/span>\n      <span style=\"color: #007700\">&lt;\/configuration&gt;<\/span>\n    <span style=\"color: #007700\">&lt;\/plugin&gt;<\/span>\n  <span style=\"color: #007700\">&lt;\/plugins&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/build&gt;<\/span>\n<\/pre><\/td><\/tr><\/table><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Binding plugin goals to phases<\/h2>\n\n\n\n<p>Following is an example code to bind a plugin goal to a phase<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007700\">&lt;build&gt;<\/span>\n    <span style=\"color: #007700\">&lt;plugins&gt;<\/span>\n        <span style=\"color: #007700\">&lt;plugin&gt;<\/span>\n        <span style=\"color: #007700\">&lt;groupId&gt;<\/span>plugin.group.id<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n        <span style=\"color: #007700\">&lt;artifactId&gt;<\/span>plugin-artifact-id<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n        <span style=\"color: #007700\">&lt;version&gt;<\/span>1.0<span style=\"color: #007700\">&lt;\/version&gt;<\/span>\n        <span style=\"color: #007700\">&lt;executions&gt;<\/span>\n          <span style=\"color: #007700\">&lt;execution&gt;<\/span>\n            <span style=\"color: #007700\">&lt;id&gt;<\/span>my-execution-identifier<span style=\"color: #007700\">&lt;\/id&gt;<\/span>\n            <span style=\"color: #007700\">&lt;phase&gt;<\/span>phase-name<span style=\"color: #007700\">&lt;\/phase&gt;<\/span>\n            <span style=\"color: #007700\">&lt;configuration&gt;<\/span>\n              <span style=\"color: #007700\">&lt;url&gt;<\/span>http:\/\/www.foo.com\/query<span style=\"color: #007700\">&lt;\/url&gt;<\/span>\n              <span style=\"color: #007700\">&lt;timeout&gt;<\/span>10<span style=\"color: #007700\">&lt;\/timeout&gt;<\/span>\n              <span style=\"color: #007700\">&lt;options&gt;<\/span>\n                <span style=\"color: #007700\">&lt;option&gt;<\/span>one<span style=\"color: #007700\">&lt;\/option&gt;<\/span>\n                <span style=\"color: #007700\">&lt;option&gt;<\/span>two<span style=\"color: #007700\">&lt;\/option&gt;<\/span>\n                <span style=\"color: #007700\">&lt;option&gt;<\/span>three<span style=\"color: #007700\">&lt;\/option&gt;<\/span>\n              <span style=\"color: #007700\">&lt;\/options&gt;<\/span>\n            <span style=\"color: #007700\">&lt;\/configuration&gt;<\/span>\n            <span style=\"color: #007700\">&lt;goals&gt;<\/span>\n              <span style=\"color: #007700\">&lt;goal&gt;<\/span>plugin-goal<span style=\"color: #007700\">&lt;\/goal&gt;<\/span>\n            <span style=\"color: #007700\">&lt;\/goals&gt;<\/span>\n          <span style=\"color: #007700\">&lt;\/execution&gt;<\/span>\n        <span style=\"color: #007700\">&lt;\/executions&gt;<\/span>\n      <span style=\"color: #007700\">&lt;\/plugin&gt;<\/span>\n    <span style=\"color: #007700\">&lt;\/plugins&gt;<\/span>\n <span style=\"color: #007700\">&lt;\/build&gt;<\/span>\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Archetype Plugin<\/h2>\n\n\n\n<p>Archetype is project templating plugin, common use of this plugin is to quickly setup project structure using <code>mvn archetype:generate<\/code> command. The command can be used to setup project based on already existing templates, as well as used to setup project based on provided project details through CLI interface (just like <code>npm init<\/code> in node.js)<\/p>\n\n\n\n<p>As an example, archetype can be used to setup complex project configurations based on variety of spring templates so that we don&#8217;t have to go through initial configuration setup for creating a spring application.<\/p>\n\n\n\n<p>Archetype Plugin can also be used to save a custom maven project as archetype template so that It can be shared and used by multiple users across different devices.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Maven is a build and dependency management tool for Java applications. Maven Terminologies Project Object Model (pom.xml) file Repositories Build Lifecycle Maven CLI structure mvn [options] [&lt;goal(s)&gt;] [&lt;phase(s)&gt;] example: Maven File Structure Following are most commonly used directories in maven project. Full list of directories\/files can be seen here. POM inheritance Much like java class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1728,"comment_status":"open","ping_status":"closed","sticky":false,"template":"single_compact.php","format":"standard","meta":{"footnotes":""},"categories":[2,6],"tags":[],"_links":{"self":[{"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/posts\/1720"}],"collection":[{"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/comments?post=1720"}],"version-history":[{"count":80,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/posts\/1720\/revisions"}],"predecessor-version":[{"id":1978,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/posts\/1720\/revisions\/1978"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/media\/1728"}],"wp:attachment":[{"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/media?parent=1720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/categories?post=1720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/tags?post=1720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}