0
# Core Execution
1
2
Maven Core execution provides the primary interfaces for orchestrating Maven build processes. This includes the main Maven execution engine, session management, and request/response handling for build operations.
3
4
## Core Maven Execution
5
6
### Maven Interface
7
8
The primary entry point for executing Maven builds programmatically.
9
10
```java { .api }
11
public interface Maven {
12
/**
13
* Execute a Maven build with the given request configuration.
14
*
15
* @param request the execution request containing build configuration
16
* @return execution result containing build outcomes and any exceptions
17
*/
18
MavenExecutionResult execute(MavenExecutionRequest request);
19
20
@Deprecated
21
String POMv4 = "4.0.0";
22
}
23
```
24
25
**Usage Example:**
26
```java
27
import org.apache.maven.Maven;
28
import org.apache.maven.execution.DefaultMavenExecutionRequest;
29
import org.apache.maven.execution.MavenExecutionRequest;
30
import org.apache.maven.execution.MavenExecutionResult;
31
32
@Component
33
private Maven maven;
34
35
public void executeBuild() throws Exception {
36
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
37
request.setPom(new File("pom.xml"));
38
request.setGoals(Arrays.asList("clean", "compile", "test"));
39
request.setBaseDirectory(new File("."));
40
41
MavenExecutionResult result = maven.execute(request);
42
43
if (result.hasExceptions()) {
44
throw new Exception("Build failed", result.getExceptions().get(0));
45
}
46
}
47
```
48
49
### Default Maven Implementation
50
51
```java { .api }
52
public class DefaultMaven implements Maven {
53
// Primary implementation of Maven execution logic
54
// Handles request processing, session management, and result aggregation
55
}
56
```
57
58
## Execution Request Configuration
59
60
### MavenExecutionRequest Interface
61
62
Comprehensive configuration interface for Maven execution requests.
63
64
```java { .api }
65
public interface MavenExecutionRequest {
66
// Build Configuration
67
MavenExecutionRequest setPom(File pom);
68
File getPom();
69
70
MavenExecutionRequest setGoals(List<String> goals);
71
List<String> getGoals();
72
73
MavenExecutionRequest setSelectedProjects(List<String> selectedProjects);
74
List<String> getSelectedProjects();
75
76
MavenExecutionRequest setResumeFrom(String resumeFrom);
77
String getResumeFrom();
78
79
MavenExecutionRequest setMakeBehavior(String makeBehavior);
80
String getMakeBehavior();
81
82
// Directory Configuration
83
MavenExecutionRequest setBaseDirectory(File baseDirectory);
84
File getBaseDirectory();
85
86
MavenExecutionRequest setMultiModuleProjectDirectory(File multiModuleProjectDirectory);
87
File getMultiModuleProjectDirectory();
88
89
// Repository Configuration
90
MavenExecutionRequest setLocalRepository(ArtifactRepository localRepository);
91
ArtifactRepository getLocalRepository();
92
93
MavenExecutionRequest setRemoteRepositories(List<ArtifactRepository> repositories);
94
List<ArtifactRepository> getRemoteRepositories();
95
96
MavenExecutionRequest setPluginArtifactRepositories(List<ArtifactRepository> repositories);
97
List<ArtifactRepository> getPluginArtifactRepositories();
98
99
// Reactor Configuration
100
static final String REACTOR_FAIL_FAST = "fail-fast";
101
static final String REACTOR_FAIL_AT_END = "fail-at-end";
102
static final String REACTOR_FAIL_NEVER = "fail-never";
103
104
MavenExecutionRequest setReactorFailureBehavior(String failureBehavior);
105
String getReactorFailureBehavior();
106
107
// Threading Configuration
108
MavenExecutionRequest setDegreeOfConcurrency(int degreeOfConcurrency);
109
int getDegreeOfConcurrency();
110
111
// Logging Configuration
112
MavenExecutionRequest setLoggingLevel(int loggingLevel);
113
int getLoggingLevel();
114
115
MavenExecutionRequest setShowErrors(boolean showErrors);
116
boolean isShowErrors();
117
118
// Properties Configuration
119
MavenExecutionRequest setUserProperties(Properties userProperties);
120
Properties getUserProperties();
121
122
MavenExecutionRequest setSystemProperties(Properties systemProperties);
123
Properties getSystemProperties();
124
125
// Profile Configuration
126
MavenExecutionRequest setActiveProfiles(List<String> activeProfiles);
127
List<String> getActiveProfiles();
128
129
MavenExecutionRequest setInactiveProfiles(List<String> inactiveProfiles);
130
List<String> getInactiveProfiles();
131
132
// Execution Listeners
133
MavenExecutionRequest setExecutionListener(ExecutionListener executionListener);
134
ExecutionListener getExecutionListener();
135
136
// Toolchains
137
MavenExecutionRequest setToolchains(Map<String, List<ToolchainModel>> toolchains);
138
Map<String, List<ToolchainModel>> getToolchains();
139
}
140
```
141
142
**Configuration Example:**
143
```java
144
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
145
146
// Basic build configuration
147
request.setPom(new File("pom.xml"));
148
request.setBaseDirectory(new File("."));
149
request.setGoals(Arrays.asList("clean", "install"));
150
151
// Repository configuration
152
request.setLocalRepository(localRepo);
153
request.setRemoteRepositories(remoteRepos);
154
155
// Reactor behavior for multi-module projects
156
request.setReactorFailureBehavior(MavenExecutionRequest.REACTOR_FAIL_FAST);
157
158
// Enable parallel builds
159
request.setDegreeOfConcurrency(4);
160
161
// Profile activation
162
request.setActiveProfiles(Arrays.asList("development", "integration-tests"));
163
164
// Properties
165
Properties userProps = new Properties();
166
userProps.setProperty("maven.test.skip", "false");
167
userProps.setProperty("project.version", "1.0.0-SNAPSHOT");
168
request.setUserProperties(userProps);
169
```
170
171
## Execution Results
172
173
### MavenExecutionResult Interface
174
175
```java { .api }
176
public interface MavenExecutionResult {
177
/**
178
* Get the root project from the execution.
179
*/
180
MavenProject getProject();
181
182
/**
183
* Get all projects in topologically sorted order.
184
*/
185
List<MavenProject> getTopologicallySortedProjects();
186
187
/**
188
* Get dependency resolution results.
189
*/
190
DependencyResolutionResult getDependencyResolutionResult();
191
192
/**
193
* Get all exceptions that occurred during execution.
194
*/
195
List<Throwable> getExceptions();
196
197
/**
198
* Get build summary for a specific project.
199
*/
200
Map<MavenProject, BuildSummary> getBuildSummary(MavenProject project);
201
202
/**
203
* Check if any exceptions occurred.
204
*/
205
boolean hasExceptions();
206
207
/**
208
* Add an exception to the result.
209
*/
210
MavenExecutionResult addException(Throwable exception);
211
}
212
```
213
214
**Result Processing Example:**
215
```java
216
MavenExecutionResult result = maven.execute(request);
217
218
// Check for build failures
219
if (result.hasExceptions()) {
220
System.err.println("Build failed with " + result.getExceptions().size() + " errors:");
221
for (Throwable exception : result.getExceptions()) {
222
exception.printStackTrace();
223
}
224
} else {
225
System.out.println("Build successful!");
226
227
// Process built projects
228
for (MavenProject project : result.getTopologicallySortedProjects()) {
229
System.out.println("Built: " + project.getGroupId() + ":" +
230
project.getArtifactId() + ":" + project.getVersion());
231
}
232
}
233
234
// Access dependency resolution results
235
DependencyResolutionResult depResult = result.getDependencyResolutionResult();
236
if (depResult != null) {
237
for (Dependency dep : depResult.getDependencies()) {
238
System.out.println("Resolved: " + dep.getArtifact());
239
}
240
}
241
```
242
243
## Session Management
244
245
### MavenSession Class
246
247
Represents the state and context of a Maven execution session.
248
249
```java { .api }
250
public class MavenSession {
251
/**
252
* Get the current project being processed.
253
*/
254
public MavenProject getCurrentProject();
255
256
/**
257
* Set the current project.
258
*/
259
public void setCurrentProject(MavenProject currentProject);
260
261
/**
262
* Get all projects in the reactor.
263
*/
264
public List<MavenProject> getProjects();
265
266
/**
267
* Set all projects in the reactor.
268
*/
269
public void setProjects(List<MavenProject> projects);
270
271
/**
272
* Get the execution request that started this session.
273
*/
274
public MavenExecutionRequest getRequest();
275
276
/**
277
* Get the repository system session for artifact operations.
278
*/
279
public RepositorySystemSession getRepositorySession();
280
281
/**
282
* Set the repository system session.
283
*/
284
public void setRepositorySession(RepositorySystemSession repositorySession);
285
286
/**
287
* Get user-defined properties.
288
*/
289
public Map<String, Object> getUserProperties();
290
291
/**
292
* Get system properties.
293
*/
294
public Map<String, Object> getSystemProperties();
295
296
/**
297
* Get session-scoped values.
298
*/
299
public Map<String, Object> getPluginContext(PluginDescriptor plugin, MavenProject project);
300
301
/**
302
* Check if parallel execution is enabled.
303
*/
304
public boolean isParallel();
305
306
/**
307
* Get project dependency graph.
308
*/
309
public ProjectDependencyGraph getProjectDependencyGraph();
310
311
/**
312
* Set project dependency graph.
313
*/
314
public void setProjectDependencyGraph(ProjectDependencyGraph projectDependencyGraph);
315
316
/**
317
* Get plugin context for session-scoped data.
318
*/
319
public Map<String, Object> getPluginContext(PluginDescriptor pluginDescriptor, MavenProject project);
320
}
321
```
322
323
**Session Usage Example:**
324
```java
325
@Component
326
private Maven maven;
327
328
public void processSession() throws Exception {
329
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
330
// ... configure request
331
332
MavenExecutionResult result = maven.execute(request);
333
334
// The session is available through Maven's execution context
335
// In a plugin or component, you can access it via:
336
337
import org.apache.maven.plugins.annotations.Parameter;
338
339
@Parameter(defaultValue = "${session}", readonly = true, required = true)
340
private MavenSession session;
341
342
// Access session information
343
MavenProject currentProject = session.getCurrentProject();
344
List<MavenProject> allProjects = session.getProjects();
345
346
// Check execution mode
347
if (session.isParallel()) {
348
System.out.println("Running in parallel mode");
349
}
350
351
// Access properties
352
String skipTests = (String) session.getUserProperties().get("maven.test.skip");
353
354
// Work with project dependencies
355
ProjectDependencyGraph graph = session.getProjectDependencyGraph();
356
List<MavenProject> upstreamProjects = graph.getUpstreamProjects(currentProject, false);
357
}
358
```
359
360
## Event System Integration
361
362
### ExecutionListener Interface
363
364
```java { .api }
365
public interface ExecutionListener {
366
void projectDiscoveryStarted(ExecutionEvent event);
367
void sessionStarted(ExecutionEvent event);
368
void sessionEnded(ExecutionEvent event);
369
void projectStarted(ExecutionEvent event);
370
void projectSucceeded(ExecutionEvent event);
371
void projectFailed(ExecutionEvent event);
372
void mojoStarted(ExecutionEvent event);
373
void mojoSucceeded(ExecutionEvent event);
374
void mojoFailed(ExecutionEvent event);
375
void forkStarted(ExecutionEvent event);
376
void forkSucceeded(ExecutionEvent event);
377
void forkFailed(ExecutionEvent event);
378
}
379
```
380
381
### ExecutionEvent Class
382
383
```java { .api }
384
public class ExecutionEvent {
385
public static final Type PROJECT_DISCOVERY_STARTED = new Type("project-discovery-started");
386
public static final Type SESSION_STARTED = new Type("session-started");
387
public static final Type SESSION_ENDED = new Type("session-ended");
388
public static final Type PROJECT_STARTED = new Type("project-started");
389
public static final Type PROJECT_SUCCEEDED = new Type("project-succeeded");
390
public static final Type PROJECT_FAILED = new Type("project-failed");
391
public static final Type MOJO_STARTED = new Type("mojo-started");
392
public static final Type MOJO_SUCCEEDED = new Type("mojo-succeeded");
393
public static final Type MOJO_FAILED = new Type("mojo-failed");
394
395
public Type getType();
396
public MavenSession getSession();
397
public MavenProject getProject();
398
public MojoExecution getMojoExecution();
399
public Throwable getException();
400
}
401
```
402
403
**Event Listener Example:**
404
```java
405
public class BuildProgressListener implements ExecutionListener {
406
407
@Override
408
public void sessionStarted(ExecutionEvent event) {
409
System.out.println("Build session started for " +
410
event.getSession().getProjects().size() + " projects");
411
}
412
413
@Override
414
public void projectStarted(ExecutionEvent event) {
415
MavenProject project = event.getProject();
416
System.out.println("Building project: " + project.getName());
417
}
418
419
@Override
420
public void mojoStarted(ExecutionEvent event) {
421
MojoExecution mojo = event.getMojoExecution();
422
System.out.println("Executing: " + mojo.getPlugin().getArtifactId() +
423
":" + mojo.getGoal());
424
}
425
426
@Override
427
public void projectSucceeded(ExecutionEvent event) {
428
MavenProject project = event.getProject();
429
System.out.println("Successfully built: " + project.getName());
430
}
431
432
@Override
433
public void projectFailed(ExecutionEvent event) {
434
MavenProject project = event.getProject();
435
Throwable exception = event.getException();
436
System.err.println("Failed to build: " + project.getName() +
437
" - " + exception.getMessage());
438
}
439
}
440
441
// Configure listener in execution request
442
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
443
request.setExecutionListener(new BuildProgressListener());
444
```
445
446
## Types
447
448
```java { .api }
449
public static class Type {
450
private final String id;
451
452
public Type(String id) {
453
this.id = id;
454
}
455
456
public String toString() {
457
return id;
458
}
459
}
460
461
public interface ProjectDependencyGraph {
462
List<MavenProject> getSortedProjects();
463
List<MavenProject> getDownstreamProjects(MavenProject project, boolean transitive);
464
List<MavenProject> getUpstreamProjects(MavenProject project, boolean transitive);
465
}
466
467
public interface DependencyResolutionResult {
468
List<Dependency> getDependencies();
469
List<Dependency> getUnresolvedDependencies();
470
List<Exception> getCollectionErrors();
471
DependencyNode getDependencyGraph();
472
}
473
474
public interface BuildSummary {
475
MavenProject getProject();
476
long getTime();
477
Exception getException();
478
}
479
```