简体   繁体   English

使用Maven引用库时出现编译错误

[英]compilation error when referring to a library using maven

I am newbie to maven and i am having some trouble building my project. 我是Maven的新手,在构建项目时遇到了一些麻烦。 i have added the log4j dependency to the pom file 我已经将log4j依赖项添加到pom文件中

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

and i am using it in a normal use in one of my classes 我在一堂课中正常使用它

import org.apache.log4j.Logger;

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

the compilation went well until i have used the mvn clean command. 直到我使用mvn clean命令为止,编译过程都进行顺利。 now when i try to build my project using mvn compile i am getting: 现在,当我尝试使用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

any ideas what i am doing wrong? 任何想法我在做什么错?

I am not a Maven specialist, but the log4j scope should be compile instead of runtime . 我不是Maven专家,但是log4j范围应该是compile而不是runtime Please correct me if I am wrong. 如果我错了,请纠正我。

From maven doc , 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. 它在运行时和测试类路径中,但不在编译类路径中。

If you had the scope as runtime , the original intention likely was to use SLF4J or somesuch instead. 如果您将范围用作runtime ,则最初的意图可能是使用SLF4J或类似的东西。 Very easy to import the wrong namespace for Logger . Logger导入错误的名称空间非常容易。

使用编译范围,仍然使罐子陷入了战争。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM