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

走远了吗. 提交于 2019-12-13 13:00:38

问题


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.


回答1:


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>
                     <excludes>
                        <exclude>module-info.java</exclude>
                    </excludes>
        </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.



来源:https://stackoverflow.com/questions/51751981/why-does-maven-shade-plugin-remove-module-info-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!