Sub topic 13
Packaging a Custom Starter
This chapter assumes you can read the example auto-configuration from the earlier chapters (GreetingAutoConfiguration, GreetingProperties, GreetingService). It also assumes you know Maven's dependencyManagement and optional dependencies. Here you will package that code so another project can use it by adding one dependency.
What a starter is, again
A starter is a dependency descriptor. It contains no logic. Its only job is to pull in the libraries and the auto-configuration a feature needs, so a user writes one dependency instead of three.
An auto-configuration is code: classes with @AutoConfiguration and conditions that create beans. The two are often confused because Boot's own starters, such as spring-boot-starter-web, only forward to spring-boot-autoconfigure and some libraries. Boot ships all of its auto-configuration in that one spring-boot-autoconfigure jar.
For your own code you have a choice about layout:
| Layout | Modules | When it fits |
|---|---|---|
| Single module | One jar holding the library, the auto-configuration, and the dependencies | Internal library, a few consumers |
| Two modules | `autoconfigure` jar plus an empty `starter` jar | You want users to opt out of the starter but keep the auto-configuration |
| Three modules | `core` library, `autoconfigure`, `starter` | The core library is usable without Spring |
The Spring Boot reference documentation recommends the two-module split for public libraries. This chapter uses three modules, because GreetingService is a plain Java class that should not depend on Spring.
Naming conventions
Names tell users what a jar is and keep you out of Boot's namespace.
- Do not start your starter's artifact ID with
spring-boot. Names likespring-boot-starter-greetingare reserved for starters the Spring Boot team publishes. - Put your own name first:
greeting-spring-boot-starterandgreeting-spring-boot-autoconfigure. Third-party projects such asmybatis-spring-boot-starterfollow this pattern. - Do not use Boot's property namespaces for your keys. Avoid
server,spring,management,logging, and similar prefixes. Ourgreetingprefix is safe. - Use your own package for your classes. Do not publish classes under
org.springframework.boot.
The core module
greeting-core holds GreetingService and has no Spring dependency. Its POM is ordinary:
<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.greeting</groupId>
<artifactId>greeting-core</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
</project>The autoconfigure module
This module holds GreetingAutoConfiguration, GreetingProperties, and the imports file. It depends on greeting-core and on spring-boot-autoconfigure.
Two choices matter in this POM.
- Import the Boot BOM, not the Boot parent. A library should not inherit
spring-boot-starter-parent. The parent configures the repackage plugin, which turns the jar into an executable archive that other projects cannot use as a dependency. Importingspring-boot-dependenciesgives you managed versions without that side effect. - Mark the
greeting-coredependencyoptional. Auto-configuration is read even when the application excludes its dependencies. IfGreetingServiceis missing at runtime, the@ConditionalOnClass(GreetingService.class)condition fails and the class backs off instead of crashing. Marking the dependencyoptionalmakes this intent explicit. The starter then adds it back for users who want the feature.
<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.greeting</groupId>
<artifactId>greeting-spring-boot-autoconfigure</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>com.example.greeting</groupId>
<artifactId>greeting-core</artifactId>
<version>1.0.0</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>The spring-boot-configuration-processor is an annotation processor. During compilation it reads GreetingProperties and writes META-INF/spring-configuration-metadata.json. IDEs use that file to offer completion and documentation for greeting.message in application.properties. Without it the property still works, but nobody sees it.
Next, the imports file. It must sit at this exact path inside the autoconfigure module:
src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsIts content is one fully qualified class name per line:
com.example.greeting.GreetingAutoConfigurationLines starting with # are comments. A typo here causes no error. Boot simply never loads the class.
The starter module
The starter has a POM and nothing else. It has no src directory.
<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.greeting</groupId>
<artifactId>greeting-spring-boot-starter</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.example.greeting</groupId>
<artifactId>greeting-spring-boot-autoconfigure</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.example.greeting</groupId>
<artifactId>greeting-core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>The greeting-core dependency is not optional here. When a user adds the starter, Maven resolves both jars and the condition on GreetingService passes. A starter should also pull in any other library the feature needs at runtime, such as a logging or HTTP client library. For an empty-jar starter, Maven still builds a small jar containing only metadata, which is fine.
If you use Gradle, the same starter uses api so the dependencies are visible to consumers:
plugins {
`java-library`
}
dependencies {
api(project(":greeting-core"))
api(project(":greeting-spring-boot-autoconfigure"))
}In the autoconfigure project, declare the core dependency as compileOnly to get the same optional behavior, and add the configuration processor with annotationProcessor.
Building and consuming the starter
Build the three modules in dependency order (core, autoconfigure, starter) and install them into your local repository:
mvn -f greeting-core/pom.xml install
mvn -f greeting-spring-boot-autoconfigure/pom.xml install
mvn -f greeting-spring-boot-starter/pom.xml installIn practice, put the three modules under a parent POM with <modules> so one mvn install builds them in the right order.
A consuming project that uses spring-boot-starter-parent adds:
<dependency>
<groupId>com.example.greeting</groupId>
<artifactId>greeting-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>The version is required. The Boot parent manages versions only for Boot's own artifacts and the libraries it lists, not for yours. If you publish several starters, also publish a small BOM (a POM with dependencyManagement and <packaging>pom</packaging>) so users import it once and omit versions afterward.
The application can then inject GreetingService with no configuration class. It can also set greeting.message=Welcome to change behavior, or define its own GreetingService bean to replace yours, because of @ConditionalOnMissingBean.
Edge cases and common errors
- The bean never appears. Check the imports file name and path first. Then run the application with
--debugand look in the conditions report forGreetingAutoConfiguration. If it is absent from both the positive and negative lists, Boot never found it. If it is listed as a negative match, the report names the failed condition. - The old
spring.factoriesfile. Before Spring Boot 2.7, registration usedMETA-INF/spring.factorieswith the keyEnableAutoConfiguration. Boot 3 no longer reads that key for auto-configuration, so a starter that only hasspring.factoriessilently stops working after the upgrade. Publish the.importsfile. If you support old Boot versions, you can ship both. - Component scanning picks up your auto-configuration. The user's
@ComponentScanstarts at their own package. If your auto-configuration package is inside it, Boot may load the class as a normal configuration and ignore its ordering and conditions. Keep your package separate, such ascom.example.greeting, and never ask users to scan it. Boot's@SpringBootApplicationalso applies an exclude filter for auto-configurations, but do not rely on it. - Core classes leaking into conditions. In
@ConditionalOnClass, refer to a class by.classonly on a class-level annotation of the auto-configuration. On a@Beanmethod, use thenameattribute instead, such as@ConditionalOnClass(name = "com.example.greeting.GreetingService"). Otherwise the JVM may try to load a missing class while reading the annotation and fail. - The BOM is not imported. If you forget
dependencyManagement,spring-boot-autoconfigurehas no version and Maven fails with a "version is missing" message. - Executable jar by mistake. Applying
spring-boot-maven-pluginto a library produces a jar withBOOT-INFdirectories that no one can compile against. Use the plugin only in applications.
Checking the package before you publish
Open the built autoconfigure jar and check three things:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsexists and names your class.META-INF/spring-configuration-metadata.jsonexists and listsgreeting.messagewith its default ofHello.- The jar has no
BOOT-INFdirectory.
Then run the ApplicationContextRunner tests from the previous chapter against the packaged jar's classes. A test that passes inside the module but fails from a consuming project usually points to a missing imports entry or a missing dependency in the starter.
Exercise
A teammate publishes greeting-spring-boot-starter containing the auto-configuration class and the imports file, with greeting-core as a normal dependency. Another team wants the auto-configuration's greeting.message property metadata but wants to supply their own copy of greeting-core. Why does the single-jar layout make this hard, and how does the two-module layout fix it? Also, name the Boot 3 file that must list the auto-configuration class.
Show answer
With one jar, anyone who wants the auto-configuration must take the starter, which brings in its dependency list, including its own greeting-core. Splitting the code into greeting-spring-boot-autoconfigure (with greeting-core marked optional) and an empty greeting-spring-boot-starter lets the second team depend only on the autoconfigure module and add their own core jar. The class is registered in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.
Exercise
Why should a library's POM import spring-boot-dependencies instead of using spring-boot-starter-parent as its parent?
Show answer
The starter parent configures the repackage goal of the Spring Boot Maven plugin, which produces an executable jar with a BOOT-INF layout that other projects cannot use as a dependency. Importing the BOM gives managed versions without that behavior.