PurgeMojo.java

  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) 2015 Jeremy Long. All Rights Reserved.
  17.  */
  18. package org.owasp.dependencycheck.maven;

  19. import java.util.Locale;
  20. import org.apache.maven.plugin.MojoExecutionException;
  21. import org.apache.maven.plugin.MojoFailureException;
  22. import org.apache.maven.plugins.annotations.LifecyclePhase;
  23. import org.apache.maven.plugins.annotations.Mojo;
  24. import org.apache.maven.plugins.annotations.ResolutionScope;
  25. import org.owasp.dependencycheck.Engine;
  26. import org.owasp.dependencycheck.exception.ExceptionCollection;
  27. import org.owasp.dependencycheck.utils.Downloader;
  28. import org.owasp.dependencycheck.utils.InvalidSettingException;

  29. /**
  30.  * Maven Plugin that purges the local copy of the NVD data.
  31.  *
  32.  * @author Jeremy Long
  33.  */
  34. @Mojo(
  35.         name = "purge",
  36.         defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
  37.         requiresProject = false,
  38.         threadSafe = true,
  39.         requiresDependencyResolution = ResolutionScope.NONE,
  40.         requiresOnline = true,
  41.         aggregator = true
  42. )
  43. public class PurgeMojo extends BaseDependencyCheckMojo {

  44.     /**
  45.      * Returns false; this mojo cannot generate a report.
  46.      *
  47.      * @return <code>false</code>
  48.      */
  49.     @Override
  50.     public boolean canGenerateReport() {
  51.         return false;
  52.     }

  53.     /**
  54.      * Purges the local copy of the NVD.
  55.      *
  56.      * @throws MojoExecutionException thrown if there is an exception executing
  57.      * the goal
  58.      * @throws MojoFailureException thrown if dependency-check is configured to
  59.      * fail the build
  60.      */
  61.     @Override
  62.     protected void runCheck() throws MojoExecutionException, MojoFailureException {
  63.         populateSettings();
  64.         try {
  65.             Downloader.getInstance().configure(getSettings());
  66.         } catch (InvalidSettingException e) {
  67.             if (isFailOnError()) {
  68.                 throw new MojoFailureException(e.getMessage(), e);
  69.             } else {
  70.                 throw new MojoExecutionException(e.getMessage(), e);
  71.             }
  72.         }
  73.         try (Engine engine = new Engine(Engine.Mode.EVIDENCE_PROCESSING, getSettings())) {
  74.             engine.purge();
  75.         } finally {
  76.             getSettings().cleanup();
  77.         }
  78.     }

  79.     /**
  80.      * Returns the report name.
  81.      *
  82.      * @param locale the location
  83.      * @return the report name
  84.      */
  85.     @Override
  86.     public String getName(Locale locale) {
  87.         return "dependency-check-purge";
  88.     }

  89.     /**
  90.      * Gets the description of the Dependency-Check report to be displayed in
  91.      * the Maven Generated Reports page.
  92.      *
  93.      * @param locale The Locale to get the description for
  94.      * @return the description
  95.      */
  96.     @Override
  97.     public String getDescription(Locale locale) {
  98.         return "Purges the local cache of the NVD dataT.";
  99.     }

  100.     /**
  101.      * Throws an exception if called. The purge mojo does not scan dependencies.
  102.      *
  103.      * @param engine the engine used to scan
  104.      * @return a collection of exceptions
  105.      * @throws MojoExecutionException thrown if there is an exception
  106.      */
  107.     @Override
  108.     protected ExceptionCollection scanDependencies(Engine engine) throws MojoExecutionException {
  109.         throw new UnsupportedOperationException("Operation not supported");
  110.     }

  111.     /**
  112.      * Throws an exception if called. The purge mojo does not scan dependencies.
  113.      *
  114.      * @param engine the engine used to scan
  115.      * @param exCollection the collection of exceptions that might have occurred
  116.      * previously
  117.      * @return a collection of exceptions
  118.      * @throws MojoExecutionException thrown if there is an exception
  119.      */
  120.     @Override
  121.     protected ExceptionCollection scanPlugins(final Engine engine, final ExceptionCollection exCollection) throws MojoExecutionException {
  122.         throw new UnsupportedOperationException("Operation not supported");
  123.     }
  124. }