An example to use maven-shade-plugin.
Make sure you have already run a local Kafka broker that listens at localhost:9092.
First build the project:
mvn clean install
Then run the demo:
mvn exec:java -Dexec.mainClass=Demo -pl demo
It's an example that makes a class use variant versions of Kafka client. Usually we can't import the same dependency with multiple versions. However, with the help of maven-shade-plugin, we can archive the similar result.
For example, this project has multiple modules:
- kafka-1-0-0
- kafka-2-0-0
Each module wraps a different version of Kafka client. Without the maven-shade-plugin, the demo
module, which imports these modules as the dependencies, will only choose one version of org.apache.kafka:kafka-clients
.
You may see followed logs.
2021-07-01 00:51:37:581 [Demo.main()] INFO org.apache.kafka-1-0-0.common.utils.AppInfoParser - Kafka version : 1.0.0
2021-07-01 00:51:37:581 [Demo.main()] INFO org.apache.kafka-1-0-0.common.utils.AppInfoParser - Kafka commitId : aaa7af6d4a11b29d
It's okay because AppInfoParser
reads the version and commit id from the same /kafka/kafka-version.properties
.
Intellij Idea doesn't support maven-shade-plugin well, if you just click the button to run Demo.main
, the classes may still be loaded from org.apache.kafka
package but not org.apache.kafka-x-y-z
package.
To solve this issue, you need to:
- Right click on
pom.xml
of shaded modules likekafka_1_0
. - Choose "Maven" - "Ignore Projects".
- Right click on the
pom.xml
of the project. - Choose "Maven" - "Reload project".
Then Idea will load classes from shaded JARs. However, since shaded modules are ignored by Idea, the code completion is disabled for these modules. So we need to reimport the shaded modules.
- Right click on
pom.xml
of shaded modules likekafka_1_0
. - Choose "Maven" - "Unignore Projects".
See here for reference.