java.lang.NoSuchMethodError: org.apache.logging.lo

2019-08-05 07:39发布

问题:

I am trying to deploy a WAR file to a Glassfish 4.1 server.

During an attempt at deployment, I receive the following exception :

java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.NoSuchMethodError: org.apache.logging.log4j.core.lookup.MapLookup.toMap(Ljava/util/List;)Ljava/util/Map;

Looking in the javadoc, I indeed can't see any toMap method ? (https://logging.apache.org/log4j/2.0/log4j-core/apidocs/org/apache/logging/log4j/core/lookup/MapLookup.html)

jar tvf on the war file :

WEB-INF/lib/log4j-core-2.1.jar
WEB-INF/lib/javax.inject-2.4.0-b06.jar
WEB-INF/lib/postgresql-9.3-1102-jdbc41.jar
WEB-INF/lib/javax.annotation-api-1.2.jar
WEB-INF/lib/javassist-3.18.1-GA.jar
WEB-INF/lib/jersey-common-2.15.jar
WEB-INF/lib/hk2-api-2.4.0-b06.jar
WEB-INF/lib/validation-api-1.1.0.Final.jar
WEB-INF/lib/jersey-container-servlet-core-2.15.jar
WEB-INF/lib/hk2-locator-2.4.0-b06.jar
WEB-INF/lib/log4j-web-2.0.2.jar
WEB-INF/lib/log4j-core-2.0.2.jar
WEB-INF/lib/jersey-client-2.15.jar
WEB-INF/lib/hk2-utils-2.4.0-b06.jar
WEB-INF/lib/javax.ws.rs-api-2.0.1.jar
WEB-INF/lib/aopalliance-repackaged-2.4.0-b06.jar
WEB-INF/lib/commons-lang3-3.3.2.jar
WEB-INF/lib/osgi-resource-locator-1.0.1.jar
WEB-INF/lib/log4j-api-2.1.jar
WEB-INF/lib/log4j-api-2.0.2.jar
WEB-INF/lib/jersey-guava-2.15.jar
WEB-INF/lib/jersey-server-2.15.jar
WEB-INF/lib/log4j-web-2.1.jar
WEB-INF/lib/commons-codec-1.10.jar

My pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>WebTool</groupId>
<artifactId>WebTool</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>WebTool</name>

<build>
    <finalName>WebTool</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- my -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
        </dependency>
        <!--
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-bom</artifactId>
            <version>${log4j.version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <!-- <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>${log4j.version}</version>
        </dependency>-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commonslang.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>${commonscodec.version}</version>
        </dependency>
        <!-- end my -->
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>
    <!-- uncomment this to get JSON support
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    -->



    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <!-- <version>${postgresql.version}</version> -->
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <!-- <version>${log4j.version}</version> -->
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <!-- <version>${log4j.version}</version> -->
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
        <!-- <version>${log4j.version}</version> -->
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <!-- <version>${commonslang.version}</version> -->
    </dependency>
    <dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
        <!-- <version>${commonscodec.version}</version> -->
    </dependency>

    <!-- configure connection pool in glassfish admin ! JDBC Connection Pool and JCBC Resource -->
</dependencies>
<!--
   DEPENDENCY MANAGEMENT
   -->

<properties>
    <postgresql.version>9.3-1102-jdbc41</postgresql.version>
    <jersey.version>2.15</jersey.version>
    <log4j.version>2.1</log4j.version>
    <dbcp2.version>2.0.1</dbcp2.version>
    <commonslang.version>3.3.2</commonslang.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <commonscodec.version>1.10</commonscodec.version>
</properties>

Dependency tree:

    [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ WebTool ---
[INFO] WebTool:WebTool:war:1.0-SNAPSHOT
[INFO] +- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.15:compile
[INFO] |  +- org.glassfish.hk2.external:javax.inject:jar:2.4.0-b06:compile
[INFO] |  +- org.glassfish.jersey.core:jersey-common:jar:2.15:compile
[INFO] |  |  +- (javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile - omitted for duplicate)
[INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.2:compile
[INFO] |  |  +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.15:compile
[INFO] |  |  +- org.glassfish.hk2:hk2-api:jar:2.4.0-b06:compile
[INFO] |  |  |  +- org.glassfish.hk2:hk2-utils:jar:2.4.0-b06:compile
[INFO] |  |  |  \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.4.0-b06:compile
[INFO] |  |  +- (org.glassfish.hk2.external:javax.inject:jar:2.4.0-b06:compile - omitted for duplicate)
[INFO] |  |  +- org.glassfish.hk2:hk2-locator:jar:2.4.0-b06:compile
[INFO] |  |  |  +- (org.glassfish.hk2.external:javax.inject:jar:2.4.0-b06:compile - omitted for duplicate)
[INFO] |  |  |  +- (org.glassfish.hk2.external:aopalliance-repackaged:jar:2.4.0-b06:compile - omitted for duplicate)
[INFO] |  |  |  +- (org.glassfish.hk2:hk2-api:jar:2.4.0-b06:compile - omitted for duplicate)
[INFO] |  |  |  +- (org.glassfish.hk2:hk2-utils:jar:2.4.0-b06:compile - omitted for duplicate)
[INFO] |  |  |  \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  |  \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile
[INFO] |  +- org.glassfish.jersey.core:jersey-server:jar:2.15:compile
[INFO] |  |  +- (org.glassfish.jersey.core:jersey-common:jar:2.15:compile - omitted for duplicate)
[INFO] |  |  +- org.glassfish.jersey.core:jersey-client:jar:2.15:compile
[INFO] |  |  |  +- (org.glassfish.jersey.core:jersey-common:jar:2.15:compile - omitted for duplicate)
[INFO] |  |  |  +- (javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile - omitted for duplicate)
[INFO] |  |  |  +- (org.glassfish.hk2:hk2-api:jar:2.4.0-b06:compile - omitted for duplicate)
[INFO] |  |  |  +- (org.glassfish.hk2.external:javax.inject:jar:2.4.0-b06:compile - omitted for duplicate)
[INFO] |  |  |  \- (org.glassfish.hk2:hk2-locator:jar:2.4.0-b06:compile - omitted for duplicate)
[INFO] |  |  +- (javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile - omitted for duplicate)
[INFO] |  |  +- (javax.annotation:javax.annotation-api:jar:1.2:compile - omitted for duplicate)
[INFO] |  |  +- (org.glassfish.hk2:hk2-api:jar:2.4.0-b06:compile - omitted for duplicate)
[INFO] |  |  +- (org.glassfish.hk2.external:javax.inject:jar:2.4.0-b06:compile - omitted for duplicate)
[INFO] |  |  +- (org.glassfish.hk2:hk2-locator:jar:2.4.0-b06:compile - omitted for duplicate)
[INFO] |  |  \- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  \- javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile
[INFO] +- org.postgresql:postgresql:jar:9.3-1102-jdbc41:compile
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.1:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.1:compile
[INFO] |  \- (org.apache.logging.log4j:log4j-api:jar:2.1:compile - omitted for duplicate)
[INFO] +- org.apache.logging.log4j:log4j-web:jar:2.1:compile
[INFO] |  +- (org.apache.logging.log4j:log4j-api:jar:2.1:compile - omitted for duplicate)
[INFO] |  \- (org.apache.logging.log4j:log4j-core:jar:2.1:compile - omitted for duplicate)
[INFO] +- org.apache.commons:commons-lang3:jar:3.3.2:compile
[INFO] \- commons-codec:commons-codec:jar:1.10:compile

回答1:

You have multiple versions of log4j version 2 in your WAR file, one you specified, and one - most likely - pulled in as a transitive dependency. Normally only the latest should be selected, but it may be a repackaged version.

Hence you need to identify where the one you do not want, comes from so you can figure out what to do with it. You can see the dependency tree with

mvn dependency:tree

See http://maven.apache.org/plugins/maven-dependency-plugin/usage.html for usage information.

Note that if you use Eclipse with m2e, just select the correct panel when having pom.xml open.

You then need to figure out what to do with it when you find out where it comes from. You most likely just want to exclude it (as you have a newer) - see http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html for instructions. I would suggest you read it carefully so you understand what you are doing - this is essentially brain washing Maven because you know better.