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.plugin.MojoExecutionException;
22  import org.apache.maven.plugin.MojoFailureException;
23  import org.apache.maven.plugins.annotations.LifecyclePhase;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.apache.maven.plugins.annotations.ResolutionScope;
26  import org.owasp.dependencycheck.Engine;
27  import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
28  import org.owasp.dependencycheck.data.update.exception.UpdateException;
29  import org.owasp.dependencycheck.utils.Settings;
30  
31  /**
32   * Maven Plugin that checks the project dependencies to see if they have any
33   * known published vulnerabilities.
34   *
35   * @author Jeremy Long
36   */
37  @Mojo(
38          name = "update-only",
39          defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
40          threadSafe = false,
41          requiresDependencyResolution = ResolutionScope.NONE,
42          requiresOnline = true,
43          aggregator = true
44  )
45  public class UpdateMojo extends BaseDependencyCheckMojo {
46  
47      /**
48       * Returns false; this mojo cannot generate a report.
49       *
50       * @return <code>false</code>
51       */
52      @Override
53      public boolean canGenerateReport() {
54          return false;
55      }
56  
57      /**
58       * Executes the dependency-check engine on the project's dependencies and
59       * generates the report.
60       *
61       * @throws MojoExecutionException thrown if there is an exception executing
62       * the goal
63       * @throws MojoFailureException thrown if dependency-check is configured to
64       * fail the build
65       */
66      @Override
67      public void runCheck() throws MojoExecutionException, MojoFailureException {
68          Engine engine = null;
69          try {
70              engine = initializeEngine();
71              engine.doUpdates();
72          } catch (DatabaseException ex) {
73              if (getLog().isDebugEnabled()) {
74                  getLog().debug("Database connection error", ex);
75              }
76              final String msg = "An exception occurred connecting to the local database. Please see the log file for more details.";
77              if (this.isFailOnError()) {
78                  throw new MojoExecutionException(msg, ex);
79              }
80              getLog().error(msg);
81          } catch (UpdateException ex) {
82              final String msg = "An exception occurred while downloading updates. Please see the log file for more details.";
83              if (this.isFailOnError()) {
84                  throw new MojoExecutionException(msg, ex);
85              }
86              getLog().error(msg);
87          }
88          if (engine != null) {
89              engine.cleanup();
90          }
91          Settings.cleanup();
92      }
93  
94      /**
95       * Returns the report name.
96       *
97       * @param locale the location
98       * @return the report name
99       */
100     @Override
101     public String getName(Locale locale) {
102         return "dependency-check-update";
103     }
104 
105     /**
106      * Gets the description of the Dependency-Check report to be displayed in
107      * the Maven Generated Reports page.
108      *
109      * @param locale The Locale to get the description for
110      * @return the description
111      */
112     @Override
113     public String getDescription(Locale locale) {
114         return "Updates the local cache of the NVD data from NIST.";
115     }
116 }