Issue
I am trying to generate Uber Jar using maven-shade-plugin and i want to exclude some resources from the shaded jar and include some of specified artifact.But following exclude resources are bundled with it.
<dependencies>
<dependency>
<groupId>com.sample.auto</groupId>
<artifactId>sample</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.sample.manual</groupId>
<artifactId>sample-manual</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>Distribute</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/LICENSE</exclude>
<exclude>LICENSE</exclude>
<exclude>com/myproject/auto/**</exclude>
<exclude>org/**</exclude>
<exclude>/*.png</exclude>
<exclude>/*.html</exclude>
<exclude>/*.jpeg</exclude>
<exclude>com/google/common/**</exclude>
</excludes>
</filter>
</filters>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Also i need to include com.sample.auto
related artifact only into my uber jar. Please let me know where i made the mistake.
Solution
The filters on the artifacts as looking into your code should ideally be:
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/LICENSE</exclude>
<exclude>LICENSE</exclude> <!--if this is same as above, not required-->
<exclude>/*.png</exclude>
<exclude>/*.html</exclude>
<exclude>/*.jpeg</exclude>
<exclude>com.myproject.auto:**</exclude> <!--to exclude all the artifacts from com.myproject.auto group-->
<exclude>org:**</exclude><!--exclude all artifacts under group org-->
<exclude>com.google.common:**</exclude>
</excludes>
</filter>
</filters>
OR in case you are looking to fine grain the classes from the dependent artifacts, then you can apply multiple filters like
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/LICENSE</exclude>
<exclude>LICENSE</exclude> <!--if this is same as above, not required-->
<exclude>/*.png</exclude>
<exclude>/*.html</exclude>
<exclude>/*.jpeg</exclude>
</excludes>
</filter>
<filter>
<artifact>com.myproject.auto:*<artifact>
<excludes>
<exclude>com/myproject/auto/**</exclude> <!--to exclude a set of classes from the project structure-->
</excludes>
</filter>
<filter>
<artifact>org:*<artifact>
<excludes>
<exclude>org/**</exclude> <!--though the artifact naming needs to be refined for this-->
</excludes>
</filter>
<filter>
<artifact>com.google.common:**<artifact>
<excludes>
<exclude>com/google/common/**</exclude> <!--exclude classes from the respective artifacts-->
</excludes>
</filter>
</filters>
There are pretty useful examples to exclude-include configurations on Apache documentation as linked by you itself.
Answered By - Naman
Answer Checked By - Katrina (JavaFixing Volunteer)