ADempiere: How To Use Scala For Customisations

Scala is a viable alternative to Java. This guide shows how easy it is to develop ADempiere ERP customisations in Scala.

Introduction

Scala is becoming an increasingly popular language and for many companies/developers it's a superior alternative to Java. ADempiere open source ERP/CRM, is written in Java but since Scala runs on JVM and has very good Java interoperability, it's very easy to customise ADempiere with Scala.

We'll see how it can be done.

Prerequisites

The only things you're going to need is
  • ADempiere binaries.  You can either download them from SourceForge or build it directly from source with RUN_build (at the time of this writing 3.7.0LTS was the latest).
  • JDK 1.6
  • Scala 2.10.x  (at the time of this writing 2.10.2 is the latest)
  • SBT 0.12.x (at the time of this writing 0.12.4 is the latest)

Project Template

I have created a template project which is hosted on github to speed up things for people. First clone it: git clone https://github.com/bahmanm/adempiere-scala.git customization.
This project (dependencies and build process) is managed by SBT which is a thin layer over Apache Ivy which tries to have the best of both the Ant and Maven worlds.

Structure

If you take a look inside the customization directory you just cloned, you'll see a directory
structure like the image below. The description for each directory is
  • lib/: For those libraries that don't exist in public or your local Ivy repositories, you just need to drop the jar file into this folder and SBT will take care of it for you.
  • project/: This directory is used by SBT and currently contains plugins.sbt which is the place to add new plugins to your customisation project. Right now there are three plugins defined for the project: sbt-assembly, sbteclipse and sbt-idea.
  • src/main/: Put your main Java or Scala files or resources in this folder to have them in your final customization.jar.
  • src/test/: Put your test classes along with resources required for testing this folder.
  • build.sbt: This file is where build parameters are defined for SBT.

Required Modifications

There is one modification that has to be done manually to the build definition file (I plan to automate this in near future). Open build.sbt in a text editor:
//
// Author:  Bahman Movaqar (Bahman@BahmanM.com)
//
import AssemblyKeys._

assemblySettings

name := "ADempiere Customisations"

version := "0.1"

scalaVersion := "2.10.2"

scalaHome := Some(file("C:/Bin/Scala/2.10.2/"))

javaHome := Some(file("C:/Bin/Java/jdk1.6.0_45"))

javacOptions ++= Seq("-source", "1.6", "-target", "1.6", "-encoding", "utf8")

jarName in assembly := "customization.jar"

excludedJars in assembly <<= (fullClasspath in assembly) map { cp => 
  cp filter { i => i.data.getName == "Adempiere.jar" || 
              i.data.getName == "AdempiereCLib.jar" ||
              i.data.getName == "AdempiereSLib.jar" }
}
Please change lines 12 and 14 to denote the correct version of Scala you have on your system and the proper home of Scala installation path.
Also change line 16 to point to your JDK installation home.

How To Use

Well, using this template is very straight-forward. Just copy Adempiere.jar, AdempiereCLib.jar and AdempiereSLib.jar from your ADempiere installation directory to the lib/ folder and you're done.
You can use the following commands in SBT shell to help you with your development:
  • sbt test: To run all your tests and get a comprehensive report of successes and failures.
  • sbt assembly: To package your customisations into one single Jar file named customization.jar.
  • sbt compile: To compile your code.
  • sbt ~ compile: To have SBT recompile your sources whenever you make a change and save.
  • sbt console: To have a Scala console (REPL) pre-loaded with your customizations and dependencies. Perfect for exploratory programming.

Eclipse Support

You may be wondering how can you use this with Eclipse; let SBT take care of that. Use sbt eclipse to have SBT generate the Eclipse project structure for you. After that it's just importing the project into Eclipse.
If you want to develop in Eclipse using ADempiere sources instead of Adempiere.jar, AdempiereCLib.jar and AdempiereSLib.jar, remember to remove these jar files from the build path of the project and add the required project to the build path instead.

Intellij IDEA Support

If your preferred IDE is IDEA, use sbt gen-idea to have SBT generate the IDEA project structure for you. Now just open the project in IDEA and voila!

Notes

  • The jar files Adempiere.jar, AdempiereCLib.jar and AdempiereSLib.jar are required for your customizations to compile. However they are not included in the final customization.jar.
  • The above jar files are available only after the RUN_setup has been run in your ADempiere installation directory.

Comments

Popular posts from this blog

Variables in GNU Make: Simple and Recursive

Checkmate on Your Terms: A Personal Journey with Correspondence Chess

Firefox profiles: Quickly replicate your settings to any machine