Maven 从不同的依赖项中动态排除具有相同名称的 class
Maven dynamically exclude class with same name from different dependencies
有两个类com.package.A
,一个来自
<dependency>
<groupId>com.package</groupId>
<artifactId>art1</artifactId>
</dependency>
还有一个来自
<dependency>
<groupId>com.package</groupId>
<artifactId>art2</artifactId>
</dependency>
请注意工件 ID 不同。
对于不同的 Maven 配置文件,我想排除一个版本而只保留另一个版本。我正在使用 Shade 插件。
有了maven-shade-plugin
,就可以exclude certain class for specific dependencies. This is configured with the help of the filters
属性:
Archive Filters to be used. Allows you to specify an artifact in the form of a composite identifier as used by artifactSet
and a set of include/exclude file patterns for filtering which contents of the archive are added to the shaded jar.
在您的情况下,要从依赖项 art2
中排除 class com.package.A
,您可以:
<filters>
<filter>
<artifact>com.package:art2</artifact>
<excludes>
<exclude>com/package/A.class</exclude>
</excludes>
</filter>
</filters>
要使其动态化,即 select 在构建时要保留 com.package.A
class,您不需要使用配置文件。您可以使用 Maven 属性 来保存要过滤的依赖项的工件 ID。在您的属性中,添加
<properties>
<shade.exclude.artifactId>art2</shade.exclude.artifactId>
</properties>
shade.exclude.artifactId
属性 将保存要过滤的依赖项的工件 ID。默认情况下,此配置为 select art2
。然后,在Shade Plugin的<filter>
配置中,可以使用<artifact>com.package:${shade.exclude.artifactId}</artifact>
.
这是一个完整的配置:
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>shade</id>
<goals>
<goal>shade</goal>
</goals>
<phase>package</phase>
<configuration>
<filters>
<filter>
<artifact>com.package:${shade.exclude.artifactId}</artifact>
<excludes>
<exclude>com/package/A.class</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<shade.exclude.artifactId>art2</shade.exclude.artifactId>
</properties>
运行 mvn clean package
将使用来自 art1
的 A.class
创建一个 uber jar,因为来自 art2
的那个被排除在外。然后,运行 mvn clean package -Dshade.exclude.artifactId=art1
将从依赖项 art2
中保留这次 A.class
因为来自 art1
的那个被排除在外。
有两个类com.package.A
,一个来自
<dependency>
<groupId>com.package</groupId>
<artifactId>art1</artifactId>
</dependency>
还有一个来自
<dependency>
<groupId>com.package</groupId>
<artifactId>art2</artifactId>
</dependency>
请注意工件 ID 不同。
对于不同的 Maven 配置文件,我想排除一个版本而只保留另一个版本。我正在使用 Shade 插件。
有了maven-shade-plugin
,就可以exclude certain class for specific dependencies. This is configured with the help of the filters
属性:
Archive Filters to be used. Allows you to specify an artifact in the form of a composite identifier as used by
artifactSet
and a set of include/exclude file patterns for filtering which contents of the archive are added to the shaded jar.
在您的情况下,要从依赖项 art2
中排除 class com.package.A
,您可以:
<filters>
<filter>
<artifact>com.package:art2</artifact>
<excludes>
<exclude>com/package/A.class</exclude>
</excludes>
</filter>
</filters>
要使其动态化,即 select 在构建时要保留 com.package.A
class,您不需要使用配置文件。您可以使用 Maven 属性 来保存要过滤的依赖项的工件 ID。在您的属性中,添加
<properties>
<shade.exclude.artifactId>art2</shade.exclude.artifactId>
</properties>
shade.exclude.artifactId
属性 将保存要过滤的依赖项的工件 ID。默认情况下,此配置为 select art2
。然后,在Shade Plugin的<filter>
配置中,可以使用<artifact>com.package:${shade.exclude.artifactId}</artifact>
.
这是一个完整的配置:
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>shade</id>
<goals>
<goal>shade</goal>
</goals>
<phase>package</phase>
<configuration>
<filters>
<filter>
<artifact>com.package:${shade.exclude.artifactId}</artifact>
<excludes>
<exclude>com/package/A.class</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<shade.exclude.artifactId>art2</shade.exclude.artifactId>
</properties>
运行 mvn clean package
将使用来自 art1
的 A.class
创建一个 uber jar,因为来自 art2
的那个被排除在外。然后,运行 mvn clean package -Dshade.exclude.artifactId=art1
将从依赖项 art2
中保留这次 A.class
因为来自 art1
的那个被排除在外。