View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.maven;
32  
33  import com.jcabi.log.Logger;
34  import com.qulice.spi.ValidationException;
35  import org.apache.maven.model.Dependency;
36  import org.apache.maven.model.Extension;
37  import org.apache.maven.model.Plugin;
38  
39  /**
40   * Check that the project has not SNAPSHOT dependencies if its own
41   * status is stable.
42   *
43   * @since 0.3
44   */
45  public final class SnapshotsValidator implements MavenValidator {
46  
47      @Override
48      public void validate(final MavenEnvironment env)
49          throws ValidationException {
50          if (!env.exclude("snapshots", "")) {
51              final String version = env.project().getVersion();
52              if (!SnapshotsValidator.isSnapshot(version)) {
53                  this.check(env);
54              }
55          }
56      }
57  
58      /**
59       * Check all plugins and deps.
60       * @param env Environment
61       * @throws ValidationException If fails
62       */
63      private void check(final MavenEnvironment env) throws ValidationException {
64          int errors = 0;
65          for (final Extension ext : env.project().getBuildExtensions()) {
66              if (SnapshotsValidator.isSnapshot(ext.getVersion())) {
67                  Logger.warn(
68                      this,
69                      "%s build extension is SNAPSHOT",
70                      ext
71                  );
72                  ++errors;
73              }
74          }
75          for (final Plugin plugin : env.project().getBuildPlugins()) {
76              if (SnapshotsValidator.isSnapshot(plugin.getVersion())) {
77                  Logger.warn(
78                      this,
79                      "%s build plugin is SNAPSHOT",
80                      plugin
81                  );
82                  ++errors;
83              }
84          }
85          for (final Dependency dep : env.project().getDependencies()) {
86              if (SnapshotsValidator.isSnapshot(dep.getVersion())) {
87                  Logger.warn(
88                      this,
89                      "%s dependency is SNAPSHOT",
90                      dep
91                  );
92                  ++errors;
93              }
94          }
95          if (errors > 0) {
96              Logger.warn(
97                  this,
98                  "The version of the project is not SNAPSHOT; there shouldn't not be any SNAPSHOT dependencies (%d found)",
99                  errors
100             );
101             throw new ValidationException(
102                 "%d dependencies are in SNAPSHOT state",
103                 errors
104             );
105         }
106     }
107 
108     /**
109      * Whether this version is a snapshot?
110      * @param version The version
111      * @return TRUE if yes
112      */
113     private static boolean isSnapshot(final String version) {
114         return version.endsWith("-SNAPSHOT");
115     }
116 
117 }