我有完全相同的问题,这是我做的工作。 我的项目包含一个“ThatProject”库,其中包含一个导致错误的ServiceFactory.java类。
<dependencies>
<dependency>
<groupId>com.example.ThatGroup</groupId>
<artifactId>ThatProject</artifactId>
<version>${ThatProject.version}</version>
</dependency>
</dependencies>
在我自己的项目,我创建了一个相同的类影子“ThatProject”(该路径必须匹配)之一。 →src \ main \ java \ com \ example \ ThatGroup \ ThatProject \ ServiceFactory.java
在我的pom.xml中,我使用了maven-shade-plugin插件,如下所示。 请注意我是如何从“ThatProject”库中排除原始ServiceFactory.java的。
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- Include all dependencies in JAR -->
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>shade</id>
<goals>
<goal>shade</goal>
</goals>
<phase>package</phase>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>com.example.ThatGroup:ThatClient</artifact>
<excludes>
<exclude>com/example/ThatGroup/ThatProject/ServiceFactory.class</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
而就是这样,它终于为我工作。就我而言,我使用pom.xml的这个配置(包括我的镜像版本的ServiceFactory.java)包含了我的项目的所有文件。如前所述,原始的ServiceFactory.java在Maven构建期间被排除。
希望这会有所帮助!
那么,这是一个实用工具类,用于不受我控制的外部jar,我无法扩展该类。 我已经直接将类替换为jar,但正如你所说这不是很正确。 类路径重新排序是在web项目中正好不起作用的东西。 – antgar9