SBT - log4j 的未知工件

Posted

技术标签:

【中文标题】SBT - log4j 的未知工件【英文标题】:SBT - Unknown artifact for log4j 【发布时间】:2017-07-09 14:23:55 【问题描述】:

我无法在我的 sbt 项目中解决 LOG4j for scala 的依赖关系。 我有一个具有相同依赖项和相同 scala 版本的 Maven 项目,它运行良好。 这是我的build.sbt

scalaVersion := "2.11.8"

libraryDependencies += "org.apache.logging.log4j" %% "log4j-core" % "2.7"

libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.1"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"

这是 IntelliJ 显示给我的错误

Error:Error while importing SBT project:<br/>...<br/><pre>[info] Resolving org.scala-sbt#template-resolver;0.1 ...
[info] Resolving org.scala-tools.sbinary#sbinary_2.10;0.4.2 ...
[info] Resolving org.scala-sbt#api;0.13.13 ...
[info] Resolving org.scala-sbt#incremental-compiler;0.13.13 ...
[info] Resolving org.scala-sbt#apply-macro;0.13.13 ...
[info] Resolving org.spire-math#json4s-support_2.10;0.6.0 ...
[info] Resolving com.thoughtworks.paranamer#paranamer;2.6 ...
[info] Resolving org.scala-sbt#test-agent;0.13.13 ...
[info] Resolving org.scala-sbt#classfile;0.13.13 ...
[info] Resolving org.scala-sbt#completion;0.13.13 ...
[info] Resolving org.scala-sbt#test-interface;1.0 ...
[info] Resolving com.jcraft#jsch;0.1.50 ...
[info] Resolving org.scala-lang#scala-compiler;2.10.6 ...
[info] Resolving org.scala-sbt#interface;0.13.13 ...
[info] Resolving org.scala-sbt#logging;0.13.13 ...
[trace] Stack trace suppressed: run 'last *:update' for the full output.
[trace] Stack trace suppressed: run 'last *:ssExtractDependencies' for the full output.
[error] (*:update) sbt.ResolveException: unresolved dependency: org.apache.logging.log4j#log4j-core_2.11;2.7: not found
[error] (*:ssExtractDependencies) sbt.ResolveException: unresolved dependency: org.apache.logging.log4j#log4j-core_2.11;2.7: not found
[error] Total time: 5 s, completed Feb 20, 2017 11:24:48 AM</pre><br/>See complete log in <a href="/Users/salvob/Library/Logs/IdeaIC2016.3/sbt.last.log">/Users/salvob/Library/Logs/IdeaIC2016.3/sbt.last.log</a>    enter code here

【问题讨论】:

【参考方案1】:

只需将log4j依赖中的%%替换成%,这样就变成了

   libraryDependencies += "org.apache.logging.log4j" % "log4j-core" % "2.7"

请参阅Build.scala, % and %% symbols meaning 了解说明。

由于 log4j 是 java 库,它在工件坐标中不包含 scala 版本。

【讨论】:

【参考方案2】:

随着 log4j 的新更新,由于 log4j 零日漏洞,建议的代码如下:

libraryDependencies += "org.apache.logging.log4j" % "log4j-api" % "2.15.0"
libraryDependencies += "org.apache.logging.log4j" % "log4j-core" % "2.15.0"

【讨论】:

以上是关于SBT - log4j 的未知工件的主要内容,如果未能解决你的问题,请参考以下文章

scalatest 的“未知工件。未解决或索引”错误

如果使用maven,通常你把log4j.properties放在java或resources下?

本地工件代理背后的 sbt

从 sbt 项目的安全工件下载依赖项

如何使用SBT修改默认工件的内容

SBT 原生打包程序:在 %install 中解压缩工件