Showing posts with label gradle. Show all posts
Showing posts with label gradle. Show all posts

Saturday, January 11, 2014

Using make to build and deploy LeJOS programs on the EV3

As discussed earlier my aim is to automate the repetitive nature of the edit-build-deploy-run cycle for programming the EV3 using LeJOS. To that end I set up build.gradle to build the project from the command-line as explained here. Furthermore I set up wifi on the EV3 (using a usb dongle). The next step was to edit ~/.ssh/config and add an entry for the EV3:
  Host ev3

      User root
      HostName 192.168.1.16
      IdentityFile ~/.ssh/abid_ev3
Note: Since the wifi setup on the EV3 is DHCP (not static) the IP address is liable to change, in which event one has to modify ~/.ssh/config accordingly.

The next step is to create a Makefile in your project root. Here is a link to the one I'm using for my "Rover" project. Note the extensive use of PHONY and EMPTY targets to ensure that build and scp tasks are only performed when needed. make once correctly set up takes care of the dependencies. I only need to issue make run from the terminal and the source is compiled and the jar file copied to the EV3 only if needed. Sweet and simple, just the way I like it.

You can take a look at the complete Rover source code on github. There you will also find a number of other repos for my various LeJOS EV3 projects.

Building LeJOS programs for EV3 using gradle

With the wifi connection (including ssh) to the EV3 running and IntelliJ set up to write programs, development for the EV3 was coming along nicely. There was still an issue, however, with the time it took to make a change in the code, compile it, copy it to the EV3 (using scp), run it on the EV3 and then repeat. Since repetition is the bane of all programmers I started looking in to ways to automate these tasks. Of course the ubiquitous make tool came instantly to mind. I would just write a make script (Makefile) to automate these tasks. But how to automate the build task that creates the jar file from the source code, and in such a way that make could do it.

The solution is gradle. Gradle is a project automation tool with excellent support for Java (Google now recommends using gradle and Android Studio - a port of IntelliJ - for Android development). Though gradle has somewhat of a learning curve, it is well worth it, since only after using gradle have I finally begun to understand how a java project comes together. Furthermore with gradle one can build projects from the command line by simply issuing the command gradle build.

Gradle installation is straightforward and is explained here. Once installed the next step is to create a build.gradle file in the root folder of your LeJOS project (your source code). The build.gradle for one of my earlier projects looks like this:

apply plugin: 'java'

def main_class = "Rover"          // We define the main class of the project

// We set the Version of the Java SDK to the one on the system: 1.6
sourceCompatibility = 1.6

sourceSets {

    main {

        // We specify the source directories for the project. The first one is created by us while the latter two are part of the LeJOS source and provide
        // essential classes for controlling the EV3.
        java {
            srcDirs = ['src', '/home/abid/applications/lejos/ev3/DBusJava/src', '/home/abid/applications/lejos/ev3/ev3classes/src']
        }

        // We specify the jar library required to compile the project. This provides the com.sun.jna package
        dependencies {
            compile files('/home/abid/applications/lejos/ev3/ev3classes/lib/jna-3.2.7.jar')
        }

        // We specify manifest attributes (key-value pairs that must appear in the META-INF/MANIFEST>MF file inside the output Jar for the EV3 to run it correctly.
        // Additionally we supply the name of the Main-Class which is the starting point of the program execution.
        jar {
            manifest {
                attributes("Main-Class": main_class, "Class-Path": "/home/root/lejos/lib/ev3classes.jar /home/root/lejos/libjna/usr/share/java/jna.jar")
            }
        }
    }
}
This is pretty much a template build.gradle file for any LeJOS EV3 project. The one-time alterations needed are to change the latter two srcDirs to point to the locations in the local LeJOS git repo as well as the jna jar dependency in the same.

The only thing one needs to alter (from project to project) is the name of the main class defined in the variable main_class near the beginning. And you are good to go. Execute gradle build and the project will compile and place the results in the build/ sub-folder.

Next time I will show you how to incorporate all of this in to one epic Makefile complete with dependencies.