如何从 Eclipse 部署 Scala 项目?

发布于 2024-09-03 08:26:18 字数 214 浏览 2 评论 0 原文

我在 Eclipse 中有一个 Scala 项目,需要将其打包以便可以将其部署到服务器。它基于 Jetty,但作为独立应用程序运行。它包含 Scala 类、Java 类和许多第 3 方 jar。

我以为 Scala Eclipse 插件中会有某种部署选项,但我却一片空白。

将 Scala 项目打包成可运行文件以便部署的最简单方法是什么?

非常感谢任何帮助。 干杯。

I have a Scala project in Eclipse that I need to package up so I can deploy it to a server. It's based on Jetty but it runs as a standalone application. It contains Scala classes, Java classes and a number of 3rd party jars.

I assumed there would be some kind of deployment option in the Scala Eclipse plugin but I've drawn a blank.

What is the simplest way to package the Scala project into a runnable file so it can be deployed?

Any help greatly appreciated.
Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光病人 2024-09-10 08:26:18

Sbt 支持网络应用部署。您当然可以使用 Ant、Maven 或其他构建工具来打包 WAR 以进行 Jetty 部署。您可以从 Eclipse 中将 sbt 作为外部工具启动。

Sbt 似乎支持 JRebel 配置。一旦运行,您应该能够从 Eclipse 更改资源和类,而无需额外的战争部署步骤。

Sbt supports web application deployments. You can of course use Ant, Maven or another build tools to package your WAR for Jetty deployment. You can start sbt as an external tool from eclipse.

Sbt seems to support JRebel configuration. Once it runs you should be able to change resources and classes from eclipse without an additional war deployment step.

金橙橙 2024-09-10 08:26:18

经过大量的试验和错误,这是我发现的打包 Scala 应用程序以进行分发的最佳方法:

首先,创建一个 Java 类作为应用程序的主要入口点,如 加里·布恩。这允许您使用 java 命令从 JAR 运行应用程序。我发现使用 java 命令运行 Scala 类是有问题的,即使源路径上有 Scala 库:

import java.util.ArrayList;

import scala.tools.nsc.MainGenericRunner;


public class Main { 
    public static void main (String[] args) {
        ArrayList<String> argList = new ArrayList<String>();
        argList.add("fully.qualified.ClassName");
        for (String s : args) {
            argList.add(s);
        }
        MainGenericRunner.main(argList.toArray(new String[0]));
    }
}

现在您可以使用 Eclipse 的 Export Runnable JAR 命令来打包所有类和库到 JAR 文件中。将 JAR 的主类设置为 Java 入口点。您还可以将 Eclipse 生成的输出设置保存为 ANT 构建文件,以便进行调整。使用 ANT 创建带有 Java 入口点的 JAR 产生了最佳结果。您还可以通过这种方式打包其他 JAR 依赖项,这使得尝试在不同主机上运行 JAR 时变得更加简单。您至少需要 Scala 库和 Scala 工具 JAR。

<zipfileset excludes="META-INF/*.SF" src="${scala.lib.jar}"/>
<zipfileset excludes="META-INF/*.SF" src="${scala.tools.jar}"/>

如果您像我一样使用嵌入式 Jetty,则可以使用以下命令将服务器作为守护进程运行 (source):

nohup java -jar MyJettyServer.jar < /dev/null >> server.log 2>> server_error.log &

这会将程序作为独立于当前用户会话的后台进程运行,因此在您注销主机后该进程将继续。

After a lot of trial and error, this is the best method I found for packaging the Scala app for distribution:

First, create a Java class to be the main entry point for the application as described by Gary Boon. This allows you to run the application from a JAR with the java command. I found that running a Scala class with the java command is problematic, even when you have the Scala libs on the source path:

import java.util.ArrayList;

import scala.tools.nsc.MainGenericRunner;


public class Main { 
    public static void main (String[] args) {
        ArrayList<String> argList = new ArrayList<String>();
        argList.add("fully.qualified.ClassName");
        for (String s : args) {
            argList.add(s);
        }
        MainGenericRunner.main(argList.toArray(new String[0]));
    }
}

Now you can use Eclipse's Export Runnable JAR command to package up all your classes and libraries into a JAR file. Set the JAR's main class to the Java entry point. You can also save the Eclipse-generated output settings as an ANT build file so you can make adjustments. Using ANT to create the JAR with a Java entry point yielded best results. You can also package up other JAR dependancies this way which makes it a whole lot simpler when trying to run the JAR on a different host. As a minimum you will need the Scala library and the Scala tools JAR.

<zipfileset excludes="META-INF/*.SF" src="${scala.lib.jar}"/>
<zipfileset excludes="META-INF/*.SF" src="${scala.tools.jar}"/>

If you're using embedded Jetty, as I am, you can run the server as a Daemon process using the following command (source):

nohup java -jar MyJettyServer.jar < /dev/null >> server.log 2>> server_error.log &

This runs the program as a background process which is independent of the current user session so the process will continue after you logout of the host.

假扮的天使 2024-09-10 08:26:18

我正在做完全相同的事情:开发在嵌入 Jetty 的普通应用程序中运行的服务器端代码。现在主要是Scala 代码,以前是Java。无论哪种方式,我都只是从一个或多个 JAR 文件运行 Java 代码。

在这两种情况下,我都使用 Ant 进行部署构建。我的 Scala 项目有时会使用现有的 Java 类。为此,我添加了一个 Ant 目标,该目标将 Java 类编译成 JAR,随后在我的 Eclipse Scala 插件和 Ant 部署构建目标中使用该 JAR。

I am doing exactly the same thing: develop server side code that runs inside a plain application that embeds Jetty. Nowadays it is mostly Scala code, it used to be Java. Either way I simply run the Java code from one or more JAR file(s).

In both cases I use Ant for deployment builds. My Scala projects sometimes use existing Java classes. For that I add an Ant target that compiles the Java classes into a JAR that is subsequently used both in my Eclipse Scala plugin and in the Ant deploy build targets.

旧瑾黎汐 2024-09-10 08:26:18

一个简单的解决方案是创建一个小的 Java 类来调用 Scala 程序的 main 方法。然后,就可以使用标准的eclipse jar导出了。

public class Main { 
    public static void main (String[] args) {
        MyScalaObjectWithMainMethod.main(args);
    }
}

重要提示:在导出之前,请确保 scala-library.jar 作为外部 jar(而不是库)位于构建路径中,以便它将与您的项目一起导出罐。

我发现这个解决方案是 Jesper Villadsen 在 Gary Boone 的页面(他提出了一种有些复杂的方法)。虽然我在运行 Gary 的解决方案时遇到了问题,但 Jesper 的这个解决方案非常适合我。谢谢!

A simple solution is to create a small Java class that calls the main method of the Scala program. Then, the standard eclipse jar export can be used.

public class Main { 
    public static void main (String[] args) {
        MyScalaObjectWithMainMethod.main(args);
    }
}

Important: Before exporting make sure that scala-library.jar is in the build path as an external jar (not as a library) so that it will be exported together with your jar.

I found this solution as a comment by Jesper Villadsen on Gary Boone's page (who proposes a somewhat complex approach). While I had problems getting to run Gary's solution, this solution by Jesper works perfectly for me. Thanks!

一梦浮鱼 2024-09-10 08:26:18

您可以使用 eclipse 的功能来创建 WAR 文件,就像创建 Java 一样。或者是一个 jar 文件,就像 Java 一样。因为,在运行时它是java。
不再需要魔法了。

You use eclipses' ability to create a WAR file, just like you would for Java. Or a jar file, just like you would for Java. Because, at run time it is java.
No more magic needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文