This Gradle plugin allows you to declare client side dependencies in build.gradle from bower, npm, yarn or git and with a much clearer dependencies resolution model. This plugin queries the registries directly so it doesn’t require or install node, npm or bower.
The plugin adds the following tasks to your build:
Task |
Description |
clientInstall |
Installs all the client dependencies you have set in your build.gradle |
clientRefresh |
Refreshes client dependencies (if you add or modify your configuration) |
clientClean |
Removes client dependencies and clears the cache |
clientReport |
Prints a report of all dependencies and the resolved versions |
You can specify dependencies from different sources (npm, bower or git) and control which files are included in your project by using the DSL shown below.
|
The following example shows the use of several different types of registries, this works but it’s best to stick to a single registry (NPM is recommended). |
clientDependencies {
bower {
'jquery'('2.0.0') // <1>
}
npm {
'bootstrap'('3.3.6', exclude: 'jquery') // <2>
'restangular'('1.5.1', transitive: false) // <3>
'angular-animate'('1.5.0', into: 'angular/modules') // <4>
'animate.css'('1.0.0', url:'daneden/animate.css') // <5>
'angular-ui-bootstrap'('1.3.x', from:'dist') { // <6>
include 'ui-bootstrap-tpls.js'
}
}
yarn {
'angular'('1.5.8') // <7>
}
}
-
Installs jquery from bower and into the location (
src/assets/vendor/jquery
) using the default copy settings -
Installs bootstrap from npm and excludes the dependency
jquery
-
Installs restangular from npm but doesn’t include any child dependencies
-
Changing the destination path using the
into
property. This is relative to the install path meaning this would install to the locationsrc/assets/vendor/angular/modules.
-
Use the url property to use a github repo as a dependency source (expands to https://www.github.com/danedan/animate.css.git). A full URL reference any git repository will also work here.
-
The from property allows you to copy code only from a certain subfolder
-
Installs angular from Yarn’s registry
💡
|
If you update your config and it doesn’t seem to be taking effect, try running the clientRefresh task.
|
The files are copied using the DSL of Gradle’s copy task. See: Gradle Copy.
The default settings for the copy task are as follows:
clientDependencies {
fileExtensions = ['css', 'js', 'eot', 'svg', 'ttf', 'woff', 'woff2', 'ts',
'jpg', 'jpeg', 'png', 'gif'] // <1>
releaseFolders = ['dist', 'release'] // <2>
copyIncludes = [] // <3>
copyExcludes = ['**/*.min.js', '**/*.min.css', '**/*.map', '**/Gruntfile.js',
'gulpfile.js', 'source/**'] // <4>
}
-
Default included file extensions
-
Default release folders to look for. If a folder by this name is found then the file extension includes are relative to this folder. (ex
dist/**/*.js
instead of**/*.js
) -
Default includes to append to the end of the copy task
-
Default excludes to append to the end of the copy task
You can add to the default values listed above by using the following methods:
clientDependencies {
fileExtensions 'ts' // <1>
releaseFolders 'lib' // <2>
copyIncludes '**' // <3>
copyExcludes 'index.js' // <4>
}
-
Adds the file extension (
ts
) to default fileExtensions -
Adds a release folders (
lib
) to the default release folder list -
Additional includes to append to the end of the copy task
-
Additional excludes to append to the end of the copy task
You can also completely override the default copy task (this will then completely ignores the settings above)
clientDependencies {
defaultCopy = {
include '**'
exclude '**/*.less', '**/*.sass'
}
}
By passing a closure as the last argument of a dependency declaration you have full control of what files get copied and where they get copied to.
For example:
clientDependencies {
npm {
'bootstrap'('3.3.6') {
include 'dist/**'
exclude '**/*.min.*', '**/*.map', '**/npm.js'
eachFile { it.path -= 'dist/' }
}
}
}
By default two registries named npm and bower are installed. You can either override these or register new custom registries. This allows you to also use it to separate out dependencies (production versus devevelopment dependencies for example).
clientDependencies {
registry 'npmLocal', type:'npm', url:'http://www.example.com/npm/'
registry 'npmDev', type: 'npm', url:'http://www.example.com/npm/'
registry 'bowerLocal', type:'bower', url:'http://www.example.com/bower/'
npmLocal {
'bootstrap'('3.3.6')
'myJSLib'('1.0.0')
}
npmDev {
'lodash'('2.4.1')
'grunt'('1.0.0')
'grunt-contrib-clean'('~0.6.0')
'colors'('^0.6.2')
}
bowerLocal {
'jquery'('2.0.0')
'myBowerJSLib'('1.0.0')
}
}
What follows are additional configuration options. With the possible exception of installDir
you typically won’t
need to set any of these options.
clientDependencies {
installDir = 'src/assets/vendor' // <1>
cacheDir = 'build/client-cache/' // <2>
userAgent = 'client-dependencies-gradle' // <3>
useGlobalCache = true // <4>
checkDownloads = true // <5>
threadPoolSize = 10 // <6>
}
-
Location that dependencies are installed to
-
Location of the local project cache
-
User agent used when making web requests
-
Whether the global caches for bower and npm are searched when resolving dependencies
-
Whether downloads are checked and verified
-
Size of thread pool used when downloading and installing dependencies
Github credentials can be set in the clientDependencies
block:
clientDependencies {
githubUsername = project.hasProperty('githubUsername') ? project.githubUsername : '' // <1>
githubPassword = project.hasProperty('githubPassword') ? project.githubPassword : '' // <2>
}
-
Your Github Username
-
Your Github password or if you use two factor login (and you really should), your personal access token (see: https://github.com/settings/tokens)
If you don’t want to use your username and password you can obtain Github token here https://github.com/settings/tokens/new and use it this way:
clientDependencies {
githubToken = project.hasProperty('githubToken') ? project.githubToken : ''
}
🔥
|
that it is important never to store your Github credentials in your build.gradle file. Instead you
can set the values in ~/.gradle/gradle.properties where they are for your eyes only.
|
Thank you to the following people who have made significant contributions to this project:
-
Janne Ruuttunen - @jruuttun
-
Søren Berg Glasius - @sbglasius
-
Eric Helgeson - @erichelgeson
-
Martin Grześlowski - @magx2