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