mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-03-26 11:01:44 +01:00
implementing the purge feature as requested in issue #328
This commit is contained in:
@@ -233,6 +233,15 @@ public abstract class BaseDependencyCheckMojo extends AbstractMojo implements Ma
|
||||
*/
|
||||
@Parameter(property = "connectionString", defaultValue = "", required = false)
|
||||
private String connectionString;
|
||||
|
||||
/**
|
||||
* Returns the connection string.
|
||||
*
|
||||
* @return the connection string
|
||||
*/
|
||||
protected String getConnectionString() {
|
||||
return connectionString;
|
||||
}
|
||||
/**
|
||||
* The database driver name. An example would be org.h2.Driver.
|
||||
*/
|
||||
@@ -594,7 +603,7 @@ public abstract class BaseDependencyCheckMojo extends AbstractMojo implements Ma
|
||||
* Takes the properties supplied and updates the dependency-check settings. Additionally, this sets the system properties
|
||||
* required to change the proxy url, port, and connection timeout.
|
||||
*/
|
||||
private void populateSettings() {
|
||||
protected void populateSettings() {
|
||||
Settings.initialize();
|
||||
InputStream mojoProperties = null;
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* This file is part of dependency-check-maven.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright (c) 2015 Jeremy Long. All Rights Reserved.
|
||||
*/
|
||||
package org.owasp.dependencycheck.maven;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.plugin.MojoFailureException;
|
||||
import org.apache.maven.plugins.annotations.LifecyclePhase;
|
||||
import org.apache.maven.plugins.annotations.Mojo;
|
||||
import org.apache.maven.plugins.annotations.ResolutionScope;
|
||||
import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
|
||||
import org.owasp.dependencycheck.utils.Settings;
|
||||
|
||||
/**
|
||||
* Maven Plugin that purges the local copy of the NVD data.
|
||||
*
|
||||
* @author Jeremy Long
|
||||
*/
|
||||
@Mojo(
|
||||
name = "purge",
|
||||
defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
|
||||
threadSafe = true,
|
||||
requiresDependencyResolution = ResolutionScope.NONE,
|
||||
requiresOnline = true
|
||||
)
|
||||
public class PurgeMojo extends BaseDependencyCheckMojo {
|
||||
|
||||
/**
|
||||
* Returns false; this mojo cannot generate a report.
|
||||
*
|
||||
* @return <code>false</code>
|
||||
*/
|
||||
@Override
|
||||
public boolean canGenerateReport() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges the local copy of the NVD.
|
||||
*
|
||||
* @throws MojoExecutionException thrown if there is an exception executing the goal
|
||||
* @throws MojoFailureException thrown if dependency-check is configured to fail the build
|
||||
*/
|
||||
@Override
|
||||
public void runCheck() throws MojoExecutionException, MojoFailureException {
|
||||
|
||||
if (getConnectionString() != null && !getConnectionString().isEmpty()) {
|
||||
getLog().error("Unable to purge the local NVD when using a non-default connection string");
|
||||
} else {
|
||||
populateSettings();
|
||||
File db;
|
||||
try {
|
||||
db = new File(Settings.getDataDirectory(), "dc.h2.db");
|
||||
if (db.exists()) {
|
||||
if (db.delete()) {
|
||||
getLog().info("Database file purged; local copy of the NVD has been removed");
|
||||
} else {
|
||||
getLog().error(String.format("Unable to delete '%s'; please delete the file manually", db.getAbsolutePath()));
|
||||
}
|
||||
} else {
|
||||
getLog().error(String.format("Unable to purge database; the database file does not exists: %s", db.getAbsolutePath()));
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
getLog().error("Unable to delete the database");
|
||||
}
|
||||
Settings.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the report name.
|
||||
*
|
||||
* @param locale the location
|
||||
* @return the report name
|
||||
*/
|
||||
public String getName(Locale locale) {
|
||||
return "dependency-check-purge";
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the description of the Dependency-Check report to be displayed in the Maven Generated Reports page.
|
||||
*
|
||||
* @param locale The Locale to get the description for
|
||||
* @return the description
|
||||
*/
|
||||
public String getDescription(Locale locale) {
|
||||
return "Purges the local cache of the NVD dataT.";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user