Close

Gradle Gradle Gradle!

A project log for Chirp! A Low Cost Function Generator

A cheap function generator for my lab that can do sine, square, and triangle waves and be automated using serial commands

mike-lembergerMike Lemberger 08/21/2016 at 03:220 Comments

The first hurdle I ran into while moving to a continuous integration tool (i.e. Travis), was migrating my build process from using the Eclipse IDE to a command line build tool. There are several options out there for this, but I decided on gradle for this project's build tool.

What is gradle? It is a flexible general purpose build tool, like make, maven, or ant. Being an embedded c programmer, I was very familiar with make but wanted to learn more about some of the newer build tools out there, which is mainly why I chose gradle.

One of the nice things over make is that gradle already has a build plugin for java. For those of you new to gradle, I would recommend checking out the gradle plugin for java.

Modify Default Gradle Folders

I wanted to change the gradle the default directory from src/main/java and /src/main/resources to src/ and lib/ for its source folder and resources folder respectively.

sourceSets {
	main.java.srcDir 'src/'
	main.resources.srcDir 'lib/'
}

Append Version Number to Binary

Next, I wanted to append the version number to the executable JAR file so I could quickly and easily identify the version of code I was running. Gradle has the ability to pass in parameters to the script with the -P option. If the parameter prodVersion is specified, then this version is applied to the suffix of the JAR file. If no value is passed in, then -Developer-Build is appended.

allprojects {
   if (project.hasProperty('prodVersion')) {
     project.version = project.prodVersion
   } else {
     project.version = 'Developer-Build'
   }
}

Include Libraries In JAR File

The default gradle JAR task was insufficient for creating the executable JAR file since nrserialjava.jar needs to be included in the JAR file. After doing some searching, I found that this could be accomplished with this code:

task fatJar(type: Jar) {
	manifest {
		attributes 'Main-Class': 'controller.Application'
    }

	baseName = project.name
	from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
	with jar
}
Now when running the command `gradle fatJar` all compiled JAR files will be included in the executable JAR

Extend Gradle JAR Task

When Travis does a deploy, it calls gradle assemble, which in turn calls gradle jar. Instead of trying to figure out how to have Travis call gradle fatJar I figured an easier solution was to extend the jar task with fatJar. This was done by adding the following code to build.gradle:

jar.finalizedBy(fatJar) 

Now, anytime jar is called, fatJar will always be called.

Opening Project with Eclipse

I prefer Eclipse for my IDE but would prefer not maintaining Eclipse project settings file and a gradle build file. Gradle will automatically generate an Eclipse .project and .classpath with the command:

gradlew eclipse

For the full build.gradle file, please check out the ChirpUI project on GitHub.

Discussions