View Javadoc
1   /*
2    * This file is part of dependency-check-maven.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.maven;
19  
20  import java.util.Locale;
21  import org.apache.maven.artifact.Artifact;
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.apache.maven.plugins.annotations.LifecyclePhase;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.plugins.annotations.ResolutionScope;
28  import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
29  import org.owasp.dependencycheck.exception.ExceptionCollection;
30  import org.owasp.dependencycheck.exception.ReportException;
31  import org.owasp.dependencycheck.utils.Settings;
32  
33  /**
34   * Maven Plugin that checks the project dependencies to see if they have any
35   * known published vulnerabilities.
36   *
37   * @author Jeremy Long
38   */
39  @Mojo(
40          name = "check",
41          defaultPhase = LifecyclePhase.VERIFY,
42          threadSafe = false,
43          requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
44          requiresOnline = true
45  )
46  public class CheckMojo extends BaseDependencyCheckMojo {
47  
48      /**
49       * Returns whether or not a the report can be generated.
50       *
51       * @return <code>true</code> if the report can be generated; otherwise
52       * <code>false</code>
53       */
54      @Override
55      public boolean canGenerateReport() {
56          boolean isCapable = false;
57          for (Artifact a : getProject().getArtifacts()) {
58              if (!excludeFromScan(a.getScope())) {
59                  isCapable = true;
60                  break;
61              }
62          }
63          return isCapable;
64      }
65  
66      /**
67       * Executes the dependency-check engine on the project's dependencies and
68       * generates the report.
69       *
70       * @throws MojoExecutionException thrown if there is an exception executing
71       * the goal
72       * @throws MojoFailureException thrown if dependency-check is configured to
73       * fail the build
74       */
75      @Override
76      public void runCheck() throws MojoExecutionException, MojoFailureException {
77          MavenEngine engine = null;
78          try {
79              engine = initializeEngine();
80          } catch (DatabaseException ex) {
81              if (getLog().isDebugEnabled()) {
82                  getLog().debug("Database connection error", ex);
83              }
84              final String msg = "An exception occured connecting to the local database. Please see the log file for more details.";
85              if (this.isFailOnError()) {
86                  throw new MojoExecutionException(msg, ex);
87              }
88              getLog().error(msg);
89          }
90          if (engine != null) {
91              ExceptionCollection exCol = scanArtifacts(getProject(), engine);
92              if (engine.getDependencies().isEmpty()) {
93                  getLog().info("No dependencies were identified that could be analyzed by dependency-check");
94              } else {
95                  try {
96                      engine.analyzeDependencies();
97                  } catch (ExceptionCollection ex) {
98                      if (this.isFailOnError() && ex.isFatal()) {
99                          throw new MojoExecutionException("One or more exceptions occured during analysis", ex);
100                     }
101                     exCol = ex;
102                 }
103                 if (exCol == null || !exCol.isFatal()) {
104                     try {
105                         writeReports(engine, getProject(), getCorrectOutputDirectory());
106                     } catch (ReportException ex) {
107                         if (this.isFailOnError()) {
108                             if (exCol != null) {
109                                 exCol.addException(ex);
110                             } else {
111                                 exCol = new ExceptionCollection("Unable to write the dependency-check report", ex);
112                             }
113                         }
114                     }
115                     writeDataFile(getProject(), null, engine.getDependencies());
116                     showSummary(getProject(), engine.getDependencies());
117                     checkForFailure(engine.getDependencies());
118                     if (exCol != null && this.isFailOnError()) {
119                         throw new MojoExecutionException("One or more exceptions occured during dependency-check analysis", exCol);
120                     }
121                 }
122             }
123             engine.cleanup();
124         }
125         Settings.cleanup();
126     }
127 
128     /**
129      * The name of the report in the site.
130      */
131     @SuppressWarnings("CanBeFinal")
132     @Parameter(property = "name", defaultValue = "dependency-check", required = true)
133     private String name = "dependency-check";
134 
135     /**
136      * Returns the report name.
137      *
138      * @param locale the location
139      * @return the report name
140      */
141     @Override
142     public String getName(Locale locale) {
143         return name;
144     }
145 
146     /**
147      * Gets the description of the Dependency-Check report to be displayed in
148      * the Maven Generated Reports page.
149      *
150      * @param locale The Locale to get the description for
151      * @return the description
152      */
153     @Override
154     public String getDescription(Locale locale) {
155         return "Generates a report providing details on any published vulnerabilities within project dependencies. "
156                 + "This report is a best effort and may contain false positives and false negatives.";
157     }
158 
159 }