1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.maven;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.Locale;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.plugins.annotations.LifecyclePhase;
26 import org.apache.maven.plugins.annotations.Mojo;
27 import org.apache.maven.plugins.annotations.ResolutionScope;
28 import org.owasp.dependencycheck.utils.Settings;
29
30
31
32
33
34
35 @Mojo(
36 name = "purge",
37 defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
38 threadSafe = false,
39 requiresDependencyResolution = ResolutionScope.NONE,
40 requiresOnline = true
41 )
42 public class PurgeMojo extends BaseDependencyCheckMojo {
43
44
45
46
47
48
49 @Override
50 public boolean canGenerateReport() {
51 return false;
52 }
53
54
55
56
57
58
59
60
61
62 @Override
63 public void runCheck() throws MojoExecutionException, MojoFailureException {
64
65 if (getConnectionString() != null && !getConnectionString().isEmpty()) {
66 final String msg = "Unable to purge the local NVD when using a non-default connection string";
67 if (this.isFailOnError()) {
68 throw new MojoFailureException(msg);
69 }
70 getLog().error(msg);
71 } else {
72 populateSettings();
73 File db;
74 try {
75 db = new File(Settings.getDataDirectory(), "dc.h2.db");
76 if (db.exists()) {
77 if (db.delete()) {
78 getLog().info("Database file purged; local copy of the NVD has been removed");
79 } else {
80 final String msg = String.format("Unable to delete '%s'; please delete the file manually", db.getAbsolutePath());
81 if (this.isFailOnError()) {
82 throw new MojoFailureException(msg);
83 }
84 getLog().error(msg);
85 }
86 } else {
87 final String msg = String.format("Unable to purge database; the database file does not exists: %s", db.getAbsolutePath());
88 if (this.isFailOnError()) {
89 throw new MojoFailureException(msg);
90 }
91 getLog().error(msg);
92 }
93 } catch (IOException ex) {
94 final String msg = "Unable to delete the database";
95 if (this.isFailOnError()) {
96 throw new MojoExecutionException(msg, ex);
97 }
98 getLog().error(msg);
99 }
100 Settings.cleanup();
101 }
102 }
103
104
105
106
107
108
109
110 @Override
111 public String getName(Locale locale) {
112 return "dependency-check-purge";
113 }
114
115
116
117
118
119
120
121
122 @Override
123 public String getDescription(Locale locale) {
124 return "Purges the local cache of the NVD dataT.";
125 }
126
127 }