播放框架:包 javax.inject 不存在

Play framework: package javax.inject does not exist

在我的 Play 2.0 Framework Java 项目中,以下行在 Eclipse 和 sbt 编译步骤中都会产生错误:

import javax.inject.*;

我已经将 javax.inject 依赖项添加到我的 build.sbt 文件中:

libraryDependencies ++= Seq(
    javaCore,
    javaJdbc,
    javaEbean,
    javaWs,
    javaFooBar,
    cache,
    "javax.inject" % "javax.inject" % "1",
    "org.atmosphere" % "atmosphere-play" % "2.1.1"
)

并疯狂地执行了 cleanupdateeclipse with-source=true

[myproject] $ eclipse with-source=true
[info] About to create Eclipse project files for your project(s).
[info] Compiling 3 Scala sources and 12 Java sources to ./myproject/target/scala-2.11/classes...
[error] ./myproject/app/com/elements/legacy/LegacyController.java:3: object inject is not a member of package javax
[error] import javax.inject.*;
[error]        ^
[error] one error found
[error] (compile:compile) Compilation failed
[info] Resolving jline#jline;2.11 ...
[error] Could not create Eclipse project files:
[error] Error evaluating task 'dependencyClasspath': error

我觉得 sbt 不会在无法解决依赖项的情况下抛出错误(例如上面的 javaFooBar)。如何激活?

如何使用 javax.inject 正确构建 Play 2.0 Java 项目?

非常感谢!

编辑:

通过以下方式扩展 project/plugins.sbt 中的存储库列表达到了目的:

// The repositories
resolvers ++= Seq(
    Resolver.sonatypeRepo("snapshots"),
    Resolver.sonatypeRepo("releases"), 
    Resolver.typesafeRepo("snapshots"), 
    Resolver.typesafeRepo("releases")
)

Donovan 描述的 dependencies 命令对于检查依赖是否可以解决非常有帮助!

这看起来像是无法在激活器中重新加载项目定义。

如果我用下面的更新我的build.sbt,项目仍然会正确编译不是因为依赖没有问题但是因为它不知道关于变化。

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  javaWs,
  "foo" % "bar" % "1.0"
)

编译消息:

[exampleApp] $ compile
[success] Total time: 0 s, completed 29-apr-2015 9:13:30

如果我现在 reload 我的项目配置,我们就会开始看到问题。

[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  ::          UNRESOLVED DEPENDENCIES         ::
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  :: foo#bar;1.0: not found
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[trace] Stack trace suppressed: run last *:update for the full output.
[error] (*:update) sbt.ResolveException: unresolved dependency: foo#bar;1.0: not found

如果您添加了需要特殊解析器的依赖项,例如快照等

让我们从 build.sbt 和 reload 中删除该行,以便我们可以正确编译,然后为项目中不存在的包添加导入。

build.sbt(随后重新加载)

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  javaWs
)

Application.java

import play.*;
import play.mvc.*;

import views.html.*;
import foo.bar.*;

public class Application extends Controller {

    public static Result index() {
        return ok(index.render("Your new application is ready."));
    }
}

编译结果为

[error] D:\tmp\exampleApp\app\controllers\Application.java:7: error: package foo.bar does not exist
[error] import foo.bar.*;
[error] ^
[error] 1 error
[error] (compile:compile) javac returned nonzero exit code

这两个错误具有截然不同的特征,结合上面提到的 dependencies 应该可以帮助您找到正确的地方。