0

when having a script file (file.sc) with the following content:

import ammonite._, Resolvers._

val mock_client_repo = Resolver.Http("Unisay at bintray","http://dl.bintray.com/unisay/maven",MavenPattern,true)

interp.resolvers() = interp.resolvers() :+ mock_client_repo

import $ivy.`com.github.unisay::mockserver-client-scala:0.2.0`

import org.mockserver.client.server.MockServerClient
import com.github.unisay.mockserver.scala.DSL.Statuses._
import com.github.unisay.mockserver.scala.DSL._

val port = 3000
val host = "127.0.0.1"
implicit val server = new MockServerClient(host,port)

forAnyRequest respond Ok

which I'm trying to execute as follows: amm files.sc
I get the following error:

Compiling file.sc :: loading settings :: url = jar:file:/usr/local/bin/amm!/org/apache/ivy/core/settings/ivysettings.xml :: resolving dependencies :: com.github.unisay#mockserver-client-scala_2.11-caller;working confs: [default]

:: problems summary :: :::: WARNINGS module not found: com.github.unisay#mockserver-client-scala_2.11;0.2.0

==== local: tried

/home/eli/.ivy2/local/com.github.unisay/mockserver-client-scala_2.11/0.2.0/ivys/ivy.xml

-- artifact

com.github.unisay#mockserver-client-scala_2.11;0.2.0!mockserver-client-scala_2.11.jar:

/home/eli/.ivy2/local/com.github.unisay/mockserver-client-scala_2.11/0.2.0/jars/mockserver-client-scala_2.11.jar

==== m2: tried

/home/eli/.m2/repository/com/github/unisay/mockserver-client-scala_2.11/0.2.0/ivy-0.2.0.xml

-- artifact

com.github.unisay#mockserver-client-scala_2.11;0.2.0!mockserver-client-scala_2.11.jar:

/home/eli/.m2/repository/com/github/unisay/mockserver-client-scala_2.11/0.2.0/mockserver-client-scala_2.11-0.2.0.jar

==== central: tried

http://repo1.maven.org/maven2/com/github/unisay/mockserver-client-scala_2.11/0.2.0/mockserver-client-scala_2.11-0.2.0.pom

-- artifact

com.github.unisay#mockserver-client-scala_2.11;0.2.0!mockserver-client-scala_2.11.jar:

http://repo1.maven.org/maven2/com/github/unisay/mockserver-client-scala_2.11/0.2.0/mockserver-client-scala_2.11-0.2.0.jar

:: USE VERBOSE OR DEBUG MESSAGE LEVEL FOR MORE DETAILS Exception in thread "main" ammonite.runtime.tools.IvyThing$IvyResolutionException: failed to resolve ivy dependencies unresolved dependency: com.github.unisay#mockserver-client-scala_2.11;0.2.0: not found

But if running the same code copy pasted from within the repl, everything works fine.

What am I doing wrong?

Eli Golin
  • 373
  • 1
  • 16
  • You could try to place the import at the beginning of the file. I do so and it works. – david.perez Nov 08 '16 at 16:21
  • @david.perez I guess you meant the `import $ivy` part ?! But I can not place it before I've updated my resolvers, it wouldn't make any scene. – Eli Golin Nov 10 '16 at 10:05
  • That's true. It must be a bug in Ammonite. Ammonite is updated very frequently, maby upgrading it, solves the problem. Otherwise I would open a bug in the github repo. – david.perez Nov 10 '16 at 10:56
  • @david.perez Someone else did it more than two weeks ago(#491), but no comment from the maintainers. – Eli Golin Nov 10 '16 at 11:04
  • Yeah issue #491 is still open. I haven't had time to investigate and properly fix it. Hopefully someone can step up to the plate and help out – Li Haoyi Jan 15 '17 at 02:59
  • Seeing that #491 is closed I tried this in 0.9.1 but it chokes on the first `import`. Is this code obsolete or is it my environment? I don't really understand everything that is happening here, like where `Resolvers` comes from. Is it `ammonite.Resolvers`? – Neil Best May 22 '17 at 23:11

1 Answers1

0

http://ammonite.io/#Multi-stageScripts

By default, everything in a script is compiled and executed as a single block. While you can use Magic Imports to load other scripts or Ivy artifacts before your script runs, those can only load "hardcoded" scripts or artifacts, and cannot e.g. load different scripts depending on some runtime variables.

If you want to load different scripts or ivy artifacts depending on runtime values, you can use the runtime-equivalent of magic imports:

  • import $cp becomes interp.load.cp
  • import $file becomes interp.load.module
  • import $ivy becomes interp.load.ivy

in your case, change

import $ivy.`com.github.unisay::mockserver-client-scala:0.2.0`

to

interp.load.ivy("com.github.unisay" %% "mockserver-client-scala" % "0.2.0")
Dyno Fu
  • 8,753
  • 4
  • 39
  • 64