{"id":492,"date":"2019-07-13T12:30:47","date_gmt":"2019-07-13T19:30:47","guid":{"rendered":"http:\/\/35.243.195.209\/?p=492"},"modified":"2021-04-19T19:18:47","modified_gmt":"2021-04-20T02:18:47","slug":"dependencymanagement-and-dependencies-in-maven","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/07\/13\/dependencymanagement-and-dependencies-in-maven\/","title":{"rendered":"DependencyManagement and Dependencies in Maven"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce the key difference between Maven&#8217;s <code>&lt;dependencyManagement&gt;<\/code> and <code>&lt;dependencies&gt;<\/code>.<\/p>\n<h2>Conclusion<\/h2>\n<ol>\n<li>In pom inheritance, artifacts specified <code>&lt;dependencies&gt;<\/code> will <strong>ALWAYS<\/strong> be included as a dependency of the child module(s). However, artifacts specified in <code>&lt;dependencyManagement&gt;<\/code> will only be included if child modules explicitly specify them in <code>&lt;dependencies&gt;<\/code>.<\/li>\n<li><code>&lt;dependencyManagement&gt;<\/code> is good for centralized version control.<\/li>\n<li><code>&lt;dependencyManagement&gt;<\/code> is good for import managed dependencies from other projects.<\/li>\n<\/ol>\n<h2>Details<\/h2>\n<p>We are always curious to dive deeper and examples are more intuitive than theory. We build a parent pom and a child pom to explain the above conclusions. We install the parent pom into local repo by <code>sudo mvn clean install<\/code>.<\/p>\n<pre><code class=\"language-xml\">&lt;!--  Parent pom  --&gt;\n    &lt;groupId&gt;edu.fighternan.java&lt;\/groupId&gt;\n    &lt;artifactId&gt;review&lt;\/artifactId&gt;\n    &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;\n    &lt;packaging&gt;pom&lt;\/packaging&gt;\n\n    &lt;dependencies&gt;\n        &lt;dependency&gt;\n            &lt;artifactId&gt;lambda&lt;\/artifactId&gt;\n            &lt;groupId&gt;software.amazon.awssdk&lt;\/groupId&gt;\n        &lt;\/dependency&gt;\n    &lt;\/dependencies&gt;\n\n    &lt;dependencyManagement&gt;\n        &lt;dependencies&gt;\n            &lt;dependency&gt;\n                &lt;groupId&gt;software.amazon.awssdk&lt;\/groupId&gt;\n                &lt;artifactId&gt;bom&lt;\/artifactId&gt;\n                &lt;version&gt;2.3.9&lt;\/version&gt;\n                &lt;type&gt;pom&lt;\/type&gt;\n                &lt;scope&gt;import&lt;\/scope&gt;\n            &lt;\/dependency&gt;\n        &lt;\/dependencies&gt;\n    &lt;\/dependencyManagement&gt;\n\n    &lt;build&gt;\n        &lt;pluginManagement&gt;\n            &lt;plugins&gt;\n                &lt;plugin&gt;\n                    &lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\n                    &lt;version&gt;3.8.0&lt;\/version&gt;\n                    &lt;configuration&gt;\n                        &lt;source&gt;1.8&lt;\/source&gt;\n                        &lt;target&gt;1.8&lt;\/target&gt;\n                    &lt;\/configuration&gt;\n                &lt;\/plugin&gt;\n            &lt;\/plugins&gt;\n        &lt;\/pluginManagement&gt;\n    &lt;\/build&gt;<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;!--  Child pom  --&gt;\n    &lt;parent&gt;\n        &lt;groupId&gt;edu.fighternan.java&lt;\/groupId&gt;\n        &lt;artifactId&gt;review&lt;\/artifactId&gt;\n        &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;\n    &lt;\/parent&gt;\n\n    &lt;groupId&gt;edu.fighternan.java&lt;\/groupId&gt;\n    &lt;artifactId&gt;review2&lt;\/artifactId&gt;\n    &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;<\/code><\/pre>\n<h3>1. Inheritance<\/h3>\n<p>The parent pom imports another <code>&lt;dependencyManagement&gt;<\/code>, which will be explained later. What we know for now is that the parent pom specifies a dependency explicitly.<\/p>\n<p>By execute <code>mvn dependency:tree<\/code>, we can see the denpendency tree.<\/p>\n<pre><code class=\"language-bash\">[INFO] edu.<code class=\"language-xml\">fighternan<\/code>.java:review:pom:1.0-SNAPSHOT [INFO] \\- software.amazon.awssdk:lambda:jar:2.3.9:compile<\/code><\/pre>\n<p>In the child pom, we do not have any explicit dependencies. However, it has a transitive dependency inherited from its parent.<\/p>\n<pre><code class=\"language-bash\">[INFO] edu.<code class=\"language-xml\">fighternan<\/code>.java:review2:jar:1.0-SNAPSHOT [INFO] \\- software.amazon.awssdk:lambda:jar:2.3.9:compile<\/code><\/pre>\n<p>If we remove the explicit dependency in parent pom by removing the whole <code>&lt;dependencies&gt;<\/code> element. There will be no dependencies for both parent and child anymore.<\/p>\n<h3>2. Centralized Version Control<\/h3>\n<p>In the parent pom, we explicitly declare a <code>AWS lambda<\/code> artifact without version. Actually <code>software.amazon.awssdk.aws-sdk-java-pom<\/code> which is the parent of <code>software.amazon.awssdk.bom<\/code> defines the version. BOM stands for <code>bill of materials<\/code>, which centralizes the version control for any other artifact which imports the BOM.<\/p>\n<h3>3. Multiple Inheritance<\/h3>\n<p>You cannot have a pom-type project as a <code>simple dependency<\/code> in another project. There can only be a parent-child relationship. However, a project can only inherit from a single parent.<\/p>\n<p><code>Import<\/code> scope for <code>pom<\/code> type dependency in <code>&lt;dependencyManagement&gt;<\/code> section allows you to achieve the equivalent of <code>multiple inheritances<\/code>. Also, you can leverage the centralized control of <code>&lt;dependencyManagement&gt;<\/code>.<\/p>\n<h2>Reference<\/h2>\n<p><a href=\"http:\/\/maven.apache.org\/guides\/introduction\/introduction-to-dependency-mechanism.html#Importing_Dependencies\">Maven&#8217;s Official Guide<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce the key difference between Maven&#8217;s &lt;dependencyManagement&gt; and &lt;dependencies&gt;. Conclusion In pom inheritance, artifacts specified &lt;dependencies&gt; will ALWAYS be included as a dependency of the child module(s). However, artifacts specified in &lt;dependencyManagement&gt; will only be included if child modules explicitly specify them in &lt;dependencies&gt;. &lt;dependencyManagement&gt; is good for centralized&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,5,25],"tags":[],"class_list":["post-492","post","type-post","status-publish","format-standard","hentry","category-java","category-proglang","category-software-engineering"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":5,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"predecessor-version":[{"id":1581,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/492\/revisions\/1581"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}