Coverage Report - org.owasp.dependencycheck.maven.CheckMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckMojo
0%
0/46
0%
0/28
6
 
 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  0
 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  0
         boolean isCapable = false;
 57  0
         for (Artifact a : getProject().getArtifacts()) {
 58  0
             if (!excludeFromScan(a.getScope())) {
 59  0
                 isCapable = true;
 60  0
                 break;
 61  
             }
 62  0
         }
 63  0
         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  0
         MavenEngine engine = null;
 78  
         try {
 79  0
             engine = initializeEngine();
 80  0
         } catch (DatabaseException ex) {
 81  0
             if (getLog().isDebugEnabled()) {
 82  0
                 getLog().debug("Database connection error", ex);
 83  
             }
 84  0
             final String msg = "An exception occurred connecting to the local database. Please see the log file for more details.";
 85  0
             if (this.isFailOnError()) {
 86  0
                 throw new MojoExecutionException(msg, ex);
 87  
             }
 88  0
             getLog().error(msg);
 89  0
         }
 90  0
         if (engine != null) {
 91  0
             ExceptionCollection exCol = scanArtifacts(getProject(), engine);
 92  0
             if (engine.getDependencies().isEmpty()) {
 93  0
                 getLog().info("No dependencies were identified that could be analyzed by dependency-check");
 94  
             } else {
 95  
                 try {
 96  0
                     engine.analyzeDependencies();
 97  0
                 } catch (ExceptionCollection ex) {
 98  0
                     if (this.isFailOnError() && ex.isFatal()) {
 99  0
                         throw new MojoExecutionException("One or more exceptions occurred during analysis", ex);
 100  
                     }
 101  0
                     exCol = ex;
 102  0
                 }
 103  0
                 if (exCol == null || !exCol.isFatal()) {
 104  
                     try {
 105  0
                         writeReports(engine, getProject(), getCorrectOutputDirectory());
 106  0
                     } catch (ReportException ex) {
 107  0
                         if (this.isFailOnError()) {
 108  0
                             if (exCol != null) {
 109  0
                                 exCol.addException(ex);
 110  
                             } else {
 111  0
                                 exCol = new ExceptionCollection("Unable to write the dependency-check report", ex);
 112  
                             }
 113  
                         }
 114  0
                     }
 115  
                     //writeDataFile(getProject(), null, engine.getDependencies());
 116  0
                     showSummary(getProject(), engine.getDependencies());
 117  0
                     checkForFailure(engine.getDependencies());
 118  0
                     if (exCol != null && this.isFailOnError()) {
 119  0
                         throw new MojoExecutionException("One or more exceptions occurred during dependency-check analysis", exCol);
 120  
                     }
 121  
                 }
 122  
             }
 123  0
             engine.cleanup();
 124  
         }
 125  0
         Settings.cleanup();
 126  0
     }
 127  
 
 128  
     /**
 129  
      * The name of the report in the site.
 130  
      */
 131  0
     @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  0
         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  0
         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  
 }