views:

364

answers:

4

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.

+4  A: 

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.

Thomas Jung
Thanks for the input Thomas. I'm afraid I'm a bit of a newb when it comes to Java build tools. I'm not sure if a WAR file is what I want. I'm running Jetty in embedded mode so I was hoping to package everything up in to a runnable JAR or something. Or am I barking up the wrong tree?
lach
A WAR is the standard (SRV.9.6 Web Application Archive File - http://jcp.org/aboutJava/communityprocess/mrel/jsr154/index2.html) way to package your application for a servlet-container (http://wiki.eclipse.org/Jetty/Howto/Deploy_Web_Applications).
Thomas Jung
+2  A: 

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.

Silvio Bierman
Hi Silvio. Thanks! That sounds like just what I need. Could you post an example of the ANT file you use to build your server app?
lach
You can find a clumsy example at:http://www.jambo-software.com/download/build.xml
Silvio Bierman
A: 

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.

Jim Barrows
No, this doesn't work for me. There is no selectable option for the class to execute when the jar is run. Maybe if you hand-crafted the jar it would work?
chrisdew
+1  A: 

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.

lach