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