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.google.common.base.Predicate;
34  import com.google.common.base.Predicates;
35  import com.google.common.collect.Collections2;
36  import com.jcabi.log.Logger;
37  import com.qulice.spi.ValidationException;
38  import java.util.Collection;
39  import java.util.LinkedList;
40  import org.apache.maven.artifact.Artifact;
41  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
42  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzer;
43  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzerException;
44  import org.cactoos.text.Joined;
45  import org.codehaus.plexus.PlexusConstants;
46  import org.codehaus.plexus.PlexusContainer;
47  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
48  import org.codehaus.plexus.context.ContextException;
49  
50  /**
51   * Validator of dependencies.
52   *
53   * @since 0.3
54   * @checkstyle ReturnCountCheck (100 line)
55   */
56  final class DependenciesValidator implements MavenValidator {
57  
58      /**
59       * Separator between lines.
60       */
61      private static final String SEP = "\n\t";
62  
63      @Override
64      @SuppressWarnings("PMD.OnlyOneReturn")
65      public void validate(final MavenEnvironment env)
66          throws ValidationException {
67          if (!env.outdir().exists() || "pom".equals(env.project().getPackaging())) {
68              Logger.info(this, "No dependency analysis in this project");
69              return;
70          }
71          final Collection<String> excludes = env.excludes("dependencies");
72          if (excludes.contains(".*")) {
73              Logger.info(this, "Dependency analysis suppressed in the project via pom.xml");
74              return;
75          }
76          final Collection<String> unused = Collections2.filter(
77              DependenciesValidator.unused(env),
78              Predicates.not(new DependenciesValidator.ExcludePredicate(excludes))
79          );
80          if (!unused.isEmpty()) {
81              Logger.warn(
82                  this,
83                  "Unused declared dependencies found:%s%s",
84                  DependenciesValidator.SEP,
85                  new Joined(DependenciesValidator.SEP, unused).toString()
86              );
87          }
88          final Collection<String> used = Collections2.filter(
89              DependenciesValidator.used(env),
90              Predicates.not(new DependenciesValidator.ExcludePredicate(excludes))
91          );
92          if (!used.isEmpty()) {
93              Logger.warn(
94                  this,
95                  "Used undeclared dependencies found:%s%s",
96                  DependenciesValidator.SEP,
97                  new Joined(DependenciesValidator.SEP, used)
98              );
99          }
100         if (!used.isEmpty() || !unused.isEmpty()) {
101             Logger.info(
102                 this,
103                 "You can suppress this message by <exclude>dependencies:...</exclude> in pom.xml, where <...> is what the dependency name starts with (not a regular expression!)"
104             );
105         }
106         final int failures = used.size() + unused.size();
107         if (failures > 0) {
108             throw new ValidationException(
109                 "%d dependency problem(s) found",
110                 failures
111             );
112         }
113         Logger.info(this, "No dependency problems found");
114     }
115 
116     /**
117      * Analyze the project.
118      * @param env The environment
119      * @return The result of analysis
120      */
121     private static ProjectDependencyAnalysis analyze(
122         final MavenEnvironment env) {
123         try {
124             return ((ProjectDependencyAnalyzer)
125                 ((PlexusContainer)
126                     env.context().get(PlexusConstants.PLEXUS_KEY)
127                 ).lookup(ProjectDependencyAnalyzer.class.getName(), "default")
128             ).analyze(env.project());
129         } catch (final ContextException | ComponentLookupException
130             | ProjectDependencyAnalyzerException ex) {
131             throw new IllegalStateException(ex);
132         }
133     }
134 
135     /**
136      * Find unused artifacts.
137      * @param env Environment
138      * @return Collection of unused artifacts
139      */
140     private static Collection<String> used(final MavenEnvironment env) {
141         final ProjectDependencyAnalysis analysis =
142             DependenciesValidator.analyze(env);
143         final Collection<String> used = new LinkedList<>();
144         for (final Object artifact : analysis.getUsedUndeclaredArtifacts()) {
145             used.add(artifact.toString());
146         }
147         return used;
148     }
149 
150     /**
151      * Find unused artifacts.
152      * @param env Environment
153      * @return Collection of unused artifacts
154      */
155     private static Collection<String> unused(final MavenEnvironment env) {
156         final ProjectDependencyAnalysis analysis =
157             DependenciesValidator.analyze(env);
158         final Collection<String> unused = new LinkedList<>();
159         for (final Object obj : analysis.getUnusedDeclaredArtifacts()) {
160             final Artifact artifact = (Artifact) obj;
161             if (!Artifact.SCOPE_COMPILE.equals(artifact.getScope())) {
162                 continue;
163             }
164             unused.add(artifact.toString());
165         }
166         return unused;
167     }
168 
169     /**
170      * Predicate for excluded dependencies.
171      *
172      * @since 0.1
173      */
174     private static class ExcludePredicate implements Predicate<String> {
175 
176         /**
177          * List of excludes.
178          */
179         private final Collection<String> excludes;
180 
181         /**
182          * Constructor.
183          * @param excludes List of excludes.
184          */
185         ExcludePredicate(final Collection<String> excludes) {
186             this.excludes = excludes;
187         }
188 
189         @Override
190         public boolean apply(final String name) {
191             boolean ignore = false;
192             for (final String exclude : this.excludes) {
193                 if (name.startsWith(exclude)) {
194                     ignore = true;
195                     break;
196                 }
197             }
198             return ignore;
199         }
200     }
201 }