部署无依赖的Scala二进制文件 [英] Deploy Scala binaries without dependencies

查看:116
本文介绍了部署无依赖的Scala二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以将Scala Jar(〜1MB)复制到服务器,然后让SBT提取并运行所需的依赖项(〜40MB)?

Is there an easy way to copy a Scala Jar (~1MB) to a server and then have SBT pull in the dependencies (~40MB) it needs and run it?

我见过sbt-onejar和 sbt-assembly ,但是这些将所有依赖项捆绑到了一起在我的情况下,一个jar变为约45MB,需要很长时间才能上传到服务器。

I've seen sbt-onejar and sbt-assembly, but these bundle all dependencies into one jar which becomes ~45MB in my case which takes too long to upload to the server.

此刻,我使用Capistrano从GitHub检出我的代码并进行编译。然后,我使用xsbt-start-script-plugin运行它-类似于Heroku的管理方式。

At the moment I use Capistrano to checkout my code from GitHub and compile it. I then run it using the xsbt-start-script-plugin - similarly to how Heroku manages this.

问题是,编译在服务器上需要很长时间(我正在使用EC2)。具有约600MB内存的EC2 Micro耗时极长,有时会随机终止该过程。我正在使用目前正在运行的EC2小型实例(1.7GB内存),但是随着代码库的增加和我添加更多服务器的使用,它可能会出现问题。

The problem is that the compilation takes a long time on the servers (I am using EC2). EC2 Micro with ~600MB ram takes insanely long and sometimes randomly kills the process. I am using an EC2 Small instance (1.7GB ram) which is working at the moment, but as the code base increases and I add more servers, it may become problematic.

理想的工作流程是在本地(或在CI服务器上)编译Scala源,复制到服务器,自上次构建以来添加SBT引入其他依赖项(现有依赖项将来自本地缓存的常春藤存储库),然后为我提供一个一个简单的脚本,可以在Ubuntu 10.04上使用Upstart运行该服务。

The ideal workflow would be to compile the Scala sources locally (or on CI server), copy to server, have SBT pull in additional dependencies added since last build (existing ones would come from local cached ivy repo), then provide me with a simple script to run the service with Upstart on Ubuntu 10.04.

我还想听听其他Scala用户如何部署他们的代码。

I would also like to hear how other Scala users deploy their code.

(OP稍后发布的答案中的代码)

(code from "answer" later posted by OP)

FWIW这是我的构建文件。

FWIW here are my build files.

import com.typesafe.startscript.StartScriptPlugin
name := "XXX"
version := "0.1.0"
scalaVersion := "2.9.1"
resolvers += "XXX" at "http://repo.XXX.XXX"
libraryDependencies += "XXXX" %% "backend" % "0.1.0"
seq(StartScriptPlugin.startScriptForJarSettings: _*)
mainClass in Compile := Some("XXX.app.Main")



project / build.sbt



project/build.sbt

resolvers += Classpaths.typesafeResolver
addSbtPlugin("com.typesafe.startscript" % "xsbt-start-script-plugin" % "0.5.0")
addSbtPlugin("com.eed3si9n" % "sbt-dirty-money" % "0.0.1")


推荐答案

将.jar部署到存储库中(我使用 Artifactory ,但是我认为您可以通过scp发布到服务器可以访问的文件系统。创建一个空sbt项目,该项目仅对您应用的.jar文件具有依赖性。您的服务器可以拉出并 sbt更新这个空项目,以利用本地Ivy缓存的优势并且对带宽非常友好的方式将jar下载到服务器。然后,只需获取正确的类路径来启动您的应用即可。

Deploy your .jar to a repository (I use Artifactory, but I think you can publish via scp to a filesystem) that your servers can access. Create an empty sbt project that has a single dependency on your app's .jar file. You servers can pull and sbt update this empty project to download the jars to the server in a way that takes advantage of the local Ivy cache and is very bandwidth friendly. Then it's just a matter of getting the right classpath to launch your app.

您需要注意的一件事是确保sbt确实会更新您的依赖项。在有关此问题的对话 = https://groups.google.com/forum/#!forum/simple-build-tool rel = nofollow noreferrer> SBT的邮件列表。

One thing you have to watch out for is to make sure that sbt will actually update your dependencies. There's been some conversation about this on SBT's mailing list.

启动应用程序的选项(以提高智能程度为顺序):

Options for launching the app (in order of increasing cleverness) are:


  • 只需运行 sbt运行

  • 使用 sbt-onejar sbt-assembly 创建一个.jar并运行 java -jar 运行它

  • 编写一些sbt脚本来构建类路径,并使用该脚本直接从Ivy缓存中启动应用程序。 我似乎回想起最近有一个脚本可以在SO上执行此操作,但是目前无法找到它。您可以找到一个执行此操作的脚本在此SO答案中,或(如您在下面建议的那样)使用 xsbt-start-script-plugin

  • Just run sbt run
  • Use sbt-onejar or sbt-assembly to create a single .jar and run java -jar to run it
  • Write some sbt scripts to build the classpath, and use that to launch the app directly out of the Ivy cache. I seem to recall seeing a script to do that recently on SO, but can't find it at the moment. You can find a script that does this in this SO answer, or (as you suggested below) use the xsbt-start-script-plugin.

这篇关于部署无依赖的Scala二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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