Run a sbt-created app without sbt

爷,独闯天下 提交于 2019-12-08 06:40:11

问题


This is somehow a follow-up of this question.

When I'm creating a Scala package with sbt, I am able to run it using one of these ways:

  • simply by typing run in the sbt console

  • or by creating a jar file (using either one-jar or assembly), and the running this jar in the system console by typing java -jar myjar.jar

But I don't know how to

  • run my package from the scala program ? (not the sbt scala console, in which everything works fine by typing import mypackage._)

  • use my Scala package in another project ? for example by importing myjar in another project ?


EDIT : you can forget my questions below about sbt-start-script because now I'm using sbt-native-packager (I have tried it just now and it works, but my previous questions remain open).


I have tried to use sbt-start-script but unsucessfully. The target/start script is well created but I get such errors:

$ sh target/start
target/start: 2: target/start: Bad substitution
Exception in thread "main" java.lang.NoClassDefFoundError: Hi
Caused by: java.lang.ClassNotFoundException: Hi
...

Here I simply have a main.scala file in the src/main/scala folder and it is:

object Hi { def main(args: Array[String]) = println("Hi!") }

I'm using these settings in build.sbt :

import com.typesafe.sbt.SbtStartScript

seq(SbtStartScript.startScriptForClassesSettings: _*)

回答1:


There are several ways how you can use your project in another project. I will not discus the publishing to remote repository, as that's probably something you don't want to do anyway (at least at this point in time).

Lets assume you have a project called projectA - the build.sbt is just this:

name := "project-a"

organization := "com.acme"

version := "0.1.0"

And you have another project called projectB, where you want to use classes defined in projectA.

Unmanaged Dependency

One of the simplest ways is to use it as a unmanaged dependency. You can do that by putting the jar file produced by package, assembly or any other command producing an artefact.

Local Repository

Another way to use your dependency is to publish it to your local repository. Given the projectA as defined above, to the build.sbt of a projectB, add a dependency

libraryDependencies += "com.acme" %% "project-a" % "0.1.0"

Now you can publish projectA to your local repository by executing publishLocal in the projectA. The advantage of this approach is that if your projectA declares any dependencies, they will be added as transitive dependencies to projectB.

Project Dependency

Last way that comes to my mind is to declare dependency directly on the projectA. You can do that by creating a build.sbt file in the projectB, which looks more or less like this

lazy val projectA = file("/home/lpiepiora/q-23607291/projectA")

lazy val projectB = project in file(".") dependsOn projectA

Now classes declared in projectA should be visible in projectB.



来源:https://stackoverflow.com/questions/23607291/run-a-sbt-created-app-without-sbt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!