Here you can see a very simple way how to use nodejs and gradle together without any plugins.
- Add a repository
repositories {
// For example: https://nodejs.org/dist/v10.16.3/node-v10.16.3-linux-x64.tar.xz
ivy {
url 'https://nodejs.org/dist/'
patternLayout {
//This maps to the pattern: [organisation]:[module]:[revision]:[classifier]@[ext]
artifact '/v[revision]/[organisation]-v[revision]-[module].[ext]'
}
metadataSources {
artifact()
ignoreGradleMetadataRedirection()
}
}
}
- Add a new configuration and a dependency
configurations {
nodejs
}
dependencies {
// to be able to download nodejs as a dependency
nodejs group: 'node', name: project.'nodejs.platform', version: project.'nodejs.version', ext: 'tar.xz'
}
Note: the version of NodeJs you can specify in the gradle.properties
- A task for installing NodeJs
def nodeJsHome = file("$projectDir/node-v${project.'nodejs.version'}-${project.'nodejs.platform'}")
task installNodeJs() {
group = 'NodeJs'
description = 'Install NodeJs'
onlyIf {
!nodeJsHome.exists()
}
doLast {
println "NodeJs: ${project.'nodejs.version'} will be installed..."
def dist = project.configurations.nodejs.files[0]
exec {
commandLine 'tar', '-xf', dist.path, '-C', projectDir
}
}
}
The code below shows how you can add new tasks into subprojects:
subprojects {
// going through {node|npm|npx}
fileTree(file("$nodeJsHome/bin")).files.each { f ->
// creates a new task under sub-projects
task "${f.name}"(dependsOn: parent.tasks['installNodeJs']) {
group 'NodeJs'
description "To be able to use: ${f.path}"
doLast {
def options = new Scanner(System.in).nextLine().split(' ')
println ">> Executing: ${f.path}"
println ">> Options $options"
println ">> Working Dir: $projectDir"
exec {
workingDir projectDir
executable f.path
args options
errorOutput = System.out
ignoreExitValue = true
}
}
}
}
}
The main trick is to pass the arguments through standard input.
def options = new Scanner(System.in).nextLine().split(' ')
exec {
executable ...
args options
...
}
After that you will have the following tasks:
- installNodeJs - Install NodeJs
- node - To be able to use: /gradle-nodejs/node-v10.16.3-linux-x64/bin/node
- npm - To be able to use: /gradle-nodejs/node-v10.16.3-linux-x64/bin/npm
- npx - To be able to use: /gradle-nodejs/node-v10.16.3-linux-x64/bin/npx
The following examples will show how you will be able to use that:
- node --help
./gradlew nodejs-projectA:node <<<--help
- npm -v
./gradlew nodejs-projectB:npm <<<-v
- npm install --save-dev webpack
./gradlew nodejs-projectA:npm <<<'install --save-dev webpack'
- npx webpack --colors
./gradlew nodejs-projectB:npx <<<'webpack --colors'