Closed. This question needs to be more focused。它当前不接受答案。












想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。

4年前关闭。





如何避免或解决Jar hell?无需使用诸如著名的OSGi框架之类的重量级框架。
假设我们有两个必需的依赖项,这些依赖项已不再受支持,并且不能与同一库的不同版本稳定地运行?我需要所有员工一起工作。

最佳答案

我使用了sonatype jarjar maven插件来自动创建重命名为其他包的依赖类的副本。

这为您的库提供了其依赖项的私有副本,从而防止了版本与可能与其部署的任何其他库发生版本冲突。

请记住,某些软件可能具有许可条件,因此您无法制作其副本。

例如,以下规则对创建Apache commons-lang3的私有副本。 Apache commons-lang通常部署在名为org.apache.commons.lang3的软件包中,但是在这里,我在软件包com.lexicalscope.fluentreflection.internal.lang3中创建了它的副本。该插件会自动修复我的图书馆使用Apache commons-lang3链接到副本的所有位置。

<rule>
    <pattern>org.apache.commons.lang3.**.*</pattern>
    <result>com.lexicalscope.fluentreflection.internal.lang3.@1.$@2</result>
</rule>
<rule>
    <pattern>org.apache.commons.lang3.*</pattern>
    <result>com.lexicalscope.fluentreflection.internal.lang3.$@1</result>
</rule>


您可以使用相同的方法将第三方库与它们自己的依存关系的私有副本重新打包。

这是完整的示例maven构建配置:

<plugin>
    <groupId>org.sonatype.plugins</groupId>
    <artifactId>jarjar-maven-plugin</artifactId>
    <version>1.5</version>
    <executions>
        <execution>
            <id>jarjar-classes</id>
            <phase>process-test-classes</phase>
            <goals>
                <goal>jarjar</goal>
            </goals>
            <configuration>
                <overwrite>true</overwrite>
                <input>{classes}</input>
            </configuration>
        </execution>
        <execution>
            <id>jarjar-testclasses</id>
            <phase>process-test-classes</phase>
            <goals>
                <goal>jarjar</goal>
            </goals>
            <configuration>
                <overwrite>true</overwrite>
                <input>{test-classes}</input>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <overwrite>true</overwrite>
        <includes>
            <include>com.google.inject:guice</include>
            <include>com.googlecode.lambdaj:lambdaj</include>
            <include>org.apache.commons:commons-lang3</include>
            <include>com.google.guava:guava</include>
        </includes>
        <rules>
            <rule>
                <pattern>com.google.common.**.*</pattern>
                <result>com.lexicalscope.fluentreflection.internal.guava.@1.$@2</result>
            </rule>
            <rule>
                <pattern>com.google.common.*</pattern>
                <result>com.lexicalscope.fluentreflection.internal.guava.$@1</result>
            </rule>
            <rule>
                <pattern>com.google.inject.**.*</pattern>
                <result>com.lexicalscope.fluentreflection.internal.guice.@1.$@2</result>
            </rule>
            <rule>
                <pattern>com.google.inject.*</pattern>
                <result>com.lexicalscope.fluentreflection.internal.guice.$@1</result>
            </rule>
            <rule>
                <pattern>ch.lambdaj.**.*</pattern>
                <result>com.lexicalscope.fluentreflection.internal.lamdaj.@1.$@2</result>
            </rule>
            <rule>
                <pattern>ch.lambdaj.*</pattern>
                <result>com.lexicalscope.fluentreflection.internal.lamdaj.$@1</result>
            </rule>
            <rule>
                <pattern>org.apache.commons.lang3.**.*</pattern>
                <result>com.lexicalscope.fluentreflection.internal.lang3.@1.$@2</result>
            </rule>
            <rule>
                <pattern>org.apache.commons.lang3.*</pattern>
                <result>com.lexicalscope.fluentreflection.internal.lang3.$@1</result>
            </rule>
        </rules>
    </configuration>
</plugin>

09-10 05:50
查看更多