How can I remove module-info.class warning for a shaded .jar?
Asked Answered
M

1

54

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.

Metagalaxy answered 9/5, 2019 at 16:10 Comment(5)
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.Implausibility
See this question not a great answer but it hits on the main points #51752481Implausibility
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 maven.apache.org/plugins/maven-shade-plugin/examples/…Implausibility
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...Abaddon
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...Metagalaxy
B
58

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!

Botnick answered 13/9, 2019 at 22:1 Comment(4)
I had to add <exclude>META-INF/**</exclude> as well to completely get rid of all warnings in my project.Logy
Note that this requires version >= 3.2.1 of the plugin. Previous versions print the warning even when the class is excluded. issues.apache.org/jira/browse/MSHADE-303Biosynthesis
It's safer to only exclude specific resources. Excluding META-INF/** could impact the application behavior in ways that are tricky to debug.Walls
@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/**/*.Inerasable

© 2022 - 2024 — McMap. All rights reserved.