-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy path110-java-maven-best-practices.xml
More file actions
601 lines (566 loc) · 22.2 KB
/
Copy path110-java-maven-best-practices.xml
File metadata and controls
601 lines (566 loc) · 22.2 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
<?xml version="1.0" encoding="UTF-8"?>
<system-prompt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="system-prompt.xsd"
id="110-java-maven-best-practices" version="1.0">
<metadata>
<description>Maven Best Practices</description>
<globs>pom.xml</globs>
<always-apply>false</always-apply>
<tags>
<tag>maven</tag>
<tag>java</tag>
<tag>best-practices</tag>
</tags>
<version>0.8.0</version>
</metadata>
<header>
<title>Maven Best Practices</title>
</header>
<system-characterization>
<role-definition>You are a Senior software engineer with extensive experience in Java software development</role-definition>
</system-characterization>
<description>
Effective Maven usage involves robust dependency management via `<dependencyManagement>` and BOMs, adherence to the standard directory layout, and centralized plugin management. Build profiles should be used for environment-specific configurations. POMs must be kept readable and maintainable with logical structure and properties for versions. Custom repositories should be declared explicitly and their use minimized, preferably managed via a central repository manager.
</description>
<table-of-contents>
<toc-item>Rule 1: Effective Dependency Management</toc-item>
<toc-item>Rule 2: Standard Directory Layout</toc-item>
<toc-item>Rule 3: Plugin Management and Configuration</toc-item>
<toc-item>Rule 4: Use Build Profiles for Environment-Specific Configurations</toc-item>
<toc-item>Rule 5: Keep POMs Readable and Maintainable</toc-item>
<toc-item>Rule 6: Manage Repositories Explicitly</toc-item>
<toc-item>Rule 7: Centralize Version Management with Properties</toc-item>
</table-of-contents>
<content-sections>
<rule-section number="1" id="effective-dependency-management">
<rule-header>
<rule-title>Effective Dependency Management</rule-title>
<rule-subtitle>Manage Dependencies Effectively using `dependencyManagement` and BOMs</rule-subtitle>
</rule-header>
<rule-description>
Use the `<dependencyManagement>` section in parent POMs or import Bill of Materials (BOMs) to centralize and control dependency versions. This helps avoid version conflicts and ensures consistency across multi-module projects. Avoid hardcoding versions directly in `<dependencies>` when managed elsewhere.
</rule-description>
<code-examples>
<good-example>
<code-block language="xml"><![CDATA[<!-- Parent POM -->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<properties>
<spring.version>5.3.23</spring.version>
<junit.version>5.9.0</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Import a BOM -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
<!-- Child POM -->
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>my-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>my-module</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<!-- Version is inherited from parent's dependencyManagement -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<!-- Version and scope inherited -->
</dependency>
</dependencies>
</project>]]></code-block>
</good-example>
<bad-example>
<code-block language="xml"><![CDATA[<!-- Child POM hardcoding versions -->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-other-module</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version> <!-- Hardcoded, may differ from parent's intention -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version> <!-- Different version, potential conflict -->
<scope>test</scope>
</dependency>
</dependencies>
</project>]]></code-block>
</bad-example>
</code-examples>
</rule-section>
<rule-section number="2" id="standard-directory-layout">
<rule-header>
<rule-title>Standard Directory Layout</rule-title>
<rule-subtitle>Adhere to the Standard Directory Layout</rule-subtitle>
</rule-header>
<rule-description>
Follow Maven's convention for directory structure (`src/main/java`, `src/main/resources`, `src/test/java`, `src/test/resources`, etc.). This makes projects easier to understand and build, as Maven relies on these defaults.
</rule-description>
<code-examples>
<good-example>
<code-block>my-app/
├── pom.xml
└── src/
├── main/
│ ├── java/
│ │ └── com/example/myapp/App.java
│ └── resources/
│ └── application.properties
└── test/
├── java/
│ └── com/example/myapp/AppTest.java
└── resources/
└── test-data.xml</code-block>
</good-example>
<bad-example>
<code-block>my-app/
├── pom.xml
├── sources/ <!-- Non-standard -->
│ └── com/example/myapp/App.java
├── res/ <!-- Non-standard -->
│ └── config.properties
└── tests/ <!-- Non-standard -->
└── com/example/myapp/AppTest.java
<!-- This would require explicit configuration in pom.xml to specify source/resource directories --></code-block>
</bad-example>
</code-examples>
</rule-section>
<rule-section number="3" id="plugin-management">
<rule-header>
<rule-title>Plugin Management and Configuration</rule-title>
<rule-subtitle>Manage Plugin Versions and Configurations Centrally</rule-subtitle>
</rule-header>
<rule-description>
Use `<pluginManagement>` in a parent POM to define plugin versions and common configurations. Child POMs can then use the plugins without specifying versions, ensuring consistency. Override configurations in child POMs only when necessary.
</rule-description>
<code-examples>
<good-example>
<code-block language="xml"><![CDATA[<!-- Parent POM -->
<project>
<!-- ... -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
<!-- Child POM -->
<project>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- Version and basic configuration inherited -->
</plugin>
</plugins>
</build>
</project>]]></code-block>
</good-example>
<bad-example>
<code-block language="xml"><![CDATA[<!-- Child POM -->
<project>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <!-- Different version, potentially older/incompatible -->
<configuration>
<source>11</source> <!-- Different configuration -->
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>]]></code-block>
</bad-example>
</code-examples>
</rule-section>
<rule-section number="4" id="build-profiles">
<rule-header>
<rule-title>Use Build Profiles for Environment-Specific Configurations</rule-title>
<rule-subtitle>Employ Build Profiles for Environment-Specific Settings</rule-subtitle>
</rule-header>
<rule-description>
Use Maven profiles to customize build settings for different environments (e.g., dev, test, prod) or other conditional scenarios. This can include different dependencies, plugin configurations, or properties. Activate profiles via command line, OS, JDK, or file presence.
</rule-description>
<code-examples>
<good-example>
<code-block language="xml"><![CDATA[
<project>
<!-- ... -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<database.url>jdbc:h2:mem:devdb</database.url>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<database.url>jdbc:postgresql://prodserver/mydb</database.url>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>run</goal></goals>
<configuration>
<target>
<!-- Minify JS/CSS for prod -->
<echo>Simulating minification for prod</echo>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
<!-- Activation: mvn clean install -P prod -->
]]></code-block>
</good-example>
<bad-example>
<code-block language="xml"><![CDATA[
<!-- Commented out sections for different environments -->
<project>
<!-- ... -->
<properties>
<!-- <database.url>jdbc:h2:mem:devdb</database.url> -->
<database.url>jdbc:postgresql://prodserver/mydb</database.url> <!-- Manually switch by commenting/uncommenting -->
</properties>
</project>]]></code-block>
</bad-example>
</code-examples>
</rule-section>
<rule-section number="5" id="readable-poms">
<rule-header>
<rule-title>Keep POMs Readable and Maintainable</rule-title>
<rule-subtitle>Structure POMs Logically for Readability</rule-subtitle>
</rule-header>
<rule-description>
Organize your `pom.xml` sections in a consistent order (e.g., project coordinates, parent, properties, dependencyManagement, dependencies, build, profiles, repositories). Use properties for recurring versions or values. Add comments for complex configurations.
</rule-description>
<code-examples>
<good-example>
<code-block language="xml"><![CDATA[
<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>
<!-- Project Coordinates -->
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>My Application</name>
<description>A sample application.</description>
<!-- Parent (if any) -->
<!-- ... -->
<!-- Properties -->
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<some.library.version>2.5.1</some.library.version>
</properties>
<!-- Dependency Management -->
<dependencyManagement>
<!-- ... -->
</dependencyManagement>
<!-- Dependencies -->
<dependencies>
<dependency>
<groupId>org.some.library</groupId>
<artifactId>some-library-core</artifactId>
<version>${some.library.version}</version>
</dependency>
<!-- ... -->
</dependencies>
<!-- Build Configuration -->
<build>
<!-- ... -->
</build>
<!-- Profiles (if any) -->
<!-- ... -->
<!-- Repositories and Plugin Repositories (if needed) -->
<!-- ... -->
</project>]]></code-block>
</good-example>
<bad-example>
<code-block language="xml"><![CDATA[
<!-- Haphazard order, missing properties for versions -->
<project>
<dependencies>
<dependency>
<groupId>org.some.library</groupId>
<artifactId>some-library-core</artifactId>
<version>2.5.1</version> <!-- Version hardcoded, repeated elsewhere -->
</dependency>
</dependencies>
<modelVersion>4.0.0</modelVersion>
<build>
<!-- ... -->
</build>
<groupId>com.example</groupId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<artifactId>my-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
</project>]]></code-block>
</bad-example>
</code-examples>
</rule-section>
<rule-section number="6" id="manage-repositories">
<rule-header>
<rule-title>Manage Repositories Explicitly</rule-title>
<rule-subtitle>Declare Custom Repositories Explicitly and Minimize Their Use</rule-subtitle>
</rule-header>
<rule-description>
Prefer dependencies from Maven Central. If custom repositories are necessary, declare them in the `<repositories>` section and `<pluginRepositories>` for plugins. It's often better to manage these in a company-wide Nexus/Artifactory instance configured in `settings.xml` rather than per-project POMs. Avoid relying on transitive repositories.
</rule-description>
<code-examples>
<good-example>
<code-block language="xml"><![CDATA[
<project>
<!-- ... -->
<repositories>
<repository>
<id>my-internal-repo</id>
<url>https://nexus.example.com/repository/maven-releases/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>my-internal-plugins</id>
<url>https://nexus.example.com/repository/maven-plugins/</url>
</pluginRepository>
</pluginRepositories>
</project>
<!-- Better: Configure these in settings.xml and use a repository manager -->]]></code-block>
</good-example>
<bad-example>
<code-block language="xml"><![CDATA[
<!-- No explicit repository for a non-central artifact, relying on developer's local settings or transitive ones -->
<project>
<!-- ... -->
<dependencies>
<dependency>
<groupId>com.internal.stuff</groupId>
<artifactId>internal-lib</artifactId>
<version>1.0</version>
<!-- If this is not in Maven Central, the build will fail unless
the repository is configured in settings.xml or a parent POM.
Relying on implicit configurations makes builds less portable. -->
</dependency>
</dependencies>
</project>]]></code-block>
</bad-example>
</code-examples>
</rule-section>
<rule-section number="7" id="centralize-version-management">
<rule-header>
<rule-title>Centralize Version Management with Properties</rule-title>
<rule-subtitle>Use Properties to Manage Dependency and Plugin Versions</rule-subtitle>
</rule-header>
<rule-description>
Define all dependency and plugin versions in the `<properties>` section rather than hardcoding them throughout the POM. This centralizes version management, makes updates easier, reduces duplication, and helps maintain consistency across related dependencies. Use consistent property naming conventions: `maven-plugin-[name].version` for Maven plugins, simple names like `[library].version` for dependencies, and descriptive names for quality thresholds like `coverage.level`.
</rule-description>
<code-examples>
<good-example>
<code-block language="xml"><![CDATA[
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<properties>
<!-- Core build properties -->
<java.version>17</java.version>
<maven.version>3.9.10</maven.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Dependency versions -->
<jackson.version>2.15.3</jackson.version>
<junit.version>5.10.1</junit.version>
<mockito.version>5.7.0</mockito.version>
<logback.version>1.4.11</logback.version>
<!-- Maven plugin versions -->
<maven-plugin-compiler.version>3.14.0</maven-plugin-compiler.version>
<maven-plugin-surefire.version>3.5.3</maven-plugin-surefire.version>
<maven-plugin-failsafe.version>3.5.3</maven-plugin-failsafe.version>
<maven-plugin-enforcer.version>3.5.0</maven-plugin-enforcer.version>
<!-- Third-party plugin versions -->
<maven-plugin-jacoco.version>0.8.13</maven-plugin-jacoco.version>
<!-- Quality thresholds -->
<coverage.level>80</coverage.level>
<mutation.level>70</mutation.level>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-plugin-compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-plugin-surefire.version}</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${maven-plugin-jacoco.version}</version>
</plugin>
</plugins>
</build>
</project>]]></code-block>
</good-example>
<bad-example>
<code-block language="xml"><![CDATA[<!-- Hardcoded versions scattered throughout the POM -->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.3</version> <!-- Hardcoded version -->
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.15.2</version> <!-- Different version of same library family! -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.1</version> <!-- Hardcoded version -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.3</version> <!-- Different JUnit version! -->
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version> <!-- Hardcoded plugin version -->
<configuration>
<source>17</source> <!-- Hardcoded Java version instead of using property -->
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version> <!-- Hardcoded plugin version -->
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version> <!-- Hardcoded and potentially outdated -->
</plugin>
</plugins>
</build>
</project>]]></code-block>
</bad-example>
</code-examples>
</rule-section>
</content-sections>
</system-prompt>