Why does Maven shade plugin remove module-info.class?

Mordechai picture Mordechai · Aug 8, 2018 · Viewed 7.6k times · Source

I try to use maven-shade-plugin for a modular jar:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <minimizeJar>true</minimizeJar>
        <artifactSet>
            <includes>
                <include>javax.xml.bind:jaxb-api</include>
                <include>com.sun.xml.bind:jaxb-impl</include>
            </includes>
        </artifactSet>
        <relocations>
            <relocation>
                <pattern>javax.xml.bind</pattern>
                <shadedPattern>org.update4j.javax.xml.bind</shadedPattern>
            </relocation>
            <relocation>
                <pattern>com.sun.xml.bind</pattern>
                <shadedPattern>org.update4j.com.sun.xml.bind</shadedPattern>
            </relocation>
        </relocations>
    </configuration>
</plugin>

But Maven will remove my module-info.class from the shaded jar, with a warning:

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

How can I configure it to leave it?

EDIT: the warning actually happens when it removes the shaded jar's module descriptor, not my own.

Answer

Marc Magon picture Marc Magon · Sep 25, 2019

Because of module-info's in the JPMS world dictate the exposure of a module, shading items in may cause that nasty "Package is read from two different modules" error.

I've solved it the following way

  • Shading in, filtering out module-info across all modules
  • Then adding in the module-info (which may or may not be valid, THANK YOU MODITECT, you have no idea how useful that is)
  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <configuration>
          <artifactSet>
            <excludes>
              <exclude>module-info.java</exclude>
            </excludes>
          </artifactSet>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.moditect</groupId>
        <artifactId>moditect-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>add-module-infos</id>
            <phase>package</phase>
            <goals>
              <goal>add-module-info</goal>
            </goals>
            <configuration>
              <overwriteExistingFiles>true</overwriteExistingFiles>
              <module>
                <moduleInfoFile>
                  src/main/java/module-info.java
                </moduleInfoFile>
              </module>
            </configuration>
          </execution>
        </executions>
      </plugin>

This is a really annoying thing, and from everything I've read they have no intention of removing it or adding a flag to bypass it.