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.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.Engine;
29 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
30 import org.owasp.dependencycheck.exception.ExceptionCollection;
31 import org.owasp.dependencycheck.exception.ReportException;
32 import org.owasp.dependencycheck.utils.Settings;
33
34
35
36
37
38
39
40 @Mojo(
41 name = "check",
42 defaultPhase = LifecyclePhase.VERIFY,
43 threadSafe = false,
44 requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
45 requiresOnline = true
46 )
47 public class CheckMojo extends BaseDependencyCheckMojo {
48
49
50
51
52
53
54
55 @Override
56 public boolean canGenerateReport() {
57 boolean isCapable = false;
58 for (Artifact a : getProject().getArtifacts()) {
59 if (!excludeFromScan(a.getScope())) {
60 isCapable = true;
61 break;
62 }
63 }
64 return isCapable;
65 }
66
67
68
69
70
71
72
73
74
75
76 @Override
77 public void runCheck() throws MojoExecutionException, MojoFailureException {
78 Engine engine = null;
79 try {
80 engine = initializeEngine();
81 } catch (DatabaseException ex) {
82 if (getLog().isDebugEnabled()) {
83 getLog().debug("Database connection error", ex);
84 }
85 final String msg = "An exception occurred connecting to the local database. Please see the log file for more details.";
86 if (this.isFailOnError()) {
87 throw new MojoExecutionException(msg, ex);
88 }
89 getLog().error(msg);
90 }
91 if (engine != null) {
92 ExceptionCollection exCol = scanArtifacts(getProject(), engine);
93 if (engine.getDependencies().isEmpty()) {
94 getLog().info("No dependencies were identified that could be analyzed by dependency-check");
95 } else {
96 try {
97 engine.analyzeDependencies();
98 } catch (ExceptionCollection ex) {
99 if (this.isFailOnError() && ex.isFatal()) {
100 throw new MojoExecutionException("One or more exceptions occurred during analysis", ex);
101 }
102 exCol = ex;
103 }
104 if (exCol == null || !exCol.isFatal()) {
105 try {
106 writeReports(engine, getProject(), getCorrectOutputDirectory());
107 } catch (ReportException ex) {
108 if (this.isFailOnError()) {
109 if (exCol != null) {
110 exCol.addException(ex);
111 } else {
112 exCol = new ExceptionCollection("Unable to write the dependency-check report", ex);
113 }
114 }
115 }
116
117 showSummary(getProject(), engine.getDependencies());
118 checkForFailure(engine.getDependencies());
119 if (exCol != null && this.isFailOnError()) {
120 throw new MojoExecutionException("One or more exceptions occurred during dependency-check analysis", exCol);
121 }
122 }
123 }
124 engine.cleanup();
125 }
126 Settings.cleanup();
127 }
128
129
130
131
132 @SuppressWarnings("CanBeFinal")
133 @Parameter(property = "name", defaultValue = "dependency-check", required = true)
134 private String name = "dependency-check";
135
136
137
138
139
140
141
142 @Override
143 public String getName(Locale locale) {
144 return name;
145 }
146
147
148
149
150
151
152
153
154 @Override
155 public String getDescription(Locale locale) {
156 return "Generates a report providing details on any published vulnerabilities within project dependencies. "
157 + "This report is a best effort and may contain false positives and false negatives.";
158 }
159
160 }