-
Notifications
You must be signed in to change notification settings - Fork 239
/
maven-in-five-minutes.apt
294 lines (216 loc) · 10.4 KB
/
maven-in-five-minutes.apt
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
----
Maven in 5 Minutes
-----
Eric Redmond
-----
2008-01-01
-----
~~ Licensed to the Apache Software Foundation (ASF) under one
~~ or more contributor license agreements. See the NOTICE file
~~ distributed with this work for additional information
~~ regarding copyright ownership. The ASF licenses this file
~~ to you under the Apache License, Version 2.0 (the
~~ "License"); you may not use this file except in compliance
~~ with the License. You may obtain a copy of the License at
~~
~~ http://www.apache.org/licenses/LICENSE-2.0
~~
~~ Unless required by applicable law or agreed to in writing,
~~ software distributed under the License is distributed on an
~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~~ KIND, either express or implied. See the License for the
~~ specific language governing permissions and limitations
~~ under the License.
~~ NOTE: For help with the syntax of this file, see:
~~ http://maven.apache.org/doxia/references/apt-format.html
Maven in 5 Minutes
* Prerequisites
You must understand how to install software on your computer.
If you do not know how to do this, please ask someone at your office, school, etc.
or pay someone to explain this to you. The Maven mailing lists are not the best place to ask for this advice.
* Installation
<Maven is a Java tool, so you must have {{{https://www.oracle.com/technetwork/java/javase/downloads/index.html}Java}}
installed in order to proceed.>
First, {{{../../download.html}download Maven}} and follow the {{{../../install.html}installation instructions}}.
After that, type the following in a terminal or in a command prompt:
-----
mvn --version
-----
It should print out your installed version of Maven, for example:
-----
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: D:\apache-maven-3.6.3\apache-maven\bin\..
Java version: 1.8.0_232, vendor: AdoptOpenJDK, runtime: C:\Program Files\AdoptOpenJDK\jdk-8.0.232.09-hotspot\jre
Default locale: en_US, platform encoding: Cp1250
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
-----
Depending upon your network setup, you may require extra configuration. Check out the
{{{../mini/guide-configuring-maven.html}Guide to Configuring Maven}} if necessary.
<<If you are using Windows, you should look at>> {{{./windows-prerequisites.html}Windows Prerequisites}}
<<to ensure that you are prepared to use Maven on Windows.>>
* Creating a Project
You need somewhere for your project to reside. Create a directory somewhere and start a shell in that directory.
On your command line, execute the following Maven goal:
-----
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
-----
<If you have just installed Maven, it may take a while on the first run. This is because Maven is downloading
the most recent artifacts (plugin jars and other files) into your local repository. You may also need to
execute the command a couple of times before it succeeds. This is because the remote server may time out before
your downloads are complete. Don't worry, there are ways to fix that.>
You will notice that the <generate> goal created a directory with the same name given as the
artifactId. Change into that directory.
-----
cd my-app
-----
Under this directory you will notice the following
{{{../introduction/introduction-to-the-standard-directory-layout.html}standard project structure}}.
-----
my-app
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- mycompany
| `-- app
| `-- App.java
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java
-----
The <<<src/main/java>>> directory contains the project source code, the <<<src/test/java>>> directory contains
the test source, and the <<<pom.xml>>> file is the project's Project Object Model, or POM.
** The POM
The <<<pom.xml>>> file is the core of a project's configuration in Maven. It is a single configuration
file that contains the majority of information required to build a project in just the way you want.
The POM is huge and can be daunting in its complexity, but it is not necessary to understand all
of the intricacies just yet to use it effectively. This project's POM is:
+-----+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
+-----+
** What did I just do?
You executed the Maven goal <archetype:generate>, and passed in various parameters to that goal.
The prefix <archetype> is the {{{../../plugins/index.html}plugin}} that provides the goal. If you are familiar with
{{{http://ant.apache.org}Ant}}, you
may conceive of this as similar to a task. This <archetype:generate> goal created a simple project based upon
a {{{/archetypes/maven-archetype-quickstart/}maven-archetype-quickstart}} archetype.
Suffice it to say for now that a <plugin> is a collection
of <goals> with a general common purpose. For example the jboss-maven-plugin, whose purpose is "deal with
various jboss items".
** Build the Project
-----
mvn package
-----
The command line will print out various actions, and end with the following:
-----
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.953 s
[INFO] Finished at: 2019-11-24T13:05:10+01:00
[INFO] ------------------------------------------------------------------------
-----
Unlike the first command executed (<archetype:generate>), the second is simply
a single word - <package>. Rather than a <goal>, this is a <phase>. A phase is a step in the
{{{../introduction/introduction-to-the-lifecycle.html}build lifecycle}}, which is an ordered
sequence of phases. When a phase is given, Maven executes every phase in the sequence
up to and including the one defined. For example, if you execute the <compile> phase, the
phases that actually get executed are:
[[1]] validate
[[2]] generate-sources
[[3]] process-sources
[[4]] generate-resources
[[5]] process-resources
[[6]] compile
[]
You may test the newly compiled and packaged JAR with the following command:
-----
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
-----
Which will print the quintessential:
-----
Hello World!
-----
* Java 9 or later
By default your version of Maven might use an old version of the <<<maven-compiler-plugin>>>
that is not compatible with Java 9 or later versions. To target Java 9 or later,
you should at least use version 3.6.0 of the <<<maven-compiler-plugin>>> and set the
<<<maven.compiler.release>>> property to the Java release you are targetting (e.g. 9, 10, 11, 12, etc.).
In the following example, we have configured our Maven project to use version 3.8.1 of <<<maven-compiler-plugin>>>
and target Java 11:
+-----+
<properties>
<maven.compiler.release>11</maven.compiler.release>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
+-----+
To learn more about <<<javac>>>'s <<<--release>>> option, see {{{https://openjdk.java.net/jeps/247}JEP 247}}.
* Running Maven Tools
** Maven Phases
Although hardly a comprehensive list, these are the most common <default> lifecycle phases executed.
* <<validate>>: validate the project is correct and all necessary information is available
* <<compile>>: compile the source code of the project
* <<test>>: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
* <<package>>: take the compiled code and package it in its distributable format, such as a JAR.
* <<integration-test>>: process and deploy the package if necessary into an environment where integration tests can be run
* <<verify>>: run any checks to verify the package is valid and meets quality criteria
* <<install>>: install the package into the local repository, for use as a dependency in other projects locally
* <<deploy>>: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
[]
There are two other Maven lifecycles of note beyond the <default> list above. They are
* <<clean>>: cleans up artifacts created by prior builds
[]
* <<site>>: generates site documentation for this project
[]
Phases are actually mapped to underlying goals. The specific goals executed per phase is dependant upon the
packaging type of the project. For example, <package> executes <jar:jar> if the project type is a JAR, and
<war:war> if the project type is - you guessed it - a WAR.
An interesting thing to note is that phases and goals may be executed in sequence.
-----
mvn clean dependency:copy-dependencies package
-----
This command will clean the project, copy dependencies, and package the project (executing all phases up to
<package>, of course).
** Generating the Site
-----
mvn site
-----
This phase generates a site based upon information on the project's pom. You can look at the
documentation generated under <<<target/site>>>.
* Conclusion
We hope this quick overview has piqued your interest in the versatility of Maven. Note that this is a very
truncated quick-start guide. Now you are ready for more comprehensive details concerning
the actions you have just performed. Check out the {{{./index.html}Maven Getting Started Guide}}.