1
votes

Our application uses slf4j, but has dependency to

  1. slf4j api
  2. slf4j over log4j
  3. log4j

The problem is that very often IDE imports classes from log4j and not from slf4j. Ideally, i want to have only slf4j api as a maven dependency for my application and pack slf4j binding with log4j only at the time i building a war.

I found several solutions so far:

  1. Add libs to WEB-INF/lib folder. This is bad, because i have no maven dependency control and have to store binary in my repo which is not the best thing to do.
  2. Use maven-war plugin with overlays. But as i understand adding dependency to overlay will require to declare dependency (at least as compile)

Is it ok to have only dependency for slf4j api? How to package other dependencies to war without declaring them as project dependencies?

Thanks!

1
You can strict the imports ne? only Logger and LoggerFactory classes are needed to import, which are form slf4j.Abimaran Kugathasan
What do you mean by "strict the imports"? I believe excluding redundant dependency is better.Vadim Kirilchuk
You know what the api classes need to import into your class...?Abimaran Kugathasan
Yes, i know, but it's not about only me, my IDE and one particular class, it's about big team.Vadim Kirilchuk

1 Answers

1
votes

Please simply specify dependency to slf4j-log4j in runtime scope.

So during compile and test time class from runtime scope will not be available. Also in IDE it shouldn't be visible - I checked it in IntelliJ.

Of course all artifacts with runtime scope will be put in WEB-INF/lib directory.

Example:

    ...
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
        <scope>runtime</scope>
    </dependency>
    ...