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      /**
21       * Default constructor.
22       */
23      public SnapshotsValidator() {
24          // nothing to initialize
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       * Check all plugins and deps.
38       * @param env Environment
39       * @throws ValidationException If fails
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       * Whether this version is a snapshot?
87       * @param version The version
88       * @return TRUE if yes
89       */
90      private static boolean isSnapshot(final String version) {
91          return version.endsWith("-SNAPSHOT");
92      }
93  }