Play框架:包javax.inject不存在

Phi*_*off 2 java build dependency-management sbt playframework-2.0

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

import javax.inject.*;
Run Code Online (Sandbox Code Playgroud)

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

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

并执行clean,updateeclipse 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
Run Code Online (Sandbox Code Playgroud)

我觉得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")
)
Run Code Online (Sandbox Code Playgroud)

dependenciesDonovan描述的命令非常有助于检查是否可以解决依赖关系!

Ste*_*ner 5

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

如果我使用以下内容更新我的build.sbt,项目仍将正确编译,不是因为依赖项没有问题,而是因为它不知道更改.

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  javaWs,
  "foo" % "bar" % "1.0"
)
Run Code Online (Sandbox Code Playgroud)

编译消息:

[exampleApp] $ compile
[success] Total time: 0 s, completed 29-apr-2015 9:13:30
Run Code Online (Sandbox Code Playgroud)

如果我现在的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
Run Code Online (Sandbox Code Playgroud)

如果您添加了需要特殊解析程序的依赖项,例如快照等,这正是您所看到的.

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

build.sbt(后面是重载)

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  javaWs
)
Run Code Online (Sandbox Code Playgroud)

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."));
    }
}
Run Code Online (Sandbox Code Playgroud)

编译结果会导致

[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
Run Code Online (Sandbox Code Playgroud)

这两个错误具有非常明显的签名,dependencies如上所述,这有助于指导您到达正确的位置.