-
Notifications
You must be signed in to change notification settings - Fork 0
/
maven-getting-started.html
181 lines (178 loc) · 39.6 KB
/
maven-getting-started.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html><html lang="de-ch"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Maven getting started - Finecloud</title><meta name="description" content="What is Maven? Declarative build management tool for automated builds Standardized directory structure Declarative dependency management Works with an XML control file (POM - Project Object Model) Open source, written in Java, for Java projects Pure command line tool IDEs offer integration Why Maven? Uniform…"><meta name="generator" content="Publii Open-Source CMS for Static Site"><link rel="stylesheet" href="https://www.finecloud.ch/media/plugins/syntaxHighlighter/prism-black.css"><link rel="canonical" href="https://www.finecloud.ch/maven-getting-started.html"><link rel="alternate" type="application/atom+xml" href="https://www.finecloud.ch/feed.xml"><link rel="alternate" type="application/json" href="https://www.finecloud.ch/feed.json"><meta property="og:title" content="Maven getting started"><meta property="og:site_name" content="Finecloud"><meta property="og:description" content="What is Maven? Declarative build management tool for automated builds Standardized directory structure Declarative dependency management Works with an XML control file (POM - Project Object Model) Open source, written in Java, for Java projects Pure command line tool IDEs offer integration Why Maven? Uniform…"><meta property="og:url" content="https://www.finecloud.ch/maven-getting-started.html"><meta property="og:type" content="article"><link rel="shortcut icon" href="https://www.finecloud.ch/media/website/finecloud.png" type="image/png"><link rel="stylesheet" href="https://www.finecloud.ch/assets/css/style.css?v=39da73365516a098a9b73b721fc970e2"><script type="application/ld+json">{"@context":"http://schema.org","@type":"Article","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.finecloud.ch/maven-getting-started.html"},"headline":"Maven getting started","datePublished":"2022-10-27T17:01","dateModified":"2022-11-03T16:59","description":"What is Maven? Declarative build management tool for automated builds Standardized directory structure Declarative dependency management Works with an XML control file (POM - Project Object Model) Open source, written in Java, for Java projects Pure command line tool IDEs offer integration Why Maven? Uniform…","author":{"@type":"Person","name":"Finecloud","url":"https://www.finecloud.ch/authors/finecloud/"},"publisher":{"@type":"Organization","name":"Finecloud"}}</script><meta name="google-site-verification" content="seFY9U12uiEq5U3_MyZiX6XWzk0AVFl9zITr2ZKsytY"></head><body><div class="site-container"><header class="top" id="js-header"><a class="logo" href="https://www.finecloud.ch/">Finecloud</a><nav class="navbar js-navbar"><button class="navbar__toggle js-toggle" aria-label="Menu" aria-haspopup="true" aria-expanded="false"><span class="navbar__toggle-box"><span class="navbar__toggle-inner">Menu</span></span></button><ul class="navbar__menu"><li><a href="https://www.finecloud.ch/" target="_self">Blog</a></li><li><a href="https://www.finecloud.ch/tags/" target="_self">Tags</a></li></ul></nav><div class="search"><div class="search__overlay js-search-overlay"><div class="search__overlay-inner"><form action="https://www.finecloud.ch/search.html" class="search__form"><input class="search__input js-search-input" type="search" name="q" placeholder="search..." aria-label="search..." autofocus="autofocus"></form><button class="search__close js-search-close" aria-label="Close">Close</button></div></div><button class="search__btn js-search-btn" aria-label="Search"><svg role="presentation" focusable="false"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#search"/></svg></button></div></header><main><article class="post"><div class="hero"><figure class="hero__image hero__image--overlay"><img src="https://www.finecloud.ch/media/website/download.jpg" srcset="https://www.finecloud.ch/media/website/responsive/download-xs.jpg 300w, https://www.finecloud.ch/media/website/responsive/download-sm.jpg 480w, https://www.finecloud.ch/media/website/responsive/download-md.jpg 768w, https://www.finecloud.ch/media/website/responsive/download-lg.jpg 1024w, https://www.finecloud.ch/media/website/responsive/download-xl.jpg 1360w, https://www.finecloud.ch/media/website/responsive/download-2xl.jpg 1600w" sizes="100vw" loading="eager" alt=""></figure><header class="hero__content"><div class="wrapper"><div class="post__meta"><time datetime="2022-10-27T17:01">Oktober 27, 2022</time></div><h1>Maven getting started</h1></div></header></div><div class="wrapper post__entry"><div class="post__toc"><h3>Table of Contents</h3><ul><li><a href="#mcetoc_1ggd2611qj0">What is Maven?</a></li><li><a href="#mcetoc_1ggd2611qj1">Why Maven?</a></li><li><a href="#mcetoc_1ggd2611qj2">Components</a></li><li><a href="#mcetoc_1ggd2611qj3">Build lifecycle</a></li><li><a href="#mcetoc_1ggd2611qj4">POM (Project Object Model) file</a></li><li><a href="#mcetoc_1ggd2611qj5">Project coordinates</a></li><li><a href="#mcetoc_1ggd443kjlt">Packaging</a></li><li><a href="#mcetoc_1ggd2611qj6">Naming conventions</a><ul><li><a href="#mcetoc_1ggd2611qj7">groupId</a></li><li><a href="#mcetoc_1ggd2611qj8">artifactId</a></li></ul></li><li><a href="#mcetoc_1ggd443kjlu">Standard directory layout</a></li><li><a href="#mcetoc_1ggd8kpb6ob">Dependencies</a><ul><li><a href="#mcetoc_1ggd8kpb6oc">Scope</a></li><li><a href="#mcetoc_1ggd9jq2mqi">Transitive dependencies</a></li></ul></li><li><a href="#mcetoc_1ggd9jq2mqj">Project inheritance</a></li><li><a href="#mcetoc_1ggd9jq2mqk">Project aggregation (Multi Module)</a></li><li><a href="#mcetoc_1ggd9tb9jr2">Properties</a></li><li><a href="#mcetoc_1ggv3g3smf5">Version structure</a><ul><li><a href="#mcetoc_1ggv3g3smf6">SNAPSHOT Version</a></li></ul></li><li><a href="#mcetoc_1ggv3g3smf7">Version resolution transitive dependencies</a></li><li><a href="#mcetoc_1ggv3g3smf8">Keeping POM clean</a></li></ul></div><h2 id="mcetoc_1ggd2611qj0">What is Maven?</h2><ul><li>Declarative build management tool for automated builds</li><li>Standardized directory structure</li><li>Declarative dependency management</li><li>Works with an XML control file (POM - Project Object Model)</li><li>Open source, written in Java, for Java projects</li><li>Pure command line tool</li><li>IDEs offer integration</li></ul><h2 id="mcetoc_1ggd2611qj1">Why Maven?</h2><ul><li>Uniform build in the project, but also across multiple projects.</li><li>Dependency management</li><li>Independent of an IDE</li><li>Basis for rapid familiarization of new developers</li><li>Basis for Continuous Integration/Continuous Deployment (DevOps)</li><li>Industry standard for Java applications</li></ul><h2 id="mcetoc_1ggd2611qj2">Components</h2><figure class="post__image"><img loading="lazy" src="https://www.finecloud.ch/media/posts/68/Screenshot-2022-10-27-at-17.17.05.png" alt="" width="851" height="555" sizes="100vw" srcset="https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-17.17.05-xs.png 300w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-17.17.05-sm.png 480w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-17.17.05-md.png 768w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-17.17.05-lg.png 1024w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-17.17.05-xl.png 1360w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-17.17.05-2xl.png 1600w"></figure><h2 id="mcetoc_1ggd2611qj3">Build lifecycle</h2><ul><li>Each Build Phase has its own Plugin. M<span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">aven delegates the work of each build phase to plugins.</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Plugins provide Goals, which do the actual work</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Goals can be assigned to one or more build phases</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Certain plugins/goals are assigned by Maven by default</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">You can assign plugins/goals to specific build phases in the project object model itself</span></li></ul><h2 id="mcetoc_1ggd2611qj4">POM (Project Object Model) file</h2><ul><li>The POM declaratively describes the build including all dependencies.</li><li>POM is an XML file that describes the Maven project.</li><li>Maven project usually creates an artifact (jar, war, ear, ...)</li></ul><p>Minimal POM example file:</p><pre class="hljs" style="color: #a9b7c6; background: #282b2e; display: block; overflow-x: auto; padding: 0.5em;"><span class="php"><span class="hljs-meta" style="color: #bbb529;"><?</span>xml version=<span class="hljs-string" style="color: #6a8759;">"1.0"</span> encoding=<span class="hljs-string" style="color: #6a8759;">"UTF-8"</span><span class="hljs-meta" style="color: #bbb529;">?></span></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string" style="color: #6a8759;">"http://maven.apache.org/POM/4.0.0"</span>
<span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string" style="color: #6a8759;">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string" style="color: #6a8759;">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">modelVersion</span>></span>4.0.0<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">modelVersion</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>com.test<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>test<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">version</span>></span>1.0-SNAPSHOT<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">project</span>></span></pre><h2 id="mcetoc_1ggd2611qj5">Project coordinates</h2><p>The project coordinates consists of groupId, artifactId and the version</p><ul><li>Maven project coordinates form a kind of address for a Maven artifact</li><li>They consist of:<br><code><groupId>com.test</groupId></code><br><code><artifactId>my-app</artifactId></code><br><code><version>1.0.0</version></code></li><li>The combination of the three parts shall be globally unique!</li><li>Using the project coordinates, the dependencies are fetched from the repository fetched</li><li>All three parts are mandatory</li></ul><h2 id="mcetoc_1ggd443kjlt">Packaging</h2><ul><li>Packaging defines the shape of the generated artifact</li><li>Packaging:<br><code><packaging>jar</packaging></code></li><li>Not specified: default is jar.</li><li>Other possible values: pom, maven-plugin, ejb, war, ear, rar</li></ul><h2 id="mcetoc_1ggd2611qj6">Naming conventions</h2><h3 id="mcetoc_1ggd2611qj7">groupId</h3><ul><li>Identifies the project globally → unique namespace required.</li><li>The Java package naming conventions are used.</li><li>Reverse Internet domain name, which one has under one's own control<br>has, e.g. com.example</li><li>After that, freely selectable, uniquely within domain, e.g. com.example.dev</li></ul><h3 id="mcetoc_1ggd2611qj8">artifactId</h3><ul><li>Identifies the concrete build artifact (jar, war, ear, ...).</li><li>E.g. todoapp</li></ul><h2 id="mcetoc_1ggd443kjlu">Standard directory layout</h2><figure class="post__image"><img loading="lazy" src="https://www.finecloud.ch/media/posts/68/Screenshot-2022-10-27-at-18.09.08.png" alt="" width="283" height="430" sizes="100vw" srcset="https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-18.09.08-xs.png 300w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-18.09.08-sm.png 480w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-18.09.08-md.png 768w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-18.09.08-lg.png 1024w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-18.09.08-xl.png 1360w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-18.09.08-2xl.png 1600w"></figure><p>Legend to the layout:</p><table class="table table-striped" border="1"><tbody><tr class="a"><td align="left"><code>src/main/java</code></td><td align="left">Application/Library sources</td></tr><tr class="b"><td align="left"><code>src/main/resources</code></td><td align="left">Application/Library resources</td></tr><tr class="a"><td align="left"><code>src/main/filters</code></td><td align="left">Resource filter files</td></tr><tr class="b"><td align="left"><code>src/main/webapp</code></td><td align="left">Web application sources</td></tr><tr class="a"><td align="left"><code>src/test/java</code></td><td align="left">Test sources</td></tr><tr class="b"><td align="left"><code>src/test/resources</code></td><td align="left">Test resources</td></tr><tr class="a"><td align="left"><code>src/test/filters</code></td><td align="left">Test resource filter files</td></tr><tr class="b"><td align="left"><code>src/it</code></td><td align="left">Integration Tests (primarily for plugins)</td></tr><tr class="a"><td align="left"><code>src/assembly</code></td><td align="left">Assembly descriptors</td></tr><tr class="b"><td align="left"><code>src/site</code></td><td align="left">Site</td></tr><tr class="a"><td align="left"><code>LICENSE.txt</code></td><td align="left">Project's license</td></tr><tr class="b"><td align="left"><code>NOTICE.txt</code></td><td align="left">Notices and attributions required by libraries that the project depends on</td></tr><tr class="a"><td align="left"><code>README.txt</code></td><td align="left">Project's readme</td></tr></tbody></table><h2 id="mcetoc_1ggd8kpb6ob">Dependencies</h2><ul><li>Dependency management is the central function of Maven</li><li>Management of the required libraries</li><li>Dependencies required by libraries themselves are added automatically (without specifying them) → transitive dependencies.</li><li>Maven automatically downloads dependencies and transitive dependencies.</li><li>The downloaded dependencies are available for compilation.<br>available</li><li>The downloaded dependencies are packed into the artifacts</li></ul><p>example of a dependency declaration in a POM file:</p><pre class="hljs" style="color: #a9b7c6; background: #282b2e; display: block; overflow-x: auto; padding: 0.5em;"><span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">dependencies</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>spring-boot-starter-jdbc<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>
<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>spring-boot-starter-web<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>
<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>spring-boot-starter-test<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">scope</span>></span>test<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">scope</span>></span>
<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">dependency</span>></span>
<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">dependencies</span>></span></pre><p>You can also list all dependencies with this command: </p><pre class="hljs" style="color: #a9b7c6; background: #282b2e; display: block; overflow-x: auto; padding: 0.5em;"><span class="hljs-comment" style="color: grey;"># mvn dependency:tree </span>
[INFO] Scanning <span class="hljs-keyword" style="color: #cc7832;">for</span> projects...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.3.0:tree (default-cli) @ demo ---
[INFO] com.example:demo:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.7.5:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.7.5:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.7.5:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.11:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.11:compile
[INFO] | | | +- org.apache.logging.log4j:<span class="hljs-built_in">log</span>4j-to-slf4j:jar:2.17.2:compile
[INFO] | | | | \- org.apache.logging.log4j:<span class="hljs-built_in">log</span>4j-api:jar:2.17.2:compile
[INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile
[INFO] | | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.30:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.7.5:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.4.2:compile
[INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.4:compile
[INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.13.4:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.13.4:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.13.4:compile
[INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.13.4:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.7.5:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.68:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.68:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.68:compile
[INFO] | +- org.springframework:spring-web:jar:5.3.23:compile
[INFO] | | \- org.springframework:spring-beans:jar:5.3.23:compile
[INFO] | \- org.springframework:spring-webmvc:jar:5.3.23:compile
[INFO] | +- org.springframework:spring-aop:jar:5.3.23:compile
[INFO] | +- org.springframework:spring-context:jar:5.3.23:compile
[INFO] | \- org.springframework:spring-expression:jar:5.3.23:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:2.7.5:runtime
[INFO] | +- org.springframework.boot:spring-boot:jar:2.7.5:compile
[INFO] | \- org.springframework.boot:spring-boot-autoconfigure:jar:2.7.5:compile
[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:2.7.5:<span class="hljs-built_in">test</span>
[INFO] +- org.springframework.boot:spring-boot-test:jar:2.7.5:<span class="hljs-built_in">test</span>
[INFO] +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.7.5:<span class="hljs-built_in">test</span>
[INFO] +- com.jayway.jsonpath:json-path:jar:2.7.0:<span class="hljs-built_in">test</span>
[INFO] | +- net.minidev:json-smart:jar:2.4.8:<span class="hljs-built_in">test</span>
[INFO] | | \- net.minidev:accessors-smart:jar:2.4.8:<span class="hljs-built_in">test</span>
[INFO] | | \- org.ow2.asm:asm:jar:9.1:<span class="hljs-built_in">test</span>
[INFO] | \- org.slf4j:slf4j-api:jar:1.7.36:compile
[INFO] +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:<span class="hljs-built_in">test</span>
[INFO] | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:<span class="hljs-built_in">test</span>
[INFO] +- org.assertj:assertj-core:jar:3.22.0:<span class="hljs-built_in">test</span>
[INFO] +- org.hamcrest:hamcrest:jar:2.2:<span class="hljs-built_in">test</span>
[INFO] +- org.junit.jupiter:junit-jupiter:jar:5.8.2:<span class="hljs-built_in">test</span>
[INFO] | +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:<span class="hljs-built_in">test</span>
[INFO] | | +- org.opentest4j:opentest4j:jar:1.2.0:<span class="hljs-built_in">test</span>
[INFO] | | +- org.junit.platform:junit-platform-commons:jar:1.8.2:<span class="hljs-built_in">test</span>
[INFO] | | \- org.apiguardian:apiguardian-api:jar:1.1.2:<span class="hljs-built_in">test</span>
[INFO] | +- org.junit.jupiter:junit-jupiter-params:jar:5.8.2:<span class="hljs-built_in">test</span>
[INFO] | \- org.junit.jupiter:junit-jupiter-engine:jar:5.8.2:<span class="hljs-built_in">test</span>
[INFO] | \- org.junit.platform:junit-platform-engine:jar:1.8.2:<span class="hljs-built_in">test</span>
[INFO] +- org.mockito:mockito-core:jar:4.5.1:<span class="hljs-built_in">test</span>
[INFO] | +- net.bytebuddy:byte-buddy:jar:1.12.18:<span class="hljs-built_in">test</span>
[INFO] | +- net.bytebuddy:byte-buddy-agent:jar:1.12.18:<span class="hljs-built_in">test</span>
[INFO] | \- org.objenesis:objenesis:jar:3.2:<span class="hljs-built_in">test</span>
[INFO] +- org.mockito:mockito-junit-jupiter:jar:4.5.1:<span class="hljs-built_in">test</span>
[INFO] +- org.skyscreamer:jsonassert:jar:1.5.1:<span class="hljs-built_in">test</span>
[INFO] | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:<span class="hljs-built_in">test</span>
[INFO] +- org.springframework:spring-core:jar:5.3.23:compile
[INFO] | \- org.springframework:spring-jcl:jar:5.3.23:compile
[INFO] +- org.springframework:spring-test:jar:5.3.23:<span class="hljs-built_in">test</span>
[INFO] \- org.xmlunit:xmlunit-core:jar:2.9.0:<span class="hljs-built_in">test</span>
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.256 s
[INFO] Finished at: 2022-10-27T19:47:12+02:00
[INFO] ------------------------------------------------------------------------
</pre><h3 id="mcetoc_1ggd8kpb6oc">Scope</h3><p>The scope of a dependency specifies in which classpaths it is provided by Maven.<br>(classpaths change depending on the build phase):</p><ul><li><strong>compile</strong>: Default Scope, Dependencies are available in all classpaths</li><li><strong>test</strong>: Only during test</li><li><strong>provided</strong>: Only available in compile and test classpaths, dependency must be provided at runtime (e.g., by the servlet container)</li><li><strong>runtime</strong>: Only during runtime and test, but not for compile</li><li><strong>other</strong> (not so important)</li></ul><h3 id="mcetoc_1ggd9jq2mqi">Transitive dependencies</h3><ul><li>Dependencies required by the dependencies we declare.</li><li>Are automatically downloaded by Maven and added to the classpath</li><li>Attention: You can also use these transitive dependencies directly in the code.<br>→ please do not do this!</li><li>Explicitly enter each dependency that you use yourself in the code as a dependency in the POM</li></ul><p>Finding out if you are using a transitive dependency in your code:</p><pre class="hljs" style="color: #a9b7c6; background: #282b2e; display: block; overflow-x: auto; padding: 0.5em;"><span class="hljs-comment" style="color: grey;"># mvn dependency:analyze </span>
[INFO] Scanning <span class="hljs-keyword" style="color: #cc7832;">for</span> projects...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> maven-dependency-plugin:3.3.0:analyze (default-cli) > <span class="hljs-built_in">test</span>-compile @ demo >>>
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo ---
[INFO] Using <span class="hljs-string" style="color: #6a8759;">'UTF-8'</span> encoding to copy filtered resources.
[INFO] Using <span class="hljs-string" style="color: #6a8759;">'UTF-8'</span> encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 <span class="hljs-built_in">source</span> file to /Users/Dave/Downloads/demo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:<span class="hljs-built_in">test</span>Resources (default-testResources) @ demo ---
[INFO] Using <span class="hljs-string" style="color: #6a8759;">'UTF-8'</span> encoding to copy filtered resources.
[INFO] Using <span class="hljs-string" style="color: #6a8759;">'UTF-8'</span> encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /Users/Dave/Downloads/demo/src/<span class="hljs-built_in">test</span>/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:<span class="hljs-built_in">test</span>Compile (default-testCompile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 <span class="hljs-built_in">source</span> file to /Users/Dave/Downloads/demo/target/<span class="hljs-built_in">test</span>-classes
[INFO]
[INFO] <<< maven-dependency-plugin:3.3.0:analyze (default-cli) < <span class="hljs-built_in">test</span>-compile @ demo <<<
[INFO]
[INFO]
[INFO] --- maven-dependency-plugin:3.3.0:analyze (default-cli) @ demo ---
[WARNING] Used undeclared dependencies found:
[WARNING] org.springframework.boot:spring-boot:jar:2.7.5:compile
[WARNING] org.junit.jupiter:junit-jupiter-api:jar:5.8.2:<span class="hljs-built_in">test</span>
[WARNING] org.springframework.boot:spring-boot-test:jar:2.7.5:<span class="hljs-built_in">test</span>
[WARNING] org.springframework.boot:spring-boot-autoconfigure:jar:2.7.5:compile
[WARNING] Unused declared dependencies found:
[WARNING] org.springframework.boot:spring-boot-starter-web:jar:2.7.5:compile
[WARNING] org.springframework.boot:spring-boot-devtools:jar:2.7.5:runtime
[WARNING] org.springframework.boot:spring-boot-starter-test:jar:2.7.5:<span class="hljs-built_in">test</span>
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.113 s
[INFO] Finished at: 2022-10-27T19:50:29+02:00
[INFO] ------------------------------------------------------------------------
</pre><h2 id="mcetoc_1ggd9jq2mqj">Project inheritance</h2><p>POMs an also inherit, this is for example needed for a Spring Boot-Application. This allows you for example, to standardize the libraries used and their versions across multiple projects or Maven modules.</p><figure class="post__image"><img loading="lazy" src="https://www.finecloud.ch/media/posts/68/inheritance.png" alt="" width="1024" height="754" sizes="100vw" srcset="https://www.finecloud.ch/media/posts/68/responsive/inheritance-xs.png 300w, https://www.finecloud.ch/media/posts/68/responsive/inheritance-sm.png 480w, https://www.finecloud.ch/media/posts/68/responsive/inheritance-md.png 768w, https://www.finecloud.ch/media/posts/68/responsive/inheritance-lg.png 1024w, https://www.finecloud.ch/media/posts/68/responsive/inheritance-xl.png 1360w, https://www.finecloud.ch/media/posts/68/responsive/inheritance-2xl.png 1600w"></figure><h2 id="mcetoc_1ggd9jq2mqk">Project aggregation (Multi Module)</h2><p>Also a POM can aggregate multiple POMs as Modules</p><figure class="post__image"><img loading="lazy" src="https://www.finecloud.ch/media/posts/68/Screenshot-2022-10-27-at-20.01.00.png" alt="" width="275" height="400" sizes="100vw" srcset="https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-20.01.00-xs.png 300w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-20.01.00-sm.png 480w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-20.01.00-md.png 768w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-20.01.00-lg.png 1024w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-20.01.00-xl.png 1360w, https://www.finecloud.ch/media/posts/68/responsive/Screenshot-2022-10-27-at-20.01.00-2xl.png 1600w"></figure><pre class="hljs" style="color: #a9b7c6; background: #282b2e; display: block; overflow-x: auto; padding: 0.5em;"><span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>com.test<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>example-maven-multimodule<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">version</span>></span>1.0-SNAPSHOT<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">version</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">packaging</span>></span>pom<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">packaging</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">modules</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">module</span>></span>module1<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">module</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">module</span>></span>module2<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">module</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">module</span>></span>module3<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">module</span>></span>
<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">modules</span>></span></pre><h2 id="mcetoc_1ggd9tb9jr2">Properties</h2><p>Properties are placeholders and can be addressed via ${<propertyname>}.</p><p>Example:</p><pre class="hljs" style="color: #a9b7c6; background: #282b2e; display: block; overflow-x: auto; padding: 0.5em;"><span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">properties</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">java.version</span>></span>1.8<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">java.version</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">commons-pool2.version</span>></span>2.6.0<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">commons-pool2.version</span>></span>
<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">properties</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>org.apache.commons<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>commons-pool2<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name" style="color: #e8bf6a;">version</span>></span>${commons-pool2.version}<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name" style="color: #e8bf6a;">dependency</span>></span></pre><h2 id="mcetoc_1ggv3g3smf5">Version structure</h2><ul><li>A version must always be specified</li><li>Maven basically leaves the structure of a version free</li><li>Recommended approach: semantic versioning (<a href="https://semver.org/">https://semver.org/</a>)<ul><li><code><Major>.<Minor>.<Bugfix>-<Qualifier>-<Buildnumber></code></li><li>Major: increment when API changes.</li><li>Minor: increment if new features are included, but same API.</li><li>Bugfix: increment, if only something was fixed</li><li>Qualifier: Optional.</li><li>Build number: Optional.</li><li>Example: <version>2.4.18</version></li></ul></li></ul><h3 id="mcetoc_1ggv3g3smf6">SNAPSHOT Version</h3><ul><li>The qualifier SNAPSHOT is used during the development of a version<br>is used</li><li>Example: 1.0-SNAPSHOT → means that the 1.0 version of the software is being<br>is being developed</li><li>A version with SNAPSHOT qualifier is always considered to be older than<br>the same one without the qualifier</li><li>Special rules apply for version resolution, Maven usually fetches a snapshot version once per day from the repository, unless you specify "-U".</li><li>Example: <code>mvn -U clean install</code> also fetches the most recent snapshot<br>versions from the repository</li></ul><h2 id="mcetoc_1ggv3g3smf7">Version resolution transitive dependencies</h2><ul><li>Multiple transitive dependencies can contain the same artifact in different<br>different versions, but can only have exactly one version of a transitive dependency in the classpath.</li><li>Resolution is performed according to the "nearest definition" principle:<ul><li>The version in the dependency tree that is closest to one's own project<br>Tree is taken</li><li>If two versions are equally close, the one found first is taken</li><li>Example: Dependency D appears twice in the dependency tree:<ul><li>A → B → C → D (version 2.0)</li><li>A → E → D (version 1.0)</li><li>Version 1.0 of D is taken, since closer to own project (A).</li></ul></li></ul></li><li>One can explicitly specify a particular version of a transitive dependency<br>specify:<ul><li>Include it explicitly as a dependency in your own POM.</li><li>Via Dependency Management</li></ul></li></ul><h2 id="mcetoc_1ggv3g3smf8">Keeping POM clean</h2><ul><li>Find out unclean dependencies:<ul><li><code>mvn dependency:analyze</code></li><li>check out <a href="https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html">https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html</a></li></ul></li><li>Lists dependencies that:<ul><li>Used and declared (--> ok)</li><li>Used but are not declared in their own pom (--> nok)</li><li>Are not used but are declared (--> potentially remnant, clean up)</li></ul></li></ul></div><footer class="wrapper post__footer"><p class="post__last-updated">This article was updated on November 3, 2022</p><ul class="post__tag"><li><a href="https://www.finecloud.ch/tags/java/">java</a></li><li><a href="https://www.finecloud.ch/tags/maven/">maven</a></li><li><a href="https://www.finecloud.ch/tags/softwareentwicklung/">software development</a></li></ul><div class="post__share"></div></footer></article><nav class="post__nav"><div class="post__nav-inner"><div class="post__nav-prev"><svg width="1.041em" height="0.416em" aria-hidden="true"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#arrow-prev"/></svg> <a href="https://www.finecloud.ch/threema-backup-to-nextcloud.html" class="post__nav-link" rel="prev"><span>Previous</span> Threema Safe backup to Nextcloud</a></div><div class="post__nav-next"><a href="https://www.finecloud.ch/opnsense-backup-to-nextcloud.html" class="post__nav-link" rel="next"><span>Next</span> Backup OPNsense to Nextcloud </a><svg width="1.041em" height="0.416em" aria-hidden="true"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#arrow-next"/></svg></div></div></nav><div class="post__related related"><div class="wrapper"><h2 class="h5 related__title">You should also read:</h2><article class="related__item"><div class="feed__meta"><time datetime="2022-09-23T07:41" class="feed__date">September 23, 2022</time></div><h3 class="h1"><a href="https://www.finecloud.ch/getting-stared-with-spring-framework.html">Getting stared with the Spring Framework</a></h3></article><article class="related__item"><div class="feed__meta"><time datetime="2022-06-22T07:51" class="feed__date">Juni 22, 2022</time></div><h3 class="h1"><a href="https://www.finecloud.ch/java-serverlet-entwickeln-mit-vscode.html">Java Serverlet mit Tomcat und Maven in VSCode</a></h3></article></div></div></main><footer class="footer"><div class="footer__copyright"><p>Powered by Publii</p></div><button onclick="backToTopFunction()" id="backToTop" class="footer__bttop" aria-label="Back to top" title="Back to top"><svg><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#toparrow"/></svg></button></footer></div><script>window.publiiThemeMenuConfig = {
mobileMenuMode: 'sidebar',
animationSpeed: 300,
submenuWidth: 'auto',
doubleClickTime: 500,
mobileMenuExpandableSubmenus: true,
relatedContainerForOverlayMenuSelector: '.top',
};</script><script defer="defer" src="https://www.finecloud.ch/assets/js/scripts.min.js?v=6ca8b60e6534a3888de1205e82df8528"></script><script>var images = document.querySelectorAll('img[loading]');
for (var i = 0; i < images.length; i++) {
if (images[i].complete) {
images[i].classList.add('is-loaded');
} else {
images[i].addEventListener('load', function () {
this.classList.add('is-loaded');
}, false);
}
}</script><script defer="defer" src="https://www.finecloud.ch/media/plugins/syntaxHighlighter/prism.js"></script></body></html>