Skip to content

Commit 2f03838

Browse files
garyrussellartembilan
authored andcommitted
INT-4401: Document working with shaded jars
JIRA: https://jira.spring.io/browse/INT-4401 How to fix up the `spring.factories` etc. * Polishing (cherry picked from commit a4130c9)
1 parent a09403c commit 2f03838

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/reference/asciidoc/overview.adoc

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,86 @@ If you need to send a messages during startup, implement `ApplicationListener` a
272272
Alternatively, implement `SmartLifecycle`, put your bean in a late phase, and send the messages from the `start()` method.
273273

274274

275+
[[shaded]]
276+
=== Considerations When using Packaged (e.g. Shaded) Jars
277+
278+
Spring Integration bootstraps certain features using Spring Framework's `SpringFactories` mechanism to load several `IntegrationConfigurationInitializer` classes.
279+
This includes the `-core` jar as well as certain others such as `-http`, `-jmx`, etc.
280+
The information for this process is stored in a file `META-INF/spring.factories` in each jar.
281+
282+
Some developers prefer to repackage their application and all dependencies into a single jar using well-known tools, such as the https://maven.apache.org/plugins/maven-shade-plugin/[Apache Maven Shade Plugin].
283+
284+
By default, the shade plugin will not merge the `spring.factories` files when producing the shaded jar.
285+
286+
In addition to `spring.factories`, there are other `META-INF` files (`spring.handlers`, `spring.schemas`) used for XML configuration.
287+
These also need to be merged.
288+
289+
IMPORTANT: https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html[Spring Boot's executable jar mechanism] takes a different approach in that it nests the jars, thus retaining each `spring.factories` file on the class path.
290+
So, with a Spring Boot application, nothing more is needed, if you use its default executable jar format.
291+
292+
Even if you are not using Spring Boot, you can still use tooling provided by Boot to enhance the shade plugin by adding transformers for the above mentioned files.
293+
294+
The following is an example configuration for the plugin at the time of writing.
295+
You may wish to consult the current https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/pom.xml[spring-boot-starter-parent pom] to see the current settings that boot uses.
296+
297+
.pom.xml
298+
[source, xml]
299+
----
300+
...
301+
<plugins>
302+
<plugin>
303+
<groupId>org.apache.maven.plugins</groupId>
304+
<artifactId>maven-shade-plugin</artifactId>
305+
<configuration>
306+
<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
307+
<createDependencyReducedPom>true</createDependencyReducedPom>
308+
</configuration>
309+
<dependencies>
310+
<dependency> <1>
311+
<groupId>org.springframework.boot</groupId>
312+
<artifactId>spring-boot-maven-plugin</artifactId>
313+
<version>${spring.boot.version}</version>
314+
</dependency>
315+
</dependencies>
316+
<executions>
317+
<execution>
318+
<phase>package</phase>
319+
<goals>
320+
<goal>shade</goal>
321+
</goals>
322+
<configuration>
323+
<transformers> <2>
324+
<transformer
325+
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
326+
<resource>META-INF/spring.handlers</resource>
327+
</transformer>
328+
<transformer
329+
implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
330+
<resource>META-INF/spring.factories</resource>
331+
</transformer>
332+
<transformer
333+
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
334+
<resource>META-INF/spring.schemas</resource>
335+
</transformer>
336+
<transformer
337+
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
338+
</transformers>
339+
</configuration>
340+
</execution>
341+
</executions>
342+
</plugin>
343+
</plugins>
344+
...
345+
----
346+
347+
Specifically,
348+
349+
<1> add the `spring-boot-maven-plugin` as a dependency
350+
351+
<2> configure the transformers
352+
353+
Add a property for `${spring.boot.version}` or use a version explicitly there.
354+
275355
[[programming-tips]]
276356
=== Programming Tips and Tricks
277357

0 commit comments

Comments
 (0)