如何解决Java9中的拆分包问题

mdfafbf1  于 2021-06-09  发布在  Hbase
关注(0)|答案(1)|浏览(400)

发行

我有 module-info.java 文件如下所示:

module server {

   ...
   // split package issue: lot of java classes within packages with the same name
   requires hbase.common;
   requires hbase.client;
   ...
}

解决方法1

这个问题不是靠别人解决的 maven-shader-plugin ,因为它不知道groupid,只知道包名。意味着着色器将重命名两个文件包中的相同文件包: hbase.common 以及 hbase.client -拆分包问题仍然存在。

解决方法2

我也试着创造一些 shader 中间层模块,用于丢弃不需要的包并解决拆分包问题。但是这个解决方案也不起作用。
着色器/module-info.java:

module shader {
    requires hbase.common;
    // exports only packages I do need at my code. Shade unneded packages
    // IS THERE ANY WAY TO MAKE IT WORK?
    // Got: X module reads package org.apache.hadoop.hbase.util from both shader and hbase.common
    exports org.apache.hadoop.hbase.util;
}

服务器/module-info.java

module server {
    requires shader;
    requires hbase.client;
}

ps公司

有没有maven插件来组合拆分包jar?

bqujaahr

bqujaahr1#

重要信息:当同一个包从不同的模块公开不同的类并且这两个类都是必需的时,这种方法不起作用。它只在使用不同的包时起作用,因此您可以从冲突的jar中筛选出包。
所以,问题是两个依赖项(可能是可传递的)具有相同的包名。在编译期间,它与jmps和failfast不兼容。这个问题的解决方案是手动(使用maven shade plugin)从一个依赖项中排除冲突的包。
maven shade插件具有每个类或每个包的包含/排除功能。这是文件。

不起作用的解决方案(问题的解释)

问题是这种方法乍一看是行不通的。如果你把插件放在同一个位置 pom.xml 如果导入了两个冲突的jar,编译将因 "module X reads package org.apache.hadoop.hbase.util from both hbase.client and hbase.common " . jpms在编译阶段运行(在插件启动的包阶段之前)。举个例子:

server/
  |-src/
  |  |-main/
  |     |-java/
  |        |-com.somapackage
  |        |-module-info.java
  |           [ 
  |               module server {
  |                   requires java.base;
  |                   requires hbase.common; //they have lots of conflicting packages
  |                   requires hbase.client; //they have lots of conflicting packages
  |               }
  |            ]
  |-pom.xml
      [
    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-common</artifactId>
            <version>${hbase.common.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
         </dependency>
         <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>${hbase.client.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
         </dependency>
    </dependencies>
    <build>
    <plugins>
           <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>
                       <configuration>
                           <minimizeJar>false</minimizeJar>
                           <filters>
                               <filter>
                                   <artifact>org.apache.hbase:hbase-client</artifact>
                                   <excludes>
                                       <exclude>org/apache/hadoop/hbase/util/**</exclude>
                                   </excludes>
                               </filter>
                               <filter>
                                   <artifact>org.apache.hbase:hbase-common</artifact>
                                   <includes>
                                       <include>org/apache/hadoop/hbase/util/**</include>
                                   </includes>
                               </filter>
                           </filters>
                       </configuration>
                   </execution>
               </executions>
           </plugin>
       </plugins>
       </build>
      ]

工作溶液

为了使jpms分割包验证在着色之后工作,我们必须将冲突的依赖项移动到单独的子模块。此外,我们必须手动解决包冲突(将同一个包从一个依赖项中排除,并包含到另一个依赖项中)-这意味着我们必须创建不同的sumbodule: shader1 以及 shader2 . 代码如下:

server/
  |-src/
  |  |-main/
  |     |-java/
  |        |-com.somapackage
  |        |-module-info.java
  |           [ 
  |               module server {
  |                   requires java.base;
  |                   requires shader1;
  |                   requires shader2;
  |               }
  |            ]
  |-pom.xml
      [
    <dependency>
        <groupId>com.organization.proj</groupId>
        <artifactId>shader1</artifactId>
        <version>${proj.version}</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/../shader/target/shader1-0.0.1.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>com.organization.proj</groupId>
        <artifactId>shader2</artifactId>
        <version>${proj.version}</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/../shader2/target/shader2-0.0.1.jar</systemPath>
    </dependency>
      ]

  |
  |
shader1
  |-src/
  |  |-main/
  |     |-java/
  |        |-com.somapackage
  |        |-module-info.java
  |           [ 
  |               module shader1 {
  |                   requires java.base;
  |                   requires transitive hbase.client;
  |               }
  |            ]
  |-pom.xml
      [
    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>${hbase.client.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
         </dependency>
    </dependencies>
    <build>
    <plugins>
           <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>
                       <configuration>
                           <minimizeJar>false</minimizeJar>
                           <filters>
                               <filter>
                                   <artifact>org.apache.hbase:hbase-client</artifact>
                                   <excludes>
                                       <exclude>org/apache/hadoop/hbase/util/**</exclude>
                                   </excludes>
                               </filter>
                           </filters>
                       </configuration>
                   </execution>
               </executions>
           </plugin>
       </plugins>
       </build>
      ]
  |
  |
shader2
  |-src/
  |  |-main/
  |     |-java/
  |        |-com.somapackage
  |        |-module-info.java
  |           [ 
  |               module shader2 {
  |                   requires java.base;
  |                   requires transitive hbase.common;
  |               }
  |            ]
  |-pom.xml
      [
    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-common</artifactId>
            <version>${hbase.common.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
         </dependency>
    </dependencies>
    <build>
    <plugins>
           <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>
                       <configuration>
                           <minimizeJar>false</minimizeJar>
                           <filters>
                               <filter>
                                   <artifact>org.apache.hbase:hbase-common</artifact>
                                   <includes>
                                       <include>org/apache/hadoop/hbase/util/**</include>
                                   </includes>
                               </filter>
                           </filters>
                       </configuration>
                   </execution>
               </executions>
           </plugin>
       </plugins>
       </build>
      ]

别忘了使用 require transitive 在着色器子模块处

相关问题