简体   繁体   中英

Running Wget in scala build.sbt

I have a requirement where I need to download a bunch of jars from a url and then place them inside a lib directory and then add them to unmanaged dependancy.

I am kind of stuck on how to do this in build.sbt . Went over sbt documentation and I found processbuilder. With that in mind, I came up with the below code.

for(i <- jarUrls) {
  val wget = s"wget -P $libDir $anonUser@$hgRootURL/$i"
  wget !
}

This runs wget on a bunch of jars and then places the file in the mentioned folder. Pretty simple code, but I am unable to run this. The error that I get is "Expression of type Unit must confirm to DslEntry in SBT file".

How to accomplish this?

build.sbt isn't just scala file, sbt does special preprocessing on it (that's why you don't have to def project = etc).

Your problem happens because every line of code (except imports and definitions) in build.sbt must return expression of type DslEntry as sbt sees every line of code as setting. When do you want your wget to get executed? usual way is to define Task :

lazy val wget = taskKey[Unit]("Wget")

wget := {
  for(i <- List(1,2,3)) {
    val wget = s"wget -P $libDir $anonUser@$hgRootURL/$i"
    wget !
  }
  ()
}

and run it like sbt wget .

You can also make wget task dependent on some other task (or you can think of them as events) in sbt.

See http://www.scala-sbt.org/0.13/docs/Tasks.html

Of course, there are tricky unsafe ways, like:

val init: Unit = {
   //any code you want here
}

But I wouldn't recommend it since you probably want those files during let's say compile stage or something:

wget := {
  your code here
} dependsOn compile

you can also use regular scala build instead of build.sbt : http://www.scala-sbt.org/0.13/docs/Full-Def.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM