Sub topic 13
Packaging a Custom Starter
This chapter assumes you can build a Maven or Gradle project and that you have read the chapters on writing, registering, and testing a custom auto-configuration. Here you package the greeting example so other teams can add it with one dependency.
Starter versus auto-configuration
A starter is a dependency descriptor. It carries no logic of its own. It pulls in the libraries a feature needs. An auto-configuration is the code that creates beans from those libraries and from properties.
Boot's convention splits these into two modules:
| Module | Contents | Example name |
|---|---|---|
| Auto-configure module | `GreetingAutoConfiguration`, `GreetingProperties`, the `.imports` file | `greeting-spring-boot-autoconfigure` |
| Starter module | A POM that depends on the auto-configure module and the library | `greeting-spring-boot-starter` |
For a small internal library, a single module that holds both the code and the dependencies is acceptable. Split them when the library is reused outside Spring Boot or when you want optional dependencies that the starter decides to include.
Naming conventions
Spring Boot reserves the spring-boot-starter-* prefix for its own official starters. Third-party starters use the form acme-spring-boot-starter, where the technology name comes first. Do not start your artifact name with spring-boot. It suggests official support and can collide with official artifacts.
The same rule applies to configuration property names. Pick a unique prefix such as greeting. Never reuse a prefix that Boot owns, like server or spring.
The structure
The application declares one dependency. Maven resolves the rest transitively.
The starter POM
The starter module has no Java code. Its POM lists what to pull in:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>greeting-spring-boot-starter</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>greeting-spring-boot-autoconfigure</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</project>The spring-boot-starter dependency is the core starter. It brings in Spring Boot, logging, and auto-configuration support. In a real build you would import the Boot BOM or use the Boot parent instead of writing the version by hand, as the dependency management chapter showed.
The auto-configure module
In the auto-configure module, mark the dependencies that only some users need as optional. This keeps them off the consumer's classpath unless the starter or the user adds them:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<optional>true</optional>
</dependency>Optional dependencies pair with @ConditionalOnClass. The configuration activates only when the class is present, so a missing optional library does not cause a failure.
Also add the spring-boot-configuration-processor as an optional dependency. It generates metadata for your @ConfigurationProperties classes, so IDEs can autocomplete greeting.message in application.properties.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>Checking the result
Install both modules locally, then add the starter to a fresh application:
<dependency>
<groupId>com.example</groupId>
<artifactId>greeting-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>Start the application with --debug. The conditions report should list GreetingAutoConfiguration under positive matches. If it appears under negative matches, read the reason. Common causes are a missing .imports file in the auto-configure JAR and a wrong class name inside it.
Exercise
Why does the starter module in this chapter contain no Java classes, and what must the auto-configure module contain for the application to get a GreetingService bean?
Show answer
A starter only declares dependencies, so it needs no code. The auto-configure module must contain the @AutoConfiguration class, the @ConfigurationProperties class, and the META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports file that lists the auto-configuration class name. Without the imports entry, Boot never considers the class.