gnodet opened a new pull request, #11401:
URL: https://github.com/apache/maven/pull/11401
## Overview
This PR introduces the `@Aggregate` annotation to Maven's dependency
injection framework, enabling modular contribution to aggregated collections
without replacing the entire collection.
## Key Features
- **@Aggregate annotation** for `@Provides` methods that return `List<T>` or
`Map<String, T>`
- **Multiple contributors**: Allows multiple modules to contribute entries
to the same collection
- **Preserves auto-aggregation**: Maintains existing auto-aggregation
behavior while enabling explicit contributions
- **Priority support**: Supports priority-based ordering for `List<T>`
aggregations
## Behavior
1. **Without @Aggregate**: An explicit `@Provides` method for `List<T>` or
`Map<String, T>` replaces the auto-aggregated collection entirely
2. **With @Aggregate**: The `@Provides` method contributes entries to the
auto-aggregated collection, allowing multiple providers to coexist
3. **Mixed mode**: If both `@Aggregate` and non-`@Aggregate` providers
exist, the non-`@Aggregate` provider takes precedence
## Use Cases
- **Plugin architecture**: Different modules can register services without
depending on a central registry
- **Lifecycle providers**: Multiple modules can contribute lifecycle phases
- **Extensible service registries**: Core and extension modules can
contribute services to the same map/list
## Implementation Details
- New `Binding.BindingToMethod` class to track the source method for bindings
- Enhanced `InjectorImpl` with aggregation logic for List and Map injection
- Support for `@Priority` ordering in aggregated lists
- Comprehensive test coverage with 30+ test cases
## Example Usage
```java
// Module A
@Provides
@Aggregate
Map<String, LifecycleProvider> lifecycleProviders() {
return Map.of("clean", new CleanLifecycle());
}
// Module B
@Provides
@Aggregate
Map<String, LifecycleProvider> moreLifecycleProviders() {
return Map.of("deploy", new DeployLifecycle());
}
// Consumer
@Inject
Map<String, LifecycleProvider> allLifecycles; // Contains all contributions
```
## Files Changed
- `api/maven-api-di/src/main/java/org/apache/maven/api/di/Aggregate.java` -
New annotation with comprehensive documentation
- `impl/maven-di/src/main/java/org/apache/maven/di/impl/Binding.java` -
Added `BindingToMethod` class
- `impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java` -
Aggregation logic implementation
-
`impl/maven-di/src/main/java/org/apache/maven/di/impl/ReflectionUtils.java` -
Updated to use `BindingToMethod`
-
`impl/maven-di/src/test/java/org/apache/maven/di/impl/InjectorImplTest.java` -
Comprehensive test coverage
## Testing
Added 30+ test cases covering:
- Basic map and list aggregation
- Multiple `@Aggregate` providers
- Explicit providers (with and without `@Aggregate`)
- Priority ordering
- Empty collections
- Duplicate keys
- Mixed scenarios
- Integration with existing DI features
---
Pull Request opened by [Augment Code](https://www.augmentcode.com/) with
guidance from the PR author
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]