Eclipse Photon + Maven + Log4j2(获取错误模块log4j.api未找到,xxx要求)

问题描述 投票:2回答:2

我下载了eclipse光子并试图用log4j2运行非常基本的例子。这是我的配置

项目结构

Project Structure

这是模块路径屏幕截图

Module Path screen shot

POM.hml

<properties>

    <java-version>10</java-version>

    <!-- Unit testing -->
    <junit.version>4.12</junit.version>
    <junit-jupiter-engine.version>5.2.0</junit-jupiter-engine.version>
    <junit-platform-runner.version>1.2.0</junit-platform-runner.version>

    <!-- Logging -->
    <log4j.version>2.11.0</log4j.version>
    <slf4j-api.verion>1.7.25</slf4j-api.verion>
    <jboss-logging.version>3.3.2.Final</jboss-logging.version>

    <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
    <maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
    <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
    <maven-failsafe-plugin.version>2.21.0</maven-failsafe-plugin.version>
    <maven-javadoc-plugin.version>3.0.0</maven-javadoc-plugin.version>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<dependencies>

    <!-- Unit Testing  -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit-jupiter-engine.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit-platform-runner.version}</version>
        <scope>test</scope>
    </dependency>

    <!-- Logging-->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jcl</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jul</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j-api.verion}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>${jboss-logging.version}</version>
        <scope>runtime</scope>
    </dependency>

</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>
    <pluginManagement>
        <plugins>

            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-enforcer-plugin</artifactId>
                                    <versionRange>[1.0.0,)</versionRange>
                                    <goals>
                                        <goal>enforce</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${maven-failsafe-plugin.version}</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>${maven-javadoc-plugin.version}</version>
            <configuration>
                <additionalparam>-Xdoclint:none</additionalparam>
            </configuration>
        </plugin>
    </plugins>
</build>

主类

package pk.training.basit;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Welcome {

    private static final Logger logger = LogManager.getLogger();

    public static void main(String[] args) {
        logger.info("Welcome to the Module System.");

        // Print the module name of the Welcome class
        Class<Welcome> cls = Welcome.class;
        Module mod = cls.getModule();
        String moduleName = mod.getName();
        System.out.format("Module Name: %s%n", moduleName);
    }
}

模块信息

module pk.training.basit {
    exports pk.training.basit;
    requires log4j.api;
}

但是,当我运行项目时,我收到了错误

Error occurred during initialization of boot layer
java.lang.module.FindException: Module log4j.api not found, required by pk.training.basit

为什么我收到此错误?依赖关系位于模块路径中。

如果我从代码中删除log4j2然后使用以下module-info运行代码:

module pk.training.basit {
    exports pk.training.basit;
}

然后代码运行正常。

那么有什么东西我做错了或它是maven eclipse相关的问题,它找不到第三方依赖?

谢谢

eclipse maven-3 log4j2 m2eclipse java-10
2个回答
1
投票

上面显示的log4j api模块的名称(log4j.api)是错误的。 Log4j API的模块名称是org.apache.logging.log4j。


0
投票

我有完全相同的问题。

As of version 2.10.0 the Log4j API is a Java module (with a module-info.java)

当键入org.apache.logging.log4j eclipse时,我建议用核心来完成它。对于log4j2应用程序的核心插件。它不会改变任何问题

唯一的解决方案是通过从module-info中删除log4j-api来打破模块化。

如果我在模块信息中保留log4j.api我也会在我的导入中收到此消息:

The package org.apache.logging.log4j is accessible from more than one module: log4j.api, org.apache.logging.log4j.core

我想要导入的类不在log4j.core中

© www.soinside.com 2019 - 2024. All rights reserved.