https://blog.ssanj.net/posts/2011-04-06-ensime-with-emacs.html
Ensime with Emacs
I’ve been toying with the idea of looking for an alternative to Intellij for Scala development for a while now. I tried to use Ensime with Emacs a few months ago and never got it going for one reason or another. More recently, I’ve got Ensime and Emacs working together and thought I’d blog about it for anyone else who had trouble getting everything to work together.
Here are the minimum requrements as stated on the Ensime user manual:
1.Unix(y) or Windows OS 2.JVM Version 6 3.Scala 2.8.1 compatible source and libraries 4.Emacs 22 or later (23 is recommended)
Configuring Emacs for Scala
-
Ensure you have a working installation of scala 2.8.1final.
-
Install emacs. On Ubuntu you can do this with
sudo apt-get install emacs
and on the Mac you can:
brew install emacs
Ensure you have at least version 22 or later.
- Go to your scala_installation_dir/misc/scala-tool-support/emacs/ directory. Copy all .el, .elc files and the Makefile into a location where you want to store these files.
Eg. ~/scalaemacs
-
Copy the contrib/dot-ctags file to your ~/.ctagsfile
-
Using a command shell, change to the above directory and run make to convert the .el files to .elc files.
-
Add the following to your ~/.emacs file:
(add-to-list 'load-path "/path_to_your_elc_files")
(require 'scala-mode-auto)
eg:
(add-to-list 'load-path "~/scalaemacs")
(require 'scala-mode-auto)
You may need to create this file if it does not exist.
- Open a .scala file in emacs to verify syntax highlighting works and other basic scala functionality works.
Installing Ensime
-
Ensime can be downloaded from here. Download the latest version. The current version is -> ensime_2.8.1-0.5.0.tar.gz
-
Extract the archive downloaded to a know location. This will be your ENSIME_ROOT.
Eg. ~/opt/ensime
-
Verify that the ENSIME_ROOT/bin/server.sh file has execute permissions.
-
Add the following to your ~/.emacs file substituting ENSIME_ROOT for where you extracted the archive:
;; Load the ensime lisp code...
(add-to-list 'load-path "ENSIME_ROOT/elisp/")
(require 'ensime)
;; This step causes the ensime-mode to be started whenever
;; scala-mode is started for a buffer. You may have to customize this step
;; if you're not using the standard scala mode.
(add-hook 'scala-mode-hook 'ensime-scala-mode-hook)
;; MINI HOWTO:
;; Open .scala file. M-x ensime (once per project)
Your complete .emacs files should look something like:
(add-to-list 'load-path "~/scalaemacs")
(require 'scala-mode-auto)
;; Load the ensime lisp code...
(add-to-list 'load-path "~/opt/ensime/elisp/")
(require 'ensime)
;; This step causes the ensime-mode to be started whenever
;; scala-mode is started for a buffer. You may have to customize this step
;; if you're not using the standard scala mode.
(add-hook 'scala-mode-hook 'ensime-scala-mode-hook)
;; MINI HOWTO:
;; Open .scala file. M-x ensime (once per project)
Creating a Project with SBT
You need to create an Ensime project for each project you want to use with Ensime. The project details are written to a .ensime file in your project root. Ensime currently has support for SBT, Maven and Ivy. If you don’t have a build system you can also generate a .ensime file through the wizard or by hand.
The following is how to create a project for an existing SBT project:
- Launch emacs
- Type M-x to open the mini-buffer and then type: ensime-generate-ensime-config-gen. A note on the Meta key (or M-) combinations: On linux M-x is Alt+x, while on the Mac it’s Esc+x. Play around until you find which meta key is used on your flavour of OS.
- Specify the root of your project.
- If your project is an SBT project, it automatically detects most settings and you should see a message like: “Your project seems to be of type ‘sbt’, continue with this assumption? (yes or no)”. Choose yes.
- Enter all the other information requested.
- At the end you will see something like “Your project config has been written to /xyz/.ensime. Use ‘M-x ensime’ to launch ENSIME.” Your ensime file has been written and you are ready to use ensime.
- Type M-x and in the mini-buffer type: ensime to launch the Ensime server for your project. Reconfirm the location of your project.
- That’s it! :)
Neat Features
- Type inspection - will dive into details of the type at the cursor
- Automatic member completion (eg. typing “blah”. followed by the Tab key will give you a list of the methods on String.
- Navigation between sources
- Refactoring (Renaming, Optimizing imports etc)
- Source formatting
- SBT support
- Dropping files into the Scala REPL
- Debugging (I haven’t had much luck getting this to work)
For a full list have a look at the online Ensime user manual.
screenshot of ensime’s autocomplete feature
----------------------------------
https://www.scala-sbt.org/1.x/docs/Installing-sbt-on-Linux.html
Installing sbt on Linux
Installing from a universal package
Download ZIP or TGZ package and expand it.
Install JDK
You must first install a JDK. We recommend Oracle JDK 8 or OpenJDK 8. The details around the package names differ from one distribution to another.
For example, Ubuntu xenial (16.04LTS) has openjdk-8-jdk.
Redhat family calls it java-1.8.0-openjdk-devel.
Ubuntu and other Debian-based distributions
DEB package is officially supported by sbt.
Ubuntu and other Debian-based distributions use the DEB format, but usually you don’t install your software from a local DEB file. Instead they come with package managers both for the command line (e.g. apt-get
, aptitude
) or with a graphical user interface (e.g. Synaptic). Run the following from the terminal to install sbt
(You’ll need superuser privileges to do so, hence the sudo
).
echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
sudo apt-get update
sudo apt-get install sbt
Package managers will check a number of configured repositories for packages to offer for installation. sbt binaries are published to Bintray, and conveniently Bintray provides an APT repository. You just have to add the repository to the places your package manager will check.
Once sbt
is installed, you’ll be able to manage the package in aptitude
or Synaptic after you updated their package cache. You should also be able to see the added repository at the bottom of the list in System Settings -> Software & Update
------------------------------------------------------------------
https://alvinalexander.com/scala/sbt-how-to-compile-run-package-scala-project
How to compile, run, and package a Scala project with SBT
By Alvin Alexander. Last updated: December 27 2018
Table of Contents
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.2, “How to compile, run, and package a Scalaproject with SBT.”
Problem
You want to use SBT to compile and run a Scala project, and package the project as a JAR file.
Solution
Create a directory layout to match what SBT expects, then run sbt compile
to compile your project, sbt run
to run your project, and sbt package
to package your project as a JAR file.
To demonstrate this, create a new SBT project directory structure as shown in Recipe 18.1, and then create a file named Hello.scala in the src/main/scaladirectory with these contents:
package foo.bar.baz object Main extends App { println("Hello, world") }
Unlike Java, in Scala, the file’s package name doesn’t have to match the directory name. In fact, for simple tests like this, you can place this file in the root directory of your SBT project, if you prefer.
From the root directory of the project, you can compile the project:
$ sbt compile
Run the project:
$ sbt run
Package the project:
$ sbt package
Discussion
The first time you run SBT, it may take a while to download all the dependencies it needs, but after that first run, it will download new dependencies only as needed. The commands executed in the Solution, along with their output, are shown here:
$ sbt compile [info] Loading global plugins from /Users/Al/.sbt/plugins [info] Set current project to Basic (in build file:/Users/Al/SbtTests/) [success] Total time: 0 s $ sbt run [info] Loading global plugins from /Users/Al/.sbt/plugins [info] Set current project to Basic (in build file:/Users/Al/SbtTests/) [info] Running foo.bar.baz.Main Hello, world [success] Total time: 1 s $ sbt package [info] Loading global plugins from /Users/Al/.sbt/plugins [info] Set current project to Basic (in build file:/Users/Al/SbtTests/) [info] Packaging /Users/Al/SbtTests/target/scala-2.10/basic_2.10-1.0.jar ... [info] Done packaging. [success] Total time: 0 s
Because compile
is a dependency of run
, you don’t have to run compile
before each run
; just type sbt run
.
The JAR file created with sbt package
is a normal Java JAR file. You can list its contents with the usual jar tvf
command:
$ jar tvf target/scala-2.10/basic_2.10-1.0.jar 261 Sat Apr 13 13:58:44 MDT 2013 META-INF/MANIFEST.MF 0 Sat Apr 13 13:58:44 MDT 2013 foo/ 0 Sat Apr 13 13:58:44 MDT 2013 foo/bar/ 0 Sat Apr 13 13:58:44 MDT 2013 foo/bar/baz/ 2146 Sat Apr 13 13:57:52 MDT 2013 foo/bar/baz/Main$.class 1003 Sat Apr 13 13:57:52 MDT 2013 foo/bar/baz/Main.class 759 Sat Apr 13 13:57:52 MDT 2013 foo/bar/baz/Main$delayedInit$body.class
You can also execute the main
method in the JAR file with the Scala interpreter:
$ scala target/scala-2.10/basic_2.10-1.0.jar Hello, world SBT commands
As with any Java-based command, there can be a little startup lag time involved with running SBT commands, so when you’re using SBT quite a bit, it’s common to run these commands in interactive mode from the SBT shell prompt to improve the speed of the process:
$ sbt > compile > run > package
You can run multiple commands at one time, such as running clean
before compile
:
> clean compile
Common SBT commands
At the time of this writing, there are 247 SBT commands available (which I just found out by hitting the Tab key at the SBT shell prompt, which triggered SBT’s tab completion). Table 18-1 show