(Quick Reference)

4 Gradle

Version: 6.3.0

4 Gradle

Gradle

This section of the documentation discusses configuration and setup of the asset-pipeline Gradle plugin. Most framework integrations use this plugin for builds so it is important to know how it works.

4.1 Getting Started

Getting Started

Getting started with Gradle is very quick and easy. The asset-pipeline-gradle plugin is included in both the Gradle plugin repository as well as Maven. For those of you not familiar with Gradle it is a very powerful build tooling system that you should definitely check out.

Asset-Pipelines build classes are not directly dependent on Gradle and can be easily integrated into other build tooling frameworks like Maven.

In its simplest form the build.gradle file might look like this:

build.gradle
buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "org.graceframework.plugins:asset-pipeline-gradle:{project-version}"
  }
}

apply plugin: "org.graceframework.asset-pipeline"

Or even simpler:

build.gradle
//Gradle 2.1+
plugins {
  id "org.graceframework.asset-pipeline" version "{project-version}"
}

When this plugin is added a couple tasks and extensions are automatically registered:

  • assetCompile - Compiles and packages assets from src/assets into build/assets

  • assetPluginPackage - Copies all assets unprocessed into an archive useful for plugin jars similar to webjars.

  • assetClean - Cleans and resets the build/assets directory.

All of these tasks support Gradle’s incubating continuous build feature. Simply prefix the task with -t.

Create a folder src/assets and subfolders:

  • javascripts

  • stylesheets

  • html

  • images

  • …​

Now assets can be packaged and processed with ease and performance far outpacing other static asset build tooling.

4.2 Configuration

Configuration

The asset-pipeline Gradle plugin provides several very useful configuration options. An extension called assets is automatically added to Gradle when the plugin is applied that provides several options:

build.gradle
assets {
  minifyJs = true
  minifyCss = true
  enableSourceMaps = true
  maxThreads = 4 //useful for concurrent asset processing during build
  configOptions = [:] //useful for custom config on extension libraries

  minifyOptions = [
    optimizationLevel: 'SIMPLE',
    angularPass: true // Can use @ngInject annotation for Angular Apps
    excludes: ['**/bundle.js'] // Example of excluding specific resources from minification, e.g., if it was minified externally
  ]

  includes = []
  excludes = ['**/*.less'] //Example Exclude GLOB pattern

  //for plugin packaging
  packagePlugin=false //set to true if this is a library

  //developmentRuntime can be turned off
  developmentRuntime=true

  //if you want to customize the jar task this task runs on you can specify a jarTaskName
  jarTaskName=null

  // Can add custom asset locations (directories or individual jar files)
  from '/vendor/lib'
  from '/path/to/file.jar'
}

The default settings will work for most projects, but if one wanted to add a custom source file location the from directive can be quite useful. Asset-pipeline’s broad Resolver support makes it easy to include assets from a diverse set of locations (even webjars).

A common configuration change for assets may be the includes/excludes patterns. These allow one to specify specific files that should not be individually required or compiled because it does not make sense for them to be individually compiled. In the example above: all .less files are excluded. This does not exclude processing however when a require directive is used making it very easy to only package and bundle assets that the final website needs. It is also important to note that these patterns do not have to be GLOB patterns but can also be regular expressions by prefixing the pattern with regex:. By default all files prefixed with an underscore are automatically excluded from individual processing (similar to SASS).

Tuning the includes/excludes patterns can dramatically reduce the build time of your project.

Some additional options are also provided for configuring what types of output assets are generated:

build.gradle
assets {
  enableDigests = true
  skipNonDigests = false
  enableGzip = true
  maxThreads = 4
}

By default both digested named versions of assets as well as non digested named versions are generated as well as the gzip equivalents. Some frameworks dont actually need the non digested named versions (spring-boot, and servlets). These frameworks take advantage of the manifest.properties file discussed earlier in the documentation to properly map requests for non digested file names to the digested equivalent and preserve compatibility with libraries that may not work well with digested file names.

Change skipNonDigests to true to help reduce the file size of your final generated project.

4.3 JVM Based Dependencies

JVM Based Dependencies

When working on any jvm based Gradle project, the asset-pipeline plugin automatically detects all dependencies on the classpath and scans them for assets. This makes it easy to package assets in shared libraries for multiple apps to use or to even include assets that are included from a webjar dependency.

If a library is simply a webjar or asset library, these packages are not needed in your final application package and can be set to provided or compileOnly.