In order to step up the build process for my web projects I’ve setup a Jenkins server and have a simple build process in place. Now it’s time to taking advantage of automation and improve the quality of software I’m producing. A simple step forward would be to combine js files, minify, gzip, jshint (or jslint) JSDoc, etc. Most of these type of tasks appear to be accomplished using Gradle and there is a nice plugin gradle-js-plugin which takes care of these specific tasks!

This is being done on a CentOS 6.5 machine.

Gradle

The Jenkins Gradle Plugin has the option to automatically install Gradle for you, I have chosen to install manually, but this could be a simpler option depending on your environment.

Download from Gradle

Install Gradle (linux)

Download to server

Unzip

# unzip gradle-1.12-bin.zip

Move to desired location

# mv gradle-1.12 /usr/local/gradle

Set path

# export PATH=$PATH:/usr/local/gradle/bin

Try running it

# gradle
:help
Welcome to Gradle 1.12.

To run a build, run gradle  ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

BUILD SUCCESSFUL

Total time: 5.369 secs

gradle-js-plugin

gradle-js-plugin on github

Gradle JS Intro

http://eriwen.github.io/gradle-js-plugin/

The easiest way to get started with this plugin is to use the some boilerplate gradle build code plugin.gradle from the github repository, just paste the contents into a build.gradle file:

# vi build.gradle

Then execute (as jenkins, from the same directory):

# gradle tasks

If you execute gradle as root the associated temp files will be owned by root and when jenkins attempts to run grandle you will recieve an access denied message.

You should see output indicating that the dependencies are being downloaded:

# gradle tasks
Download http://repo1.maven.org/maven2/com/eriwen/gradle-js-plugin/1.11.0/gradle-js-plugin-1.11.0.pom
Download http://repo1.maven.org/maven2/com/google/javascript/closure-compiler/v20130227/closure-compiler-v20130227.pom
Download http://repo1.maven.org/maven2/args4j/args4j/2.0.16/args4j-2.0.16.pom
Download http://repo1.maven.org/maven2/args4j/args4j-site/2.0.16/args4j-site-2.0.16.pom
Download http://repo1.maven.org/maven2/com/google/guava/guava/14.0/guava-14.0.pom
Download http://repo1.maven.org/maven2/com/google/guava/guava-parent/14.0/guava-parent-14.0.pom
Download http://repo1.maven.org/maven2/org/sonatype/oss/oss-parent/7/oss-parent-7.pom
Download http://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/2.4.1/protobuf-java-2.4.1.pom
Download http://repo1.maven.org/maven2/com/google/google/1/google-1.pom
Download http://repo1.maven.org/maven2/org/json/json/20090211/json-20090211.pom
Download http://repo1.maven.org/maven2/org/apache/ant/ant/1.8.2/ant-1.8.2.pom
Download http://repo1.maven.org/maven2/org/apache/ant/ant-parent/1.8.2/ant-parent-1.8.2.pom
Download http://repo1.maven.org/maven2/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.pom
Download http://repo1.maven.org/maven2/com/googlecode/jarjar/jarjar/1.1/jarjar-1.1.pom
Download http://repo1.maven.org/maven2/org/apache/ant/ant-launcher/1.8.2/ant-launcher-1.8.2.pom
Download http://repo1.maven.org/maven2/com/eriwen/gradle-js-plugin/1.11.0/gradle-js-plugin-1.11.0.jar
Download http://repo1.maven.org/maven2/com/google/javascript/closure-compiler/v20130227/closure-compiler-v20130227.jar
Download http://repo1.maven.org/maven2/args4j/args4j/2.0.16/args4j-2.0.16.jar
Download http://repo1.maven.org/maven2/com/google/guava/guava/14.0/guava-14.0.jar
Download http://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/2.4.1/protobuf-java-2.4.1.jar
Download http://repo1.maven.org/maven2/org/json/json/20090211/json-20090211.jar
Download http://repo1.maven.org/maven2/org/apache/ant/ant/1.8.2/ant-1.8.2.jar
Download http://repo1.maven.org/maven2/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar
Download http://repo1.maven.org/maven2/com/googlecode/jarjar/jarjar/1.1/jarjar-1.1.jar
Download http://repo1.maven.org/maven2/org/apache/ant/ant-launcher/1.8.2/ant-launcher-1.8.2.jar
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build tasks
-----------
combineJs - Combine many JavaScript files into one
gzipJs - GZip a given JavaScript file
minifyJs - Minify JavaScript using Closure Compiler
props2js - Convert Java properties files for use with JavaScript
requireJs - Run the r.js Optimizer to produce Require.js output

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Documentation tasks
-------------------
jsdoc - Produce HTML documentation with JSDoc 3

Help tasks
----------
dependencies - Displays all dependencies declared in root project 'workspace'.
dependencyInsight - Displays the insight into a specific dependency in root project 'workspace'.
help - Displays a help message
projects - Displays the sub-projects of root project 'workspace'.
properties - Displays the properties of root project 'workspace'.
tasks - Displays the tasks runnable from root project 'workspace'.

Verification tasks
------------------
jshint - Analyze JavaScript sources with JSHint

Other tasks
-----------
clean
combine
gzip
individualMinify
jsdocz
jshintz
props

To see all tasks and more detail, run with --all.

BUILD SUCCESSFUL

Total time: 23.212 secs

The build file can be easily configured to match your particular project…

Jenkins Gradle Plugin

Installation of Plugins in Jenkins is slick –

  • Jenkins > Manage Jenkins > Manage Plugins
  • Click  Tab “Available”
  • Filter : gradle
  • Check “Gradle Plugin”
  • Click “Install without restart”
  • Check “Restart Jenkins when installation is complete and no jobs are running”
  • My browser never reloads automatically after a restart…

Jenkins Configuration

Configure

Jenkins > Manage Jenkins > Configure System

  • Scroll down to the Gradle section
  • Click “Add Gradle”
  • Uncheck “Install automatically”
  • name : Gradle 1.12
  • GRADLE_HOME : /usr/local/gradle
  • Scroll to the bottom
  • Click “Save”

Using Gradle Plugin Within a Jenkins Job

First I modified the build.gradle file which is in the Jenkins job workspace directory (/var/lib/jenkins/jobs/{JobName}/workspace/build.gradle) to reflect my projects js source directory.

Jenkins > {JobName} >Configure

  • In the Build section
  • Click “Add build step”
  • Select “Invoke Gradle script”, “Gradle 1.12”
  • Tasks : combine
  • Check Force GRADLE_USER_HOME to use workspace
  • Scroll to the bottom
  • Click “Save”
  • Click “Build Now”

The combined js file will be located at:

/var/lib/jenkins/jobs/{JobName}/workspace/build

Stepping it up a notch we can run multiple tasks by providing a space separated list of Tasks:

clean combine

clean jshintz jsdocz combine individualMinify gzip

Trouble Shooting

FAILURE: Build failed with an exception.

* What went wrong:
java.io.FileNotFoundException: /var/lib/jenkins/jobs/JobName/workspace/.gradle/1.12/taskArtifacts/cache.properties.lock (Permission denied)
> /var/lib/jenkins/jobs/JobName/workspace/.gradle/1.12/taskArtifacts/cache.properties.lock (Permission denied)

The jenkins user doesn’t have permission to modify .gradle contents, just delete the files and re-run the build.

Leave a reply

Your email address will not be published. Required fields are marked *