How to declare zip dependency and know its path in file system? - sbt

I would like to download a zip artifact and find the corresponding file in local repository.
Where I can declare the zip extension ?
libraryDependencies ++= Seq(
"com.acme" % "audit-agent" % "0.7" % "test" // ??? where I put zip ?
)
May be, I can just use some object to reference the artifact, download it, and file the filename ?
Any Idea ?

Use from method of sbt.ModuleID in libraryDependencies as described in Explicit URL:
libraryDependencies += "organization" % "myModuleName" % "1.0" from "https://myhost.pl/slinky.zip"
Then follow How to extract dependency jar to specific folder during compilation? and use update and .filter:
val jar = (update in Compile).value
.select(configurationFilter("compile"))
.filter(_.name.contains("myModuleName"))
.head

There is a more native way:
libraryDependencies += "org" % "name" % "rev" artifacts(Artifact("name", "type", "ext"))
or in your case
libraryDependencies ++= Seq(
"com.acme" % "audit-agent" % "0.7" % "test" artifacts(Artifact("audit-agent", "zip", "zip")))

Related

Where to put "enablePlugins" in SBT?

Following this tutorial, I am asked to add enablePlugins(WindowsPlugin) to my SBT configuration.
I did this by stating exactly this line in my build.sbt but all I get is "Cannot resolve symbol". Do I need to add the dependency somewhere?
Is this an auto plugin and can anyone explain to me what an auto plugin actually is and how I use it?
UPDATE: My build.sbt looks like that:
name := "ApplicationName"
version := "0.3-SNAPSHOT"
scalaVersion := "2.13.1"
enablePlugins(WindowsPlugin)
mainClass in assembly := Some("application.ConfigEditorApplication")
assemblyJarName in assembly := s"application-$version.jar"
assemblyMergeStrategy in assembly := {
case PathList("META-INF", xs#_*) => MergeStrategy.discard
case PathList("reference.conf") => MergeStrategy.concat
case x => MergeStrategy.first
}
libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.9"
libraryDependencies += "commons-io" % "commons-io" % "2.6"
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"
libraryDependencies += "com.typesafe.scala-logging" % "scala-logging_2.13" % "3.9.2"
libraryDependencies += "com.typesafe.akka" %% "akka-actor-typed" % "2.6.3"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.1.1" % "test"
libraryDependencies += "org.scalamock" %% "scalamock" % "4.4.0" % Test
libraryDependencies += "org.mockito" % "mockito-scala_2.13" % "1.11.3"
libraryDependencies += "org.mockito" % "mockito-scala-scalatest_2.13" % "1.11.3"
I found the solution to my problem: From the beginning, I suspected, that the plugin needs to be added before it can be enabled. Unfortunately, nothing of that sort was mentioned in the tutorial I was following.
The plugin which has to be added is the native-packager plugin: addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.7.0").
You should create your auto pluggin in your build.sbt. The build.sbt file must be at the root of your projet, at the same level with the file src.
You have information about it here and here.
In the page you mentioned they say you should set this in your build.sbt. Try this.
// general package information (can be scoped to Windows)
maintainer := "Josh Suereth <joshua.suereth#typesafe.com>"
packageSummary := "test-windows"
packageDescription := """Test Windows MSI."""
// wix build information
wixProductId := "ce07be71-510d-414a-92d4-dff47631848a"
wixProductUpgradeId := "4552fb0e-e257-4dbd-9ecb-dba9dbacf424"
UPDATE
Also, I found this question which is related to yours. It is true, it is an old one, but it might give you some hints. Some answers suggest performing updatings, others deleting and then reimporting the project.

scalaFX standalone execute jar file

Good day! Help me, please. I startup this example
sbt> run
It's okey all play, after
sbt> package
Will build jar file, after double click messge:
Error: A JNI error has occured, please check your installation and try again.
Scala version: 2.12.4. JVM:1.8.0_152. ScalaFX:8.0.102-R11
hello.scala: `
package hello
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.Scene
import scalafx.scene.paint.Color._
import scalafx.scene.shape.Rectangle
object HelloStage extends JFXApp {
stage = new JFXApp.PrimaryStage {
title.value = "Hello Stage"
width = 600
height = 450
scene = new Scene {
fill = LightGreen
content = new Rectangle {
x = 25
y = 40
width = 100
height = 100
fill <== when(hover) choose Green otherwise Red
}
}
}
}
build.sbt:
name := "Scala"
organization := "scalafx.org"
version := "1.0.5"
scalaVersion := "2.12.4"
scalacOptions ++= Seq("-unchecked", "-deprecation", "-Xcheckinit", "-encoding", "utf8")
resourceDirectory in Compile := (scalaSource in Compile).value
libraryDependencies ++= Seq(
"org.scalafx" %% "scalafx" % "8.0.102-R11",)
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
fork := true
This is a Java classpath issue. When you try to execute the resulting JAR file, it cannot find the jar files that it needs to run.
Try the following:
Firstly, copy & paste the following to project/plugins.sbt:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")
This loads the sbt-assembly plugin, which will create a fat JAR file, containing all of the dependencies.
Secondly, change your build.sbt file to the following:
name := "Scala"
organization := "scalafx.org"
version := "1.0.5"
scalaVersion := "2.12.4"
scalacOptions ++= Seq("-unchecked", "-deprecation", "-Xcheckinit", "-encoding", "utf8")
libraryDependencies += "org.scalafx" %% "scalafx" % "8.0.102-R11"
fork := true
mainClass in assembly := Some("hello.HelloStage")
This simplifies what you originally had. The macro paradise compiler plugin is not required, and I also removed the slightly odd resourceDirectory setting.
To create the fat JAR, run the command:
sbt
sbt> assembly
The JAR file you're looking for is most likely located at target/scala-2.12/Scala-assembly-1.0.5.jar. You should now be good to go...
Alternatively, you can add all the necessary JAR files to your classpath. Another plugin that can help with that (you probably shouldn't use it with sbt-assembly) - is sbt-native-packager, which creates installers for you. You can then install your app and run it like a regular application.

Excluding a dependency in creating fat jar using SBT

I am writing a akka application. While creating far jar of application , I dont want scala libraries to be packaged with the jar. My build.sbt looks as follows:
lazy val root = (project in file(".")).
settings(
name :="akka-app",
version :="1.0",
scalaVersion :="2.10.4",
mainClass in Compile := Some("sample.hello.HelloWorld")
)
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.3.4" % "provided",
"com.typesafe" % "config" % "1.2.1"
)
// META-INF discarding
mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) =>
{
case PathList("META-INF", xs # _*) => MergeStrategy.discard
case x => MergeStrategy.first
}
}
But this sbt packages scala with jar. I want only com.typesafe.config library to be present in the jar. Any solution how to achieve this?
You can exclude Scala by modifying the option in the assemblyOption setting:
assemblyOption in assembly :=
(assemblyOption in assembly).value.copy(includeScala = false)

How to tell sbteclipse to ignore src/main/java?

How can I get the sbt-eclipse plugin to ignore adding/creating the src/main/java and src/test/java to the eclipse .classpath?
I dont have these folders and when I run >eclipse the eclipse-sbt-plugin creates those folders and adds to eclipse .classpath.
build.sbt file
name := "myproject"
version := "1.0"
scalaVersion := "2.10.1"
resolvers += "google-api-services" at "http://google-api-client-libraries.appspot.com/mavenrepo"
libraryDependencies += "org.scalatest" %% "scalatest" % "1.9.1" % "test"
libraryDependencies += "junit" % "junit" % "4.10" % "test"
libraryDependencies += "com.novocode" % "junit-interface" % "0.10-M1" % "test"
EclipseKeys.createSrc := EclipseCreateSrc.ValueSet(EclipseCreateSrc.Unmanaged, EclipseCreateSrc.Source, EclipseCreateSrc.Resource)
projects/plugins.sbt file
resolvers += Classpaths.typesafeResolver
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.2.0")
Thanks.
This is the default behavior of sbt to have javaSources and scalaSources in classpath.
Them being in eclipse is just a consequence.
It can be changed with (for only java project):
unmanagedSourceDirectories in Compile := (javaSource in Compile).value :: Nil
or (for only scala project)
unmanagedSourceDirectories in Compile := (scalaSource in Compile).value :: Nil
or just remove them all
unmanagedSourceDirectories in Compile := Nil
You can do it like this:
unmanagedSourceDirectories in Test <<= (sourceDirectory){ src => src / "somerandompathfortestsources" :: Nil}
To see what they are (in sbt console):
show unmanagedSourceDirectories
show sources
...
To see what makes them:
inspect unmanagedSourceDirectories
...
More about:
http://www.scala-sbt.org/0.13.0/docs/Detailed-Topics/Java-Sources.html

How to generate Java files from WSDL and publish them?

I'm trying to create a plugin for sbt 0.12.1 that will generate java files from WSDL, compile them, and then publish the jar.
My project layout is like:
./build.sbt
./project/build.sbt
./project/WsdlBuild.scala
./src/main/wsdl/...many wsdl files...
I'm using axis to generate the java files, and build.sbt looks like:
name := "zxtm-api"
organization := "com.giltgroupe.zeus"
unmanagedBase <<= baseDirectory { base => base / "wsdl-lib" }
libraryDependencies ++= Seq(
"axis" % "axis-wsdl4j" % "1.2.1",
"commons-logging" % "commons-logging" % "1.0.4",
"commons-discovery" % "commons-discovery" % "0.2",
"log4j" % "log4j" % "1.2.8",
"org.apache.axis" % "axis" % "1.4",
"org.apache.axis" % "axis-ant" % "1.4",
"org.apache.axis" % "axis-jaxrpc" % "1.4",
"org.apache.axis" % "axis-saaj" % "1.4"
)
gilt.zxtm.WsdlBuild.wsdlSettings
(There was one jar we couldn't find in any maven repo that's in wsdl-lib)
project/build.sbt is very similar:
libraryDependencies ++= Seq(
"axis" % "axis-wsdl4j" % "1.2.1",
"commons-logging" % "commons-logging" % "1.0.4",
"commons-discovery" % "commons-discovery" % "0.2",
"log4j" % "log4j" % "1.2.8",
"org.apache.axis" % "axis" % "1.4",
"org.apache.axis" % "axis-ant" % "1.4",
"org.apache.axis" % "axis-jaxrpc" % "1.4",
"org.apache.axis" % "axis-saaj" % "1.4"
)
unmanagedBase <<= baseDirectory { base => base / "wsdl-lib" }
So I wrote the code in WsdlBuild.scala to generate the java files, and ended up with something like:
object WsdlBuild extends Plugin {
lazy val wsdlSourceDir = SettingKey[File]("wsdl-source-dir")
lazy val wsdlToJava = TaskKey[Unit]("wsdl-to-java")
lazy val managedSrcDir = file("target/src_managed/wsdl")
val wsdlSettings = inConfig(Compile)(Seq(
compile <<= compile dependsOn wsdlToJava,
javaSource := managedSrcDir,
managedSourceDirectories := Seq(managedSrcDir)
)) ++ Seq(
wsdlToJava <<= (wsdlSourceDir, managedSourceDirectories in Compile, state) map {
(wsdlDir, managedDirs, s) =>
// by convention use the first one. Not obvious why there is
// ever more than one
createJavaFromWsdl(wsdlDir, managedDirs.head, s.log)
},
wsdlSourceDir := file("src/main/wsdl")
)
def createJavaFromWsdl(wsdlDir: File, outputDir: File, log: Logger): File = { ... }
So this sort of works. If I run compile, it generates the wsdl correctly. But if I publish-local, it doesn't compile. So in order to publish or publish-local, and I have to manually compile first.
Any suggestions?
Generating sources and resources is described in this howto of the sbt docs.
In your case, wsdlSettings might look like:
val wsdlSettings = inConfig(Compile)(Seq(
sourceGenerators <+= wsdlToJava,
wsdlSourceDir <<= baseDirectory(_ / "src/main/wsdl"),
wsdlToJava <<= (wsdlSourceDir, sourceManaged, streams) map {
(wsdlDir, managedDir, s) =>
createJavaFromWsdl(wsdlDir, managedDir, s.log)
},
)
Some changes unrelated to your question:
Get the logger from streams. This sends output to a task-specific logger so that you can retrieve it individually. See this howto for more information on this.
Always use absolute paths, often by basing a file on baseDirectory. See Use absolute paths.
The question is quite old, although the problem might be still relevant to someone.
In my case, I approached a very similar problem by having an sh script that does all the dirty work of WSDL generation with wsimport (comes with Java out of the box). A dedicated sbt subproject wraps it as a task and executes on compilation. Such subproject can be easily inserted into any other, bigger sbt setup where you can just add a dependency on it.
Enough talking, here's a template on GitHub that demonstrates exactly that: https://github.com/sainnr/sbt-scala-wsdl-template. Hope it saves someone a good couple of hours messing around with WSDL and build tools. Feel free to fork or improve it if you find it any helpful!

Resources