45

I'm packaging a Spring Boot app into an uber jar using the maven-shade plugin. Simple, right? Well, it is except as of recently I'm getting the following warning at the end of mvn clean package:

[WARNING] Discovered module-info.class. Shading will break its strong encapsulation.

It's not actually breaking anything but I'm a perfectionist and this is driving me nuts. How do I get rid of it? I've tried many things without success.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 3
    This is what happens when you create an "Uber Jar" you are removing the use of modules inside that jar. Simple answer would be this is life as a a developer you are gonna need to lose that perfectionist attitude. However, the real question is why are you creating the uber jar and do you really need to. – nerdlyist May 09 '19 at 16:22
  • 1
    See this question not a great answer but it hits on the main points https://stackoverflow.com/questions/51751981/why-does-maven-shade-plugin-remove-module-info-class – nerdlyist May 09 '19 at 16:23
  • 1
    Sorry for the multiple comments. There is a configuration on shade to filter out files so you could do that up front and if that happens before warnings you should not see it anymore. Check it out here https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html – nerdlyist May 09 '19 at 16:32
  • 2
    If you are doing a spring boot app I don't know why you are using maven-shade-plugin does not make sense. In a spring boot app you are using spring-boot-maven-plugin ...which handles that....furthermore it does not make sense having module-info you your application for a spring boot app... – khmarbaise May 09 '19 at 17:00
  • Thank guys for the responses. I'll have a look through my code when I get home and will let you know. I think khmarbaise might be right, my project was a Dropwizard project recently converted to Springboot and the maven-shade plugin might be a hangover from that conversion... – Albert Rannetsperger May 10 '19 at 03:59

1 Answers1

48

Filtering out the file in the shade plugin seems to work fine for me.

Here's what my maven-shade-plugin config looks like:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <configuration>
        <filters>
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>module-info.class</exclude>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                </excludes>
            </filter>
        </filters>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The key line is the <exclude>module-info.class</exclude>. The filter excludes that file whenever it sees it, in any artifact (*:* = any artifact). (The other three excludes I use to get rid of bugs with signature files in dependencies)

I haven't noticed any unwanted side effects from doing this, and the warning is now gone!

double-beep
  • 5,031
  • 17
  • 33
  • 41
rococo
  • 2,280
  • 2
  • 22
  • 37
  • 2
    I had to add `META-INF/**` as well to completely get rid of all warnings in my project. – Vapid Mar 10 '21 at 11:07
  • 1
    Note that this requires version >= 3.2.1 of the plugin. Previous versions print the warning even when the class is excluded. https://issues.apache.org/jira/browse/MSHADE-303 – sudeep Apr 10 '21 at 04:10
  • It's safer to only exclude specific resources. Excluding `META-INF/**` could impact the application behavior in ways that are tricky to debug. – Bart Swennenhuis Mar 11 '22 at 08:50
  • 2
    @VapidLinus Just one thing to notice: If you exclude the entire META-INF directory, the app may stop working if, for example, SPI service interfaces are used. In this case you should also include the "org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" to relocate the packages including services if you relocate. Better exclude META-INF/versions/**/*. – Tiemo Vorschütz Apr 05 '22 at 21:34