Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compilation error when referring to a library using maven

I am newbie to maven and i am having some trouble building my project. i have added the log4j dependency to the pom file

    <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. now when i try to build my project using mvn compile i am getting:

[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?

like image 806
special0ne Avatar asked Mar 31 '11 10:03

special0ne


2 Answers

I am not a Maven specialist, but the log4j scope should be compile instead of runtime. Please correct me if I am wrong.

From 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.

like image 60
Petar Minchev Avatar answered Nov 01 '22 00:11

Petar Minchev


If you had the scope as runtime, the original intention likely was to use SLF4J or somesuch instead. Very easy to import the wrong namespace for Logger.

like image 40
usethe4ce Avatar answered Oct 31 '22 23:10

usethe4ce