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