您能帮我解决拆分包问题吗?
我有一个module-info.java
文件,如下所示:
module server {
...
// split package issue: lot of java classes within packages with the same name
requires hbase.common;
requires hbase.client;
...
}
maven-shader-plugin
无法解决问题,因为它不知道groupId,仅包名称。意味着着色器将从hbase.common
和hbase.client
中重命名相同的程序包-拆分程序包问题仍然存在。
我还尝试创建一些shader
中间层模块,以丢弃不需要的程序包并解决拆分程序包问题。但是此解决方案也不起作用。
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;
}
服务器/模块信息.java
module server {
requires shader;
requires hbase.client;
}
我有用于组合拆分包装jar的maven插件吗?
答案 0 :(得分:1)
重要当相同的程序包暴露来自不同模块的不同类并且都需要它们时,这种方法不起作用。仅当使用不同的软件包时,它才起作用,因此您可以从冲突的JAR中过滤出软件包。
所以,问题是-具有相同程序包名称的两个依赖项(可能是可传递的)。它与JMPS不兼容,并且在编译过程中会发生故障。该问题的解决方案是手动(使用maven-shade-plugin)从依赖项之一中排除冲突的程序包。
maven-shade-plugin 具有按类或按包包含/排除的功能。这是documentation。
问题在于这种方法乍看之下不起作用。如果将插件放置在导入两个冲突的JAR的同一pom.xml
位置,则编译将由于"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拆分包验证有效,我们必须将冲突的依赖项移至单独的子模块。此外,我们必须手动解决程序包冲突(将同一程序包从一个依赖项中排除并包含到另一个依赖项中)-这意味着我们必须创建不同的sumbodules: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