Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/idelstak/maven-serviceloader-autodiscovery-example
Maven project showcasing seamless auto-discovery using the ServiceLoader pattern.
https://github.com/idelstak/maven-serviceloader-autodiscovery-example
Last synced: about 1 month ago
JSON representation
Maven project showcasing seamless auto-discovery using the ServiceLoader pattern.
- Host: GitHub
- URL: https://github.com/idelstak/maven-serviceloader-autodiscovery-example
- Owner: IdelsTak
- Created: 2024-02-04T12:10:48.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-02-04T12:23:18.000Z (11 months ago)
- Last Synced: 2024-02-04T13:29:45.450Z (11 months ago)
- Language: Java
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# ServiceLoader Maven Plugin Example
Demonstrating the use of the `serviceloader-maven-plugin` for seamless `ServiceLoader` auto service discovery in a Maven multi-module project.
> I fell back to this after failing to make the [conventional](https://docs.oracle.com/javase%2F9%2Fdocs%2Fapi%2F%2F/java/util/ServiceLoader.html) JDK service auto discovery work as expected in a multi-module Maven project
## Key Components
### 1. Service Module
Defines the `TextService` interface.
```java
public interface TextService {
String process(String text);
}
```### 2. Provider Module
Implements `UpperCaseTextService`, transforming text to uppercase.
```java
public class UpperCaseTextService implements TextService {
@Override
public String process(String text) {
return text == null ? null : text.toUpperCase();
}
}
```### 3. Consumer Module POM
Configures Maven build for the `consumer` module as a service consumer, dynamically discovering implementations.
```xml
${project.groupId}
service
${project.version}${project.groupId}
provider
${project.version}
runtimecom.github.idelstak.consumer.Consumer
libs
```
### Runtime Dependency
- `provider` module: Runtime dependency for dynamic service discovery. Scope set to `runtime` to avoid compile-time transitive dependencies.
> Runtime dependency on `provider` is crucial for ServiceLoader to dynamically discover and load `TextService` implementations during execution.
## Parent POM
Configures the `serviceloader-maven-plugin` at the parent level for effective service discovery across modules.