在没有sbt的情况下运行sbt创建的应用程序 [英] Run a sbt-created app without sbt

查看:191
本文介绍了在没有sbt的情况下运行sbt创建的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是以某种方式跟进这个问题

当我使用sbt创建Scala包时,我可以使用以下方法之一运行它:

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


  • 只需在sbt控制台中键入 run

或创建一个jar文件(使用 one-jar 程序集),并通过键入 java -jar myjar.jar

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

但我不知道如何


  • 从scala程序运行我的包? (不是sbt scala控制台,通过输入 import mypackage ._ )一切正常。

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

在另一个项目中使用我的Scala包?例如,在另一个项目中导入 myjar

编辑:您可以忘记以下关于 sbt-start-script 的问题,因为现在我正在使用 sbt-native-packager (我刚刚尝试过它,但是我之前的问题仍未解决)。

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曾试图使用 sbt-start-script 但不成功。 target / start 脚本创建得很好,但是我收到了这样的错误:

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
...

这里我只有 main.scala 文件位于 src / main / scala 文件夹中,它是:

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!") }

我在 build.sbt 中使用这些设置:

import com.typesafe.sbt.SbtStartScript

seq(SbtStartScript.startScriptForClassesSettings: _*)


推荐答案

有几种方法可以在另一个项目中使用你的项目。我不会讨论发布到远程存储库,因为这可能是你不想做的事情(至少在这个时间点)。

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).

让我们假设你有一个名为 projectA 的项目 - build.sbt 就是这样:

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

name := "project-a"

organization := "com.acme"

version := "0.1.0"

您还有另一个名为 projectB 的项目,您希望在其中使用 projectA

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

最简单的方法之一就是使用它作为非托管依赖。您可以通过放置 package 生成的 jar 文件来执行此操作,程序集或产生人工制品的任何其他命令。

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.

使用依赖关系的另一种方法是将其发布到您的本地存储库。给定上面定义的 projectA ,到 projectB build.sbt ,添加依赖项

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"

现在,您可以通过执行 publishLocal projectA 发布到本地存储库c>在 projectA 中。这种方法的优点是,如果您的 projectA 声明任何依赖项,它们将作为传递依赖项添加到 projectB

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.

我想到的最后一种方法是直接在 projectA 上声明依赖。您可以通过在 projectB 中创建一个 build.sbt 文件来实现这一点,该文件看起来或多或少像这样

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

现在在 projectA 中声明的类应该是在 projectB 中可见。

Now classes declared in projectA should be visible in projectB.

这篇关于在没有sbt的情况下运行sbt创建的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆