Maven build [警告]我们有一个重复的类

She*_*jie 28 build maven maven-shade-plugin apache-commons-logging

有谁知道我的maven构建发生了什么?我收到了很多重复的警告.

[WARNING] We have a duplicate org/apache/commons/logging/impl/LogFactoryImpl$1.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/LogFactoryImpl.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/NoOpLog.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/SimpleLog$1.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/SimpleLog.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/Jdk14Logger.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
Run Code Online (Sandbox Code Playgroud)

我查看了我当地的m2 repo,我在commons-logging-api jar,LogFactoryImpl.class和LogFactoryImpl $ 1.class中有两个类.与警告中提到的所有类相同.

有一点需要提一下,我在我的pom.xml中使用了shade插件.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <createDependencyReducedPom>true</createDependencyReducedPom>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.~~~~black out my own main class here~~~~~</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

我注意到依赖树如下所示

[INFO] +- org.apache.cxf:cxf-bundle-jaxrs:jar:2.5.1:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] \- org.apache.hadoop.hive:hive-jdbc:jar:0.7.1-cdh3u3:compile
[INFO]    \- org.apache.hadoop.hive:hive-common:jar:0.7.1-cdh3u3:compile
[INFO]       \- commons-logging:commons-logging-api:jar:1.0.4:compile
Run Code Online (Sandbox Code Playgroud)

和commons-logging.jar和commons-logging-api.jar都有org/apache/commons/logging/LogFactory.class.

不知何故,Shad插件最终试图将它们挤进一个大胖罐.然后警告出现了.有人说这是可以忽略的警告.但我有点担心,如果有两个具有相同名称的重复类,应用程序如何知道应该使用的确切类?

use*_*388 11

您可能还遇到了maven-shader-plugin的限制.它取代了默认的jar工件(由maven-jar-plugin创建).这在干净的构建中工作正常,但是在没有重新生成jar的重建中,着色器再次在它上次创建的jar上运行,该jar已经包含所有类依赖项的副本.这会产生很多关于重复的警告.

从maven-shader-plugin 2.0开始,这个问题仍未得到解决:https: //issues.apache.org/jira/browse/MSHADE-126

一种解决方法是将maven-jar-plugin明确添加到pom.xml并添加配置设置<forceCreation>true</forceCreation>.


nde*_*rge 9

看一下Maven文档中的"Dependency Exclusions"部分.

在您提供的示例中,我将从中排除commons-logging:commons-logging-api:jar:1.0.4:compile依赖项org.apache.hadoop.hive:hive-common:jar:0.7.1-cdh3u3:compile.在你的pom.xml中:

    <dependency>
        <groupId>org.apache.hadoop.hive</groupId>
        <artifactId>hive-common:jar</artifactId>
        <version>0.7.1-cdh3u3</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
Run Code Online (Sandbox Code Playgroud)


Som*_*era 5

就我而言,我的父 pom 包含 commons-beanutils,而我的子模块(这是我唯一想要编译的东西)包含 commons-io。

由于 commons-io 和 commons-beansutil 共享一些公共类,因此阴影插件抱怨重复。请注意,即使不需要并且没有使用 beansutiul,它也被包含在内。

我通过将其添加到配置中来最小化 jar 来解决这个问题:

<minimizeJar>true</minimizeJar>
Run Code Online (Sandbox Code Playgroud)

现在阴影插件没有添加未使用的资源。

警告消失了。