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.Engine;
29  import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
30  import org.owasp.dependencycheck.exception.ExceptionCollection;
31  import org.owasp.dependencycheck.exception.ReportException;
32  import org.owasp.dependencycheck.utils.Settings;
33  
34  /**
35   * Maven Plugin that checks the project dependencies to see if they have any
36   * known published vulnerabilities.
37   *
38   * @author Jeremy Long
39   */
40  @Mojo(
41          name = "check",
42          defaultPhase = LifecyclePhase.VERIFY,
43          threadSafe = false,
44          requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
45          requiresOnline = true
46  )
47  public class CheckMojo extends BaseDependencyCheckMojo {
48  
49      /**
50       * Returns whether or not a the report can be generated.
51       *
52       * @return <code>true</code> if the report can be generated; otherwise
53       * <code>false</code>
54       */
55      @Override
56      public boolean canGenerateReport() {
57          boolean isCapable = false;
58          for (Artifact a : getProject().getArtifacts()) {
59              if (!excludeFromScan(a.getScope())) {
60                  isCapable = true;
61                  break;
62              }
63          }
64          return isCapable;
65      }
66  
67      /**
68       * Executes the dependency-check engine on the project's dependencies and
69       * generates the report.
70       *
71       * @throws MojoExecutionException thrown if there is an exception executing
72       * the goal
73       * @throws MojoFailureException thrown if dependency-check is configured to
74       * fail the build
75       */
76      @Override
77      public void runCheck() throws MojoExecutionException, MojoFailureException {
78          Engine engine = null;
79          try {
80              engine = initializeEngine();
81          } catch (DatabaseException ex) {
82              if (getLog().isDebugEnabled()) {
83                  getLog().debug("Database connection error", ex);
84              }
85              final String msg = "An exception occurred connecting to the local database. Please see the log file for more details.";
86              if (this.isFailOnError()) {
87                  throw new MojoExecutionException(msg, ex);
88              }
89              getLog().error(msg);
90          }
91          if (engine != null) {
92              ExceptionCollection exCol = scanArtifacts(getProject(), engine);
93              if (engine.getDependencies().isEmpty()) {
94                  getLog().info("No dependencies were identified that could be analyzed by dependency-check");
95              } else {
96                  try {
97                      engine.analyzeDependencies();
98                  } catch (ExceptionCollection ex) {
99                      if (this.isFailOnError() && ex.isFatal()) {
100                         throw new MojoExecutionException("One or more exceptions occurred during analysis", ex);
101                     }
102                     exCol = ex;
103                 }
104                 if (exCol == null || !exCol.isFatal()) {
105                     try {
106                         writeReports(engine, getProject(), getCorrectOutputDirectory());
107                     } catch (ReportException ex) {
108                         if (this.isFailOnError()) {
109                             if (exCol != null) {
110                                 exCol.addException(ex);
111                             } else {
112                                 exCol = new ExceptionCollection("Unable to write the dependency-check report", ex);
113                             }
114                         }
115                     }
116                     //writeDataFile(getProject(), null, engine.getDependencies());
117                     showSummary(getProject(), engine.getDependencies());
118                     checkForFailure(engine.getDependencies());
119                     if (exCol != null && this.isFailOnError()) {
120                         throw new MojoExecutionException("One or more exceptions occurred during dependency-check analysis", exCol);
121                     }
122                 }
123             }
124             engine.cleanup();
125         }
126         Settings.cleanup();
127     }
128 
129     /**
130      * The name of the report in the site.
131      */
132     @SuppressWarnings("CanBeFinal")
133     @Parameter(property = "name", defaultValue = "dependency-check", required = true)
134     private String name = "dependency-check";
135 
136     /**
137      * Returns the report name.
138      *
139      * @param locale the location
140      * @return the report name
141      */
142     @Override
143     public String getName(Locale locale) {
144         return name;
145     }
146 
147     /**
148      * Gets the description of the Dependency-Check report to be displayed in
149      * the Maven Generated Reports page.
150      *
151      * @param locale The Locale to get the description for
152      * @return the description
153      */
154     @Override
155     public String getDescription(Locale locale) {
156         return "Generates a report providing details on any published vulnerabilities within project dependencies. "
157                 + "This report is a best effort and may contain false positives and false negatives.";
158     }
159 
160 }