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   *
17   * @since 0.3
18   */
19  public final class SnapshotsValidator implements MavenValidator {
20  
21      /**
22       * Default constructor.
23       */
24      public SnapshotsValidator() {
25          // nothing to initialize
26      }
27  
28      @Override
29      public void validate(final MavenEnvironment env)
30          throws ValidationException {
31          if (!env.exclude("snapshots", "")
32              && !SnapshotsValidator.isSnapshot(env.project().getVersion())) {
33              this.check(env);
34          }
35      }
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                  String.format("%d dependencies are in SNAPSHOT state", errors)
77              );
78          }
79      }
80  
81      private static boolean isSnapshot(final String version) {
82          return version.endsWith("-SNAPSHOT");
83      }
84  }