gpt4 book ai didi

java - 使用maven引用库时出现编译错误

转载 作者:搜寻专家 更新时间:2023-10-31 08:10:27 26 4
gpt4 key购买 nike

我是 Maven 的新手,在构建我的项目时遇到了一些问题。我已经将 log4j 依赖项添加到 pom 文件中

    <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>runtime</scope>
</dependency>

我在我的一个类(class)中正常使用它

import org.apache.log4j.Logger;

public class ConnectionPoolImpl implements Runnable, ConnectionPool {
static Logger logger = Logger.getLogger(ConnectionPoolImpl.class);

编译进行得很顺利,直到我使用了 mvn clean 命令。现在,当我尝试使用 mvn compile 构建我的项目时,我得到:

[INFO] Compiling 2 source files to C:\Temp\cp\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] \Temp\cp\src\main\java\com\opower\connectionpool\ConnectionPoolImpl.java
:[9,23] package org.apache.log4j does not exist
[ERROR] \Temp\cp\src\main\java\com\opower\connectionpool\ConnectionPoolImpl.java
:[19,9] cannot find symbol
symbol : class Logger
location: class com.opower.connectionpool.ConnectionPoolImpl
[ERROR] \Temp\cp\src\main\java\com\opower\connectionpool\ConnectionPoolImpl.java
:[19,25] cannot find symbol
symbol : variable Logger
location: class com.opower.connectionpool.ConnectionPoolImpl
[INFO] 3 errors

知道我做错了什么吗?

最佳答案

我不是 Maven 专家,但 log4j 范围应该是 compile 而不是 runtime。如果我错了,请纠正我。

来自 maven doc ,

runtime - this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

关于java - 使用maven引用库时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5498362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com