Calculus5000
Calculus5000

Reputation: 427

Adding MongoDB to SBT in a Java only project

I'm currently learning the Play! framework and the project that I'm going through uses sbt as its build tool (not Maven). I've been trying to add MongoDB as a dependency to build.sbt, but haven't been successful thus far.

Is there something that I've missed out? Btw, why don't I need to list JUnit as a dependency?

build.sbt file:

name := "warehouse"
version := "1.0-SNAPSHOT"
autoScalaLibrary := false

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

libraryDependencies += "org.mongodb" % "mongodb-java-driver" % "3.2.0"

play.Project.playJavaSettings

Also added this to application.conf (link):

# The mongo module
module.mongo=${play.path}/modules/mongo

# mongodb connection details
mongo.host=localhost
mongo.port=27017
mongo.database=play

EDIT: The below is the compile-time error message that I get when executing the play compile command in the terminal:

[info] Resolving org.mongodb#mongodb-java-driver;3.2.0 ...
[warn]  module not found: org.mongodb#mongodb-java-driver;3.2.0
[warn] ==== local: tried
[warn]   ~/Developer/Play/play-2.2.6/repository/local/org.mongodb/mongodb-java-driver/3.2.0/ivys/ivy.xml
[warn] ==== Maven2 Local: tried
[warn]   file:~/.m2/repository/org/mongodb/mongodb-java-driver/3.2.0/mongodb-java-driver-3.2.0.pom
[warn] ==== public: tried
[warn]   http://repo1.maven.org/maven2/org/mongodb/mongodb-java-driver/3.2.0/mongodb-java-driver-3.2.0.pom
[warn] ==== Typesafe Releases Repository: tried
[warn]   http://repo.typesafe.com/typesafe/releases/org/mongodb/mongodb-java-driver/3.2.0/mongodb-java-driver-3.2.0.pom
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  ::          UNRESOLVED DEPENDENCIES         ::
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  :: org.mongodb#mongodb-java-driver;3.2.0: not found
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
sbt.ResolveException: unresolved dependency: org.mongodb#mongodb-java-driver;3.2.0: not found
.
.
.
[error] (*:update) sbt.ResolveException: unresolved dependency: org.mongodb#mongodb-java-driver;3.2.0: not found

Upvotes: 0

Views: 1048

Answers (1)

Jerry
Jerry

Reputation: 492

The problem you met is that your project can not resolve the dependency which I have met ever before. Before answering your question, I want to talk something about dependency resolving process as follows.

When your project needs to resolve dependency, it will try to find the dependency in some repositories. The repository accessed sequencely is as following

Firstly, it steps into the repository of your project, as for your project, whose directory is ~/Developer/Play/play-2.2.6/repository/ .

If not found, the maven repository will be searched whose directory is ~/.m2/repository/

If dependency is also not found, it will access the repository on the internet, such as http://repo1.maven.org/maven2, then download it to the local.

Maybe your network does not support that your access to http://repo1.maven.org/maven2, which I think it's the reason.

You can solve this problem by doing the following things:

1) download the jar file, http://central.maven.org/maven2/org/mongodb/mongo-java-driver/3.2.0/mongo-java-driver-3.2.0.jar

2) publish it the the local maven repository using the following command

mvn install:install-file -Dfile=~/mongo-java-driver-3.2.0.jar -DgroupId=org.mongodb -DartifactId=mongo-java-driver -Dversion=3.2.0 -Dpackaging=jar

note that

If you download the driver into directory "~/", then

-Dfile=~/mongo-java-driver-3.2.0.jar

If you have not installed mvn, just install it. Then, reopen your project.

Have a good luck

Upvotes: 1

Related Questions