Creating Single file Runnable Jars in Scala with Eclipse - eclipse

Can anyone tell me how I can create a runnable JAR file from a Scala program using Eclipse and Scala 2.9.x ?
A quick Google brings up results like this ..
http://garyboone.com/2009/06/creating-single-file-runnable-jars-in-scala-and-eclipse/
Which uses a Java class to call the Scala main however it doesn't seem to work with Scala 2.9.
Thanks for your time.
Ian

In Scala 2.9.1, you can simply create a .java file like this to be able to make use of Eclipse's Create Runnable JAR functionality:
public class ScalaRunner {
public static void main(String[] args) {
Main.main(args);
}
}

If, by chance, you're using sbt to build then you can use the assembly-sbt plugin. We use this at Yammer to build fat JARs of our infrastructure services. We're (gradually) switching over to use maven and the shade plugin. I believe shade does a slightly more intelligent job of merging JARs than assembly-sbt does. However, we've had great success so far with assembly-sbt.

Not Eclipse specific, but I would use sbt with the Proguard plugin: https://github.com/siasia/xsbt-proguard-plugin
This lets you setup a reusable build without depending on Eclipse or similar IDEs.

Proguard is a great tool to create an executable jar -- if you're not using sbt (if so, use the sbt-proguard-plugin other folks are advising) a standard way to build the executable jar from within eclipse would be to build an ant build file, and then reference that ant build from within eclipse.
Almost all android developers who use scala use proguard, and you'll find lots of documentation from them about this.
Here's proguard documentation about creating an ant task:
http://proguard.sourceforge.net/index.html#/manual/ant.html
And step 4 from this page about android development from scala IDE wiki has the specific steps for adding the ant build file to eclipse.
http://www.assembla.com/wiki/show/scala-ide/Developing_for_Android

Related

How to build a sdk in scala which can be reused in other projects

I am new to scala and programming and I have written a class in spark scala that is generic and should be reused by other projects. How can I build a sdk in scala sbt so that I can reuse this class without re writing the code again. Any online documentation would help.
What you're calling a "sdk" sounds more like what we call a "library".
The high level steps are:
create your library project (regular sbt project)
configure this project for publishing
publish the project to a repository (Artifactory, Nexus..) or optionally publish locally if you don't need a repository
in any other project, add your library project as a dependency
I would recommend reading the sbt documentation which should cover the most of it. For instance, Publishing with sbt.

how to keep jar dependancy while checking in code

I have a scala project built with dependancy on a locally built jar file (java code). Once I need to check in my scala code into a different environment for building and deployment, what's the best way to keep my jar file in the dependancy?
I know that if I use the sbt dependancy from online modules, I don't need to worry, it will download the version and build, but what if I want to use my own jar file for this purpose?
This is in OSX, and code will be checked into linux machines, I am using intellij and sbt to manage my scala project. I also used intellij to build my external java lib into jar file and added dependancy of this specific path.
I hope there should be some generic solution, but I am new in JAVA and SBT
I got it figured out. Add the jar files under the lib directory right under the project will solve the problem. SBT will pick it up automatically and you can certainly check in the jar files like source code.

How to add new JVM languages e.g. Scala, Clojure, Fantom, Groovy to Eclipse IDE?

What's a prefereed way to download Scala , via scala-lang.org, can it be added directly by the Eclipse IDE or how to add Scala to Eclipse IDE? Is there a convention on how to add a JVM language?
Update
I could add Clojure and Scala from Help...install new software so now I can create those kinds of projects:
You typically install a plugin for that language using Eclipse's build in plugin language.
for instance for Clojure you can search the plugin manager for "counter clockwise" to get Clojure support. for Scala it looks like http://scala-ide.org/ is a good starting point for detailed instructions and a nice screencast on setting this up. It's worth noting that many JVM languages like Clojure don't need to be explicitly installed, they are downloaded as required by standard Java build tools like Maven. Other languages like Groovy are more easily used if you install them explicity.
If you're an IDEA user, by far the best way is to use SBT to manage your projects and use its sbt-idea plug-in (which adds the gen-idea task) to create IDEA projects files and directories.
Ideally, use Paul Phillips' SBT launcher (to get SBT version flexibility if you're ever going to build 3rd-party projects whose specified SBT versions can be all over the map) and configure non-project-specific plugins, such as sbt-idea, in your per-user shared ~/.sbt directory. That way all your projects have it automatically yet it does not intrude on the project definition itself.
Optionally, there is an IDEA plug-in that integrates SBT's interactive console with the IDEA app. The best part of that is compilation errors become hyperlinks to your code.
I don't know if there's an Eclipse counterpart to sbt-idea, but even if there's not, SBT is the way to go.
The JVM languages typically have plugins for Eclipse, e.g. Counterclockwise for Clojure.
In many cases you can also use JVM languages without a plugin in a regular Java project. For example the following approach works with Clojure:
Include clojure-1.4.0.jar as a dependency (using Maven or similar)
Include clojure source files as regular resources in your project
Write a short piece of Java code that executes the clojure code by calling the appropriate methods in clojure.lang.RT (a class provided in Clojure's jar file)

How to build a scala application that was created in eclipse scala plugin FROM THE CL

I have developed a scala application for the first time, but I have to deploy it with a "one-click" type script that can run and build the scala application from source WITHOUT ECLIPSE.
Since I'm completely new to scala I don't know how to tell it where all my source files are etc... to get it to build my app from the command line. I also have 2 3rd party .jar libraries that I need to tell the scala compiler to link to...
Any documentation on this? Or example command lines? My project hierarchy is:
src/packagename: contains all .scala
bin/packagename: contains all.class files
libs/ -> contains 2 .jar files I will need to import somehow
I'm working on debian linux
EDIT: I found this ability to export in eclipse so I created a .java file and called my main scala object from it. Then I exported as a runnable jar. However, when I go to run the new runnable jar "sudo java runnable.jar" it says "class not found exception: runnable.jar"
You should take a look at https://github.com/harrah/xsbt/wiki which is the common way to build a Scala project. Run through the tutorial in the wiki to learn how you should organise your directory structure, so that everything may run fine.
If you want to combine it with eclipse, checkout this plugin: https://github.com/typesafehub/sbteclipse

Creating ANT file for use in Eclipse and CruiseControl

I have a project in Eclipse that currently has no ANT build file, but is about to have one created (to facilitate building jars). I'd like to write the build file so that it would work for both. I was just going to write the full build in ANT but I noticed this line in an IBM Help doc.
The Java builder runs the internal
Eclipse Java compiler which in turn is
responsible for indexing your source
so that searching, refactoring and
many other features are available.
Thus it is not possible to replace the
internal Eclipse Java compiler by
using a project builder. You can
disable the Java builder and you can
control when the Java Builder runs
with respect to the project builders
that you define.
Do I need to write the ANT file so that it conditionally compiles (javac) only outside Eclipse? Are there other things I need to do to make ANT and Eclipse play nicely together?
There are maybe more sophisticated approaches, but here's what works for me. Just set up your Java project in Eclipse to output classes to the same dir as your Ant buildfile does. Let Eclipse compile your project using normal Java compiler.
During typical development, you will probably be changing Java source files and wanting to see that they compile. The Eclipse Java compiler will take care of this. Less frequently you will need to rebuild jars. Ant will take care of that. You can kick off the Ant build when you need to from the command line or from Eclipse.