Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy a Scala project from Eclipse?

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.

like image 994
lach Avatar asked May 27 '10 13:05

lach


People also ask

How do I run a Scala application in Eclipse?

Open the Eclipse, click on the menu and click on the “New” ->Scala project.

How do I import a Scala project into Eclipse?

To import the Scala IDE in your workspace simply click on File > Import. The Eclipse Import dialog will open. There, select General > Existing Projects into Workspace and click Next.

Can we use Eclipse for Scala?

Eclipse, including the JDT. Either the “Classic” or “Eclipse for Java Developers” is sufficient. Scala IDE v3. 0 can be installed on both Eclipse 3.7 (Indigo) and Eclipse 4.2 (Juno), through different update sites.


2 Answers

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.

like image 133
Thomas Jung Avatar answered Oct 18 '22 10:10

Thomas Jung


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.

like image 5
lach Avatar answered Oct 18 '22 12:10

lach