Skip to content

Commit a4130c9

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
1 parent eaa19c8 commit a4130c9

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
@@ -273,6 +273,86 @@ If you need to send a messages during startup, implement `ApplicationListener` a
273273
Alternatively, implement `SmartLifecycle`, put your bean in a late phase, and send the messages from the `start()` method.
274274

275275

276+
[[shaded]]
277+
=== Considerations When using Packaged (e.g. Shaded) Jars
278+
279+
Spring Integration bootstraps certain features using Spring Framework's `SpringFactories` mechanism to load several `IntegrationConfigurationInitializer` classes.
280+
This includes the `-core` jar as well as certain others such as `-http`, `-jmx`, etc.
281+
The information for this process is stored in a file `META-INF/spring.factories` in each jar.
282+
283+
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].
284+
285+
By default, the shade plugin will not merge the `spring.factories` files when producing the shaded jar.
286+
287+
In addition to `spring.factories`, there are other `META-INF` files (`spring.handlers`, `spring.schemas`) used for XML configuration.
288+
These also need to be merged.
289+
290+
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.
291+
So, with a Spring Boot application, nothing more is needed, if you use its default executable jar format.
292+
293+
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.
294+
295+
The following is an example configuration for the plugin at the time of writing.
296+
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.
297+
298+
.pom.xml
299+
[source, xml]
300+
----
301+
...
302+
<plugins>
303+
<plugin>
304+
<groupId>org.apache.maven.plugins</groupId>
305+
<artifactId>maven-shade-plugin</artifactId>
306+
<configuration>
307+
<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
308+
<createDependencyReducedPom>true</createDependencyReducedPom>
309+
</configuration>
310+
<dependencies>
311+
<dependency> <1>
312+
<groupId>org.springframework.boot</groupId>
313+
<artifactId>spring-boot-maven-plugin</artifactId>
314+
<version>${spring.boot.version}</version>
315+
</dependency>
316+
</dependencies>
317+
<executions>
318+
<execution>
319+
<phase>package</phase>
320+
<goals>
321+
<goal>shade</goal>
322+
</goals>
323+
<configuration>
324+
<transformers> <2>
325+
<transformer
326+
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
327+
<resource>META-INF/spring.handlers</resource>
328+
</transformer>
329+
<transformer
330+
implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
331+
<resource>META-INF/spring.factories</resource>
332+
</transformer>
333+
<transformer
334+
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
335+
<resource>META-INF/spring.schemas</resource>
336+
</transformer>
337+
<transformer
338+
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
339+
</transformers>
340+
</configuration>
341+
</execution>
342+
</executions>
343+
</plugin>
344+
</plugins>
345+
...
346+
----
347+
348+
Specifically,
349+
350+
<1> add the `spring-boot-maven-plugin` as a dependency
351+
352+
<2> configure the transformers
353+
354+
Add a property for `${spring.boot.version}` or use a version explicitly there.
355+
276356
[[programming-tips]]
277357
=== Programming Tips and Tricks
278358

0 commit comments

Comments
 (0)