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.plugin.MojoExecutionException;
22  import org.apache.maven.plugin.MojoFailureException;
23  import org.apache.maven.plugins.annotations.LifecyclePhase;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.apache.maven.plugins.annotations.ResolutionScope;
26  import org.owasp.dependencycheck.Engine;
27  import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
28  import org.owasp.dependencycheck.data.update.exception.UpdateException;
29  import org.owasp.dependencycheck.exception.ExceptionCollection;
30  import org.owasp.dependencycheck.utils.InvalidSettingException;
31  import org.owasp.dependencycheck.utils.Settings;
32  
33  
34  
35  
36  
37  
38  @Mojo(
39          name = "update-only",
40          requiresProject = false,
41          defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
42          threadSafe = true,
43          requiresDependencyResolution = ResolutionScope.NONE,
44          requiresOnline = true,
45          aggregator = true
46  )
47  public class UpdateMojo extends BaseDependencyCheckMojo {
48  
49      
50  
51  
52  
53  
54      @Override
55      public boolean canGenerateReport() {
56          return false;
57      }
58  
59      
60  
61  
62  
63  
64  
65  
66  
67  
68      @Override
69      protected void runCheck() throws MojoExecutionException, MojoFailureException {
70          muteNoisyLoggers();
71          try (Engine engine = initializeEngine()) {
72              try {
73                  if (!engine.getSettings().getBoolean(Settings.KEYS.AUTO_UPDATE)) {
74                      engine.getSettings().setBoolean(Settings.KEYS.AUTO_UPDATE, true);
75                  }
76              } catch (InvalidSettingException ex) {
77                  engine.getSettings().setBoolean(Settings.KEYS.AUTO_UPDATE, true);
78              }
79              engine.doUpdates();
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          } catch (UpdateException ex) {
90              final String msg = "An exception occurred while downloading updates. Please see the log file for more details.";
91              if (this.isFailOnError()) {
92                  throw new MojoExecutionException(msg, ex);
93              }
94              getLog().error(msg);
95          } finally {
96              getSettings().cleanup();
97          }
98      }
99  
100     
101 
102 
103 
104 
105 
106     @Override
107     public String getName(Locale locale) {
108         return "dependency-check-update";
109     }
110 
111     
112 
113 
114 
115 
116 
117 
118     @Override
119     public String getDescription(Locale locale) {
120         return "Updates the local cache of the NVD data from NIST.";
121     }
122 
123     
124 
125 
126 
127 
128 
129 
130 
131     @Override
132     protected ExceptionCollection scanDependencies(Engine engine) throws MojoExecutionException {
133         throw new UnsupportedOperationException("Operation not supported");
134     }
135 
136     
137 
138 
139 
140 
141 
142 
143 
144 
145     @Override
146     protected ExceptionCollection scanPlugins(final Engine engine, final ExceptionCollection exCollection) throws MojoExecutionException {
147         throw new UnsupportedOperationException("Operation not supported");
148     }
149 }