Skip to content

Commit 4bda27d

Browse files
committed
[MNG-8547] Introduce Event/Listener hierarchy with ExecutionEvent and ExecutionListener
Introduce a base Event/Listener hierarchy to support multiple event families (execution, repository, etc.) through a single registration point on Session. - Event: minimal base interface with session() accessor - ExecutionEvent: typed execution event (extends Event) with project(), mojoExecution(), exception() accessors - ExecutionListener: typed callbacks (extends Listener) with per-event-type default methods - ExecutionEventType: renamed from EventType for clarity - EventType: kept as @deprecated alias - Listener: refactored to base marker, onEvent() deprecated - DefaultEvent: implements ExecutionEvent with noun-style accessors - EventSpyImpl: dispatches to both legacy and typed listeners
1 parent cc4a0b7 commit 4bda27d

9 files changed

Lines changed: 424 additions & 36 deletions

File tree

api/maven-api-core/src/main/java/org/apache/maven/api/Event.java

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,98 @@
2121
import java.util.Optional;
2222

2323
import org.apache.maven.api.annotations.Experimental;
24+
import org.apache.maven.api.annotations.Immutable;
2425
import org.apache.maven.api.annotations.Nonnull;
2526

2627
/**
27-
* Event sent by maven during various phases of the build process.
28-
* Such events can be listened to using {@link Listener}s objects
29-
* registered in the {@link Session}.
28+
* Base interface for all Maven events.
29+
* Specific event families extend this interface to provide typed event data.
30+
* Events can be listened to using {@link Listener} objects registered in the {@link Session}.
3031
*
32+
* @see ExecutionEvent
33+
* @see Listener
3134
* @since 4.0.0
3235
*/
3336
@Experimental
37+
@Immutable
3438
public interface Event {
3539

3640
/**
37-
* Gets the type of the event.
41+
* Gets the session from which this event originates.
3842
*
39-
* @return the type of the event, never {@code null}
43+
* @return the current session, never {@code null}
4044
*/
4145
@Nonnull
42-
EventType getType();
46+
Session session();
4347

4448
/**
4549
* Gets the session from which this event originates.
4650
*
4751
* @return the current session, never {@code null}
52+
* @deprecated Use {@link #session()} instead.
53+
*/
54+
@Deprecated(since = "4.1.0", forRemoval = true)
55+
@Nonnull
56+
default Session getSession() {
57+
return session();
58+
}
59+
60+
/**
61+
* Gets the type of the event.
62+
*
63+
* @return the type of the event, never {@code null}
64+
* @deprecated Use {@link ExecutionEvent#type()} instead.
4865
*/
66+
@Deprecated(since = "4.1.0", forRemoval = true)
4967
@Nonnull
50-
Session getSession();
68+
default EventType getType() {
69+
if (this instanceof ExecutionEvent ee) {
70+
return EventType.fromExecutionEventType(ee.type());
71+
}
72+
throw new UnsupportedOperationException("getType() is only supported on ExecutionEvent instances");
73+
}
5174

5275
/**
5376
* Gets the current project (if any).
5477
*
5578
* @return the current project or {@code empty()} if not applicable
79+
* @deprecated Use {@link ExecutionEvent#project()} instead.
5680
*/
81+
@Deprecated(since = "4.1.0", forRemoval = true)
5782
@Nonnull
58-
Optional<Project> getProject();
83+
default Optional<Project> getProject() {
84+
if (this instanceof ExecutionEvent ee) {
85+
return ee.project();
86+
}
87+
return Optional.empty();
88+
}
5989

6090
/**
6191
* Gets the current mojo execution (if any).
6292
*
6393
* @return the current mojo execution or {@code empty()} if not applicable
94+
* @deprecated Use {@link ExecutionEvent#mojoExecution()} instead.
6495
*/
96+
@Deprecated(since = "4.1.0", forRemoval = true)
6597
@Nonnull
66-
Optional<MojoExecution> getMojoExecution();
98+
default Optional<MojoExecution> getMojoExecution() {
99+
if (this instanceof ExecutionEvent ee) {
100+
return ee.mojoExecution();
101+
}
102+
return Optional.empty();
103+
}
67104

68105
/**
69106
* Gets the exception that caused the event (if any).
70107
*
71108
* @return the exception or {@code empty()} if none
109+
* @deprecated Use {@link ExecutionEvent#exception()} instead.
72110
*/
73-
Optional<Exception> getException();
111+
@Deprecated(since = "4.1.0", forRemoval = true)
112+
default Optional<Exception> getException() {
113+
if (this instanceof ExecutionEvent ee) {
114+
return ee.exception();
115+
}
116+
return Optional.empty();
117+
}
74118
}

api/maven-api-core/src/main/java/org/apache/maven/api/EventType.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
* The possible types of execution events.
2525
*
2626
* @since 4.0.0
27+
* @deprecated Use {@link ExecutionEventType} instead.
2728
*/
29+
@Deprecated(since = "4.1.0", forRemoval = true)
2830
@Experimental
2931
public enum EventType {
3032
PROJECT_DISCOVERY_STARTED,
@@ -43,5 +45,24 @@ public enum EventType {
4345
FORK_FAILED,
4446
FORKED_PROJECT_STARTED,
4547
FORKED_PROJECT_SUCCEEDED,
46-
FORKED_PROJECT_FAILED,
48+
FORKED_PROJECT_FAILED;
49+
50+
/**
51+
* Converts this deprecated {@code EventType} to the new {@link ExecutionEventType}.
52+
*
53+
* @return the corresponding {@link ExecutionEventType}, never {@code null}
54+
*/
55+
public ExecutionEventType toExecutionEventType() {
56+
return ExecutionEventType.values()[ordinal()];
57+
}
58+
59+
/**
60+
* Converts an {@link ExecutionEventType} to the deprecated {@code EventType}.
61+
*
62+
* @param type the {@link ExecutionEventType} to convert, must not be {@code null}
63+
* @return the corresponding {@code EventType}, never {@code null}
64+
*/
65+
public static EventType fromExecutionEventType(ExecutionEventType type) {
66+
return values()[type.ordinal()];
67+
}
4768
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.api;
20+
21+
import java.util.Optional;
22+
23+
import org.apache.maven.api.annotations.Experimental;
24+
import org.apache.maven.api.annotations.Immutable;
25+
import org.apache.maven.api.annotations.Nonnull;
26+
27+
/**
28+
* Event sent by Maven during various phases of the build lifecycle (session, project, mojo).
29+
* Such events can be listened to using {@link ExecutionListener} objects registered in the {@link Session}.
30+
*
31+
* @see ExecutionListener
32+
* @see ExecutionEventType
33+
* @since 4.1.0
34+
*/
35+
@Experimental
36+
@Immutable
37+
public interface ExecutionEvent extends Event {
38+
39+
/**
40+
* Gets the type of the execution event.
41+
*
42+
* @return the type of the event, never {@code null}
43+
*/
44+
@Nonnull
45+
ExecutionEventType type();
46+
47+
/**
48+
* Gets the current project (if any).
49+
*
50+
* @return the current project or {@code empty()} if not applicable
51+
*/
52+
@Nonnull
53+
Optional<Project> project();
54+
55+
/**
56+
* Gets the current mojo execution (if any).
57+
*
58+
* @return the current mojo execution or {@code empty()} if not applicable
59+
*/
60+
@Nonnull
61+
Optional<MojoExecution> mojoExecution();
62+
63+
/**
64+
* Gets the exception that caused the event (if any).
65+
*
66+
* @return the exception or {@code empty()} if none
67+
*/
68+
@Nonnull
69+
Optional<Exception> exception();
70+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.api;
20+
21+
import org.apache.maven.api.annotations.Experimental;
22+
23+
/**
24+
* The possible types of execution events during the Maven build lifecycle.
25+
*
26+
* @see ExecutionEvent
27+
* @see ExecutionListener
28+
* @since 4.0.0
29+
* @since 4.1.0 (renamed from {@link EventType})
30+
*/
31+
@Experimental
32+
public enum ExecutionEventType {
33+
PROJECT_DISCOVERY_STARTED,
34+
SESSION_STARTED,
35+
SESSION_ENDED,
36+
PROJECT_SKIPPED,
37+
PROJECT_STARTED,
38+
PROJECT_SUCCEEDED,
39+
PROJECT_FAILED,
40+
MOJO_SKIPPED,
41+
MOJO_STARTED,
42+
MOJO_SUCCEEDED,
43+
MOJO_FAILED,
44+
FORK_STARTED,
45+
FORK_SUCCEEDED,
46+
FORK_FAILED,
47+
FORKED_PROJECT_STARTED,
48+
FORKED_PROJECT_SUCCEEDED,
49+
FORKED_PROJECT_FAILED,
50+
}

0 commit comments

Comments
 (0)