maven-shade-plugin的AppendingTransformer合并spring.factories出现重复key问题
Hey there, let's break down your problem clearly:
First off, the duplicate keys appearing in your merged spring.factories file is not a bug—it's the expected behavior of the AppendingTransformer. This transformer works by simply appending the content of identical resource files from all your dependencies together, with no logic to check for duplicate keys or merge their values. When multiple dependencies define the same key (like org.springframework.context.ApplicationContextInitializer) in their spring.factories, the merged file ends up with that key repeated. Spring only reads the last occurrence of each key during loading, which causes earlier configurations to be overwritten—leading to missing components and startup failures.
The Fix: Use Spring Boot's Specialized Shade Transformer
To properly merge spring.factories (and handle duplicate keys by combining their values into a single entry), replace your AppendingTransformer instances with Spring Boot's ShadeSpringBootTransformer.
Here's your updated maven-shade-plugin configuration:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.1.1</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.1.RELEASE</version> </dependency> </dependencies> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope> <minimizeJar>false</minimizeJar> <createDependencyReducedPom>false</createDependencyReducedPom> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.mindgeek.cloud.downloader.application.Application</mainClass> </transformer> <!-- Replaces the three AppendingTransformer entries; handles merging of spring.factories, spring.handlers, and spring.schemas automatically --> <transformer implementation="org.springframework.boot.maven.ShadeSpringBootTransformer"/> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
Why This Works
ShadeSpringBootTransformer is purpose-built for Spring Boot projects using the shade plugin:
- For
spring.factories, it automatically merges all values for the same key into a single comma-separated line, eliminating duplicate keys entirely. - It also correctly handles merging of
spring.handlersandspring.schemasfiles, so you don't need separate transformers for those anymore.
Quick Notes
- Make sure the version of
spring-boot-maven-pluginmatches your project's Spring Boot version (your current 2.0.1.RELEASE is fine as long as it aligns with your project dependencies). - Your current maven-shade-plugin version (3.1.1) is compatible with Spring Boot 2.0.1.RELEASE, so no need to upgrade it right now.
After updating your configuration and repackaging, your merged spring.factories will have the correct format, and your server should start without issues.
内容的提问来源于stack exchange,提问作者Ugo Ducharme

