1
2
3
4
5 package com.qulice.maven;
6
7 import com.jcabi.log.Logger;
8 import com.qulice.spi.ValidationException;
9 import org.apache.maven.model.Dependency;
10 import org.apache.maven.model.Extension;
11 import org.apache.maven.model.Plugin;
12
13
14
15
16
17
18 public final class SnapshotsValidator implements MavenValidator {
19
20
21
22
23 public SnapshotsValidator() {
24
25 }
26
27 @Override
28 public void validate(final MavenEnvironment env)
29 throws ValidationException {
30 if (!env.exclude("snapshots", "")
31 && !SnapshotsValidator.isSnapshot(env.project().getVersion())) {
32 this.check(env);
33 }
34 }
35
36
37
38
39
40
41 private void check(final MavenEnvironment env) throws ValidationException {
42 int errors = 0;
43 for (final Extension ext : env.project().getBuildExtensions()) {
44 if (SnapshotsValidator.isSnapshot(ext.getVersion())) {
45 Logger.warn(
46 this,
47 "%s build extension is SNAPSHOT",
48 ext
49 );
50 ++errors;
51 }
52 }
53 for (final Plugin plugin : env.project().getBuildPlugins()) {
54 if (SnapshotsValidator.isSnapshot(plugin.getVersion())) {
55 Logger.warn(
56 this,
57 "%s build plugin is SNAPSHOT",
58 plugin
59 );
60 ++errors;
61 }
62 }
63 for (final Dependency dep : env.project().getDependencies()) {
64 if (SnapshotsValidator.isSnapshot(dep.getVersion())) {
65 Logger.warn(
66 this,
67 "%s dependency is SNAPSHOT",
68 dep
69 );
70 ++errors;
71 }
72 }
73 if (errors > 0) {
74 Logger.warn(
75 this,
76 "The version of the project is not SNAPSHOT; there shouldn't not be any SNAPSHOT dependencies (%d found)",
77 errors
78 );
79 throw new ValidationException(
80 String.format("%d dependencies are in SNAPSHOT state", errors)
81 );
82 }
83 }
84
85
86
87
88
89
90 private static boolean isSnapshot(final String version) {
91 return version.endsWith("-SNAPSHOT");
92 }
93 }