Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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}
runtime

com.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.