View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
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   * Check that the project has not SNAPSHOT dependencies if its own
15   * status is stable.
16   *
17   * @since 0.3
18   */
19  public final class SnapshotsValidator implements MavenValidator {
20  
21      @Override
22      public void validate(final MavenEnvironment env)
23          throws ValidationException {
24          if (!env.exclude("snapshots", "")) {
25              final String version = env.project().getVersion();
26              if (!SnapshotsValidator.isSnapshot(version)) {
27                  this.check(env);
28              }
29          }
30      }
31  
32      /**
33       * Check all plugins and deps.
34       * @param env Environment
35       * @throws ValidationException If fails
36       */
37      private void check(final MavenEnvironment env) throws ValidationException {
38          int errors = 0;
39          for (final Extension ext : env.project().getBuildExtensions()) {
40              if (SnapshotsValidator.isSnapshot(ext.getVersion())) {
41                  Logger.warn(
42                      this,
43                      "%s build extension is SNAPSHOT",
44                      ext
45                  );
46                  ++errors;
47              }
48          }
49          for (final Plugin plugin : env.project().getBuildPlugins()) {
50              if (SnapshotsValidator.isSnapshot(plugin.getVersion())) {
51                  Logger.warn(
52                      this,
53                      "%s build plugin is SNAPSHOT",
54                      plugin
55                  );
56                  ++errors;
57              }
58          }
59          for (final Dependency dep : env.project().getDependencies()) {
60              if (SnapshotsValidator.isSnapshot(dep.getVersion())) {
61                  Logger.warn(
62                      this,
63                      "%s dependency is SNAPSHOT",
64                      dep
65                  );
66                  ++errors;
67              }
68          }
69          if (errors > 0) {
70              Logger.warn(
71                  this,
72                  "The version of the project is not SNAPSHOT; there shouldn't not be any SNAPSHOT dependencies (%d found)",
73                  errors
74              );
75              throw new ValidationException(
76                  "%d dependencies are in SNAPSHOT state",
77                  errors
78              );
79          }
80      }
81  
82      /**
83       * Whether this version is a snapshot?
84       * @param version The version
85       * @return TRUE if yes
86       */
87      private static boolean isSnapshot(final String version) {
88          return version.endsWith("-SNAPSHOT");
89      }
90  
91  }