PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/sdk/gradle-plugins-node/README.markdown

http://github.com/liferay/liferay-portal
Markdown | 315 lines | 238 code | 77 blank | 0 comment | 0 complexity | 0ce847d2a8c8a7e74b9d15eaf9935d5c MD5 | raw file
Possible License(s): LGPL-2.0
  1. # Node Gradle Plugin
  2. The Node Gradle plugin lets you run [Node.js](https://nodejs.org/) and
  3. [NPM](https://www.npmjs.com/) as part of your build.
  4. The plugin has been successfully tested with Gradle 5.6.4.
  5. ## Usage
  6. To use the plugin, include it in your build script:
  7. ```gradle
  8. buildscript {
  9. dependencies {
  10. classpath group: "com.liferay", name: "com.liferay.gradle.plugins.node", version: "7.0.5"
  11. }
  12. repositories {
  13. maven {
  14. url "https://repository-cdn.liferay.com/nexus/content/groups/public"
  15. }
  16. }
  17. }
  18. apply plugin: "com.liferay.node"
  19. ```
  20. ## Project Extension
  21. The Node Gradle plugin exposes the following properties through the extension
  22. named `node`:
  23. Property Name | Type | Default Value | Description
  24. ------------- | ---- | ------------- | -----------
  25. <a name="download"></a>`download` | `boolean` | `true` | Whether to download and use a local and isolated Node.js distribution instead of the one installed in the system.
  26. `global` | `boolean` | `false` | Whether to use a single Node.js installation for the whole multi-project build. This reduces the time required to unpack the Node.js distribution and the time required to download NPM packages thanks to a shared packages cache. If `download` is `false`, this property has no effect.
  27. <a name="nodedir"></a>`nodeDir` | `File` | <p>**If `global` is `true`:** `"${rootProject.buildDir}/node"`</p><p>**Otherwise:** `"${project.buildDir}/node"`</p> | The directory where the Node.js distribution is unpacked. If `download` is `false`, this property has no effect.
  28. `nodeUrl` | `String` | `"http://nodejs.org/dist/v${node.nodeVersion}/node-v${node.nodeVersion}-${platform}-x${bitMode}.${extension}"` | The URL of the Node.js distribution to download. If `download` is `false`, this property has no effect.
  29. `nodeVersion` | `String` | `"5.5.0"` | The version of the Node.js distribution to use. If `download` is `false`, this property has no effect.
  30. `npmArgs` | `List<String>` | `[]` | The arguments added automatically to every task of type [`ExecutePackageManagerTask`](#executepackagemanagertask).
  31. `npmUrl` | `String` | `"https://registry.npmjs.org/npm/-/npm-${node.npmVersion}.tgz"` | The URL of the NPM version to download. If `download` is `false`, this property has no effect.
  32. `npmVersion` | `String` | `null` | The version of NPM to use. If `null`, the version of NPM embedded inside the Node.js distribution is used. If `download` is `false`, this property has no effect.
  33. It is possible to override the default value of the `download` property by
  34. setting the `nodeDownload` project property. For example, this can be done via
  35. command line argument:
  36. ```bash
  37. ./gradlew -PnodeDownload=false npmInstall
  38. ```
  39. The same extension exposes the following methods:
  40. Method | Description
  41. ------ | -----------
  42. `NodeExtension npmArgs(Iterable<?> npmArgs)` | Adds arguments to automatically add to every task of type [`ExecutePackageManagerTask`](#executepackagemanagertask).
  43. `NodeExtension npmArgs(Object... npmArgs)` | Adds arguments to automatically add to every task of type [`ExecutePackageManagerTask`](#executepackagemanagertask).
  44. The properties of type `File` support any type that can be resolved by
  45. [`project.file`](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:file(java.css.Object)).
  46. Moreover, it is possible to use Closures and Callables as values for `String`,
  47. to defer evaluation until execution.
  48. Please note that setting the `global` property of the `node` extension via the
  49. command line is not supported. It can only be set via Gradle script, which can
  50. be done by adding the following code to the `build.gradle` file in the root of
  51. a project (e.g., Liferay Workspace):
  52. ```gradle
  53. allprojects {
  54. plugins.withId("com.liferay.node") {
  55. node.global = true
  56. }
  57. }
  58. ```
  59. ## Tasks
  60. The plugin adds a series of tasks to your project:
  61. Name | Depends On | Type | Description
  62. ---- | ---------- | ---- | -----------
  63. `cleanNPM` | \- | [`Delete`](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Delete.html) | Deletes the `node_modules` directory, the `npm-shrinkwrap.json` and the `package-lock.json` files from the project, if present.
  64. <a name="downloadnode"></a>`downloadNode` | \- | [`DownloadNodeTask`](#downloadnodetask) | Downloads and unpacks the local Node.js distribution for the project. If `node.download` is `false`, this task is disabled.
  65. `npmInstall` | `downloadNode` | [`NpmInstallTask`](#npminstalltask) | Runs `npm install` to install the dependencies declared in the project's `package.json` file, if present. By default, the task is [configured](#npminstallretries) to run `npm install` two more times if it fails.
  66. `npmPackageLock` | `cleanNPM`, `npmInstall` | [`DefaultTask`](https://docs.gradle.org/current/javadoc/org/gradle/api/DefaultTask.html) | Deletes the NPM files and runs `npm install` to install the dependencies declared in the project's `package.json` file, if present.
  67. `npmShrinkwrap` | `cleanNPM`, `npmInstall` | [`NpmShrinkwrapTask`](#npmshrinkwraptask) | Locks down the versions of a package's dependencies in order to control which dependency versions are used.
  68. [`packageRun${script}`](#packagerunscript-task) | `npmInstall` | [`ExecutePackageManagerTask`](#executepackagemanagertask) | Runs the `${script}` in the project's `package.json` file.
  69. ### DownloadNodeTask
  70. The purpose of this task is to download and unpack a Node.js distribution.
  71. #### Task Properties
  72. Property Name | Type | Default Value | Description
  73. ------------- | ---- | ------------- | -----------
  74. `nodeDir` | `File` | `null` | The directory where the Node.js distribution is unpacked.
  75. `nodeUrl` | `String` | `null` | The URL of the Node.js distribution to download.
  76. `npmUrl` | `String` | `null` | The URL of the NPM version to download.
  77. The properties of type `File` support any type that can be resolved by [`project.file`](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:file(java.css.Object)).
  78. Moreover, it is possible to use Closures and Callables as values for the
  79. `String` properties, to defer evaluation until task execution.
  80. ### ExecuteNodeTask
  81. This is the base task to run Node.js in a Gradle build. All tasks of type
  82. `ExecuteNodeTask` automatically depend on [`downloadNode`](#downloadnode).
  83. #### Task Properties
  84. Property Name | Type | Default Value | Description
  85. ------------- | ---- | ------------- | -----------
  86. `args` | `List<Object>` | `[]` | The arguments for the Node.js invocation.
  87. `command` | `String` | `"node"` | The file name of the executable to invoke.
  88. `environment` | `Map<Object, Object>` | `[]` | The environment variables for the Node.js invocation.
  89. `inheritProxy` | `boolean` | `true` | Whether to set the `http_proxy`, `https_proxy`, and `no_proxy` environment variables in the Node.js invocation based on the values of the system properties `https.proxyHost`, `https.proxyPort`, `https.proxyUser`, `https.proxyPassword`, `https.nonProxyHosts`, `https.proxyHost`, `https.proxyPort`, `https.proxyUser`, `https.proxyPassword`, and `https.nonProxyHosts`. If these environment variables are already set, their values will not be overwritten.
  90. `nodeDir` | `File` | <p>**If [`node.download`](#download) is `true`:** [`node.nodeDir`](#nodedir)</p><p>**Otherwise:** `null`</p> | The directory that contains the executable to invoke. If `null`, the executable must be available in the system `PATH`.
  91. <a name="npminstallretries"></a>`npmInstallRetries` | `int` | `0` | The number of times the `node_modules` is deleted, the NPM cached data is verified (`npm cache verify`), and `npm install` is retried in case the Node.js invocation defined by this task fails. This can help solving corrupted `node_modules` directories by re-downloading the project's dependencies.
  92. `useGradleExec` | `boolean` | <p>**If running in a [Gradle Daemon](https://docs.gradle.org/current/userguide/gradle_daemon.html):** `true`</p><p>**Otherwise:** `false`</p> | Whether to invoke Node.js using [`project.exec`](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:exec(org.gradle.api.Action)), which can solve hanging problems with the Gradle Daemon.
  93. <a name="workingdir"></a>`workingDir` | `File` | `project.projectDir` | The working directory to use in the Node.js invocation.
  94. The properties of type `File` support any type that can be resolved by
  95. [`project.file`](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:file(java.css.Object)).
  96. Moreover, it is possible to use Closures and Callables as values for the
  97. `String` properties to defer evaluation until task execution.
  98. #### Task Methods
  99. Method | Description
  100. ------ | -----------
  101. `ExecuteNodeTask args(Iterable<?> args)` | Adds arguments for the Node.js invocation.
  102. `ExecuteNodeTask args(Object... args)` | Adds arguments for the Node.js invocation.
  103. `ExecuteNodeTask environment(Map<?, ?> environment)` | Adds environment variables for the Node.js invocation.
  104. `ExecuteNodeTask environment(Object key, Object value)` | Adds an environment variable for the Node.js invocation.
  105. ### ExecuteNodeScriptTask
  106. The purpose of this task is to execute a Node.js script. Tasks of type
  107. `ExecuteNodeScriptTask` extend [`ExecuteNodeTask`](#executenodetask).
  108. #### Task Properties
  109. Property Name | Type | Default Value | Description
  110. ------------- | ---- | ------------- | -----------
  111. `scriptFile` | `File` | `null` | The Node.js script to execute.
  112. The properties of type `File` support any type that can be resolved by
  113. [`project.file`](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:file(java.css.Object)).
  114. ### ExecutePackageManagerTask
  115. The purpose of this task is to execute an NPM command. Tasks of type
  116. `ExecutePackageManagerTask` extend [`ExecuteNodeScriptTask`](#executenodescripttask) with
  117. the following properties set by default:
  118. Property Name | Default Value
  119. ------------- | -------------
  120. `command` | <p>**If `nodeDir` is `null`:** `"npm"`</p><p>**Otherwise:** `"node"`</p>
  121. `scriptFile` | <p>**If `nodeDir` is `null`:** `null`</p><p>**Otherwise:** `"${nodeDir}/lib/node_modules/npm/bin/npm-cli.js"` or `"${nodeDir}/node_modules/npm/bin/npm-cli.js"` on Windows.</p>
  122. #### Task Properties
  123. Property Name | Type | Default Value | Description
  124. ------------- | ---- | ------------- | -----------
  125. `cacheConcurrent` | `boolean` | <p>**If `node.npmVersion` is greater than or equal to `5.0.0`, or `node.nodeVersion` is greater than or equal to `8.0.0`:** `true`</p><p>**Otherwise:** `false`</p> | Whether to run this task concurrently, in case the version of NPM in use supports multiple concurrent accesses to the same cache directory.
  126. `cacheDir` | `File` | <p>**If `nodeDir` is `null`, or `node.npmVersion` is greater than or equal to `5.0.0`, or `node.nodeVersion` is greater than or equal to `8.0.0`:** `null`</p><p>**Otherwise:** `"${nodeDir}/.cache"`</p> | The location of NPM's cache directory. It sets the [`--cache`](https://docs.npmjs.com/misc/config#cache) argument. Leave the property `null` to keep the default value.
  127. `logLevel` | `String` | Value to mirror the log level set in the task's [`logger`](https://docs.gradle.org/current/dsl/org.gradle.api.Task.html#org.gradle.api.Task:logger) object. | The NPM log level. It sets the [--loglevel](https://docs.npmjs.com/misc/config#loglevel) argument.
  128. `production` | `boolean` | `false` | Whether to run in production mode during the NPM invocation. It sets the [`--production`](https://docs.npmjs.com/misc/config#production) argument.
  129. `progress` | `boolean` | `true` | Whether to show a progress bar during the NPM invocation. It sets the [`--progress`](https://docs.npmjs.com/misc/config#progress) argument.
  130. <a name="registry"></a>`registry` | `String` | `null` | The base URL of the NPM package registry. It sets the [`--registry`](https://docs.npmjs.com/misc/config#registry) argument. Leave the property `null` or empty to keep the default value.
  131. The properties of type `File` support any type that can be resolved by [`project.file`](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:file(java.css.Object)).
  132. Moreover, it is possible to use Closures and Callables as values for the
  133. `String` properties, to defer evaluation until task execution.
  134. ### DownloadNodeModuleTask
  135. The purpose of this task is to download a Node.js package. The packages are
  136. downloaded in the `${workingDir}/node_modules` directory, which is equal, by
  137. default, to the `node_modules` directory of the project. Tasks of type
  138. `DownloadNodeModuleTask` extend [`ExecutePackageManagerTask`](#executepackagemanagertask) in order to
  139. execute the command [`npm install ${moduleName}@${moduleVersion}`](https://docs.npmjs.com/cli/install).
  140. `DownloadNodeModuleTask` instances are automatically disabled if the project's
  141. `package.json` file already lists a module with the same name in its
  142. `dependencies` or `devDependencies` object.
  143. #### Task Properties
  144. Property Name | Type | Default Value | Description
  145. ------------- | ---- | ------------- | -----------
  146. `moduleName` | `String` | `null` | The name of the Node.js package to download.
  147. `moduleVersion` | `String` | `null` | The version of the Node.js package to download.
  148. It is possible to use Closures and Callables as values for the `String`
  149. properties, to defer evaluation until task execution.
  150. ### NpmInstallTask
  151. Purpose of these tasks is to install the dependencies declared in a
  152. `package.json` file. Tasks of type `NpmInstallTask` extend
  153. [`ExecutePackageManagerTask`](#executepackagemanagertask) in order to run the command [`npm install`](https://docs.npmjs.com/cli/install).
  154. `NpmInstallTask` instances are automatically disabled if the `package.json` file
  155. does not declare any dependency in the `dependency` or `devDependencies` object.
  156. #### Task Properties
  157. Property Name | Type | Default Value | Description
  158. ------------- | ---- | ------------- | -----------
  159. `nodeModulesCacheDir` | `File` | `null` | <p>The directory where `node_modules` directories are cached. By setting this property, it is possible to cache the `node_modules` directory of a project and avoid unnecessary invocations of `npm install`, useful especially in Continuous Integration environments.</p><p>The `node_modules` directory is cached based on the content of the project's `package-lock.json` (or `npm-shrinkwrap.json`, or `package.json` if absent). Therefore, if `NpmInstallTask` tasks in multiple projects are configured with the same `nodeModulesCacheDir`, and their `package-lock.json`, `npm-shrinkwrap.json` or `package.json` declare the same dependencies, their `node_modules` caches will be shared.</p><p>This feature is not available if the [`com.liferay.cache`](https://github.com/liferay/liferay-portal/tree/master/modules/sdk/gradle-plugins-cache) plugin is applied.</p>
  160. `nodeModulesCacheNativeSync` | `boolean` | `true` | Whether to use `rsync` (on Linux/macOS) or `robocopy` (on Windows) to cache and restore the `node_modules` directory. If `nodeModulesCacheDir` is not set, this property has no effect.
  161. `nodeModulesDigestFile` | `File` | `null` | <p>If this property is set, the content of the project's `package-lock.json` (or `npm-shrinkwrap.json`, or `package.json` if absent) is checked with the digest from the `node_modules` directory. If the digests match, do nothing. If the digests don't match, the `node_modules` directory is deleted before running `npm install`.</p><p>This feature is not available if the `com.liferay.cache` plugin is applied or if the property `nodeModulesCacheDir` is set.</p>
  162. `removeShrinkwrappedUrls` | `boolean` | `true` if the [registry](#registry) property has a value, `false` otherwise. | Whether to temporarily remove all the hard-coded URLs in the `from` and `resolved` fields of the `npm-shinkwrap.json` file before invoking `npm install`. This way, it is possible to force NPM to download all dependencies from a custom registry declared in the [`registry`](#registry) property.
  163. `useNpmCI` | `boolean` | `false` | Whether to run `npm ci` instead of `npm install`. If the `package-lock.json` file does not exist, this property has no effect.
  164. The properties of type `File` support any type that can be resolved by [`project.file`](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:file(java.css.Object)).
  165. ### NpmShrinkwrapTask
  166. The purpose of this task is to lock down the versions of a package's
  167. dependencies so that you can control exactly which dependency versions are used
  168. when your package is installed. Tasks of type `NpmShrinkwrapTask` extend
  169. [`ExecutePackageManagerTask`](#executepackagemanagertask) to execute the command
  170. [`npm shrinkwrap`](https://docs.npmjs.com/cli/shrinkwrap).
  171. The generated `npm-shrinkwrap.json` file is automatically sorted and formatted,
  172. so it's easier to see the changes with the previous version.
  173. `NpmShrinkwrapTask` instances are automatically disabled if the `package.json`
  174. file does not exist.
  175. #### Task Properties
  176. Property Name | Type | Default Value | Description
  177. ------------- | ---- | ------------- | -----------
  178. `excludedDependencies` | `List<String>` | `[]` | The package names to exclude from the generated `npm-shrinkwrap.json` file.
  179. `includeDevDependencies` | `boolean` | `true` | Whether to include the package's `devDependencies`. It sets the [`--dev`](https://docs.npmjs.com/cli/shrinkwrap#other-notes) argument.
  180. It is possible to use Closures and Callables as values for the `String`
  181. properties to defer evaluation until task execution.
  182. #### Task Methods
  183. Method | Description
  184. ------ | -----------
  185. `NpmShrinkwrapTask excludeDependencies(Iterable<?> excludedDependencies)` | Adds package names to exclude from the generated `npm-shrinkwrap.json` file.
  186. `NpmShrinkwrapTask excludeDependencies(Object... excludedDependencies)` | Adds package names to exclude from the generated `npm-shrinkwrap.json` file.
  187. ### PublishNodeModuleTask
  188. The purpose of this task is to publish a package to the
  189. [NPM registry](https://www.npmjs.com/). Tasks of type `PublishNodeModuleTask`
  190. extend [`ExecutePackageManagerTask`](#executepackagemanagertask) in order to execute the command
  191. [`npm publish`](https://docs.npmjs.com/cli/publish).
  192. These tasks generate a new temporary `package.json` file in the directory
  193. assigned to the [`workingDir`](#workingdir) property; then the `npm publish`
  194. command is executed. If the `package.json` file in that location does not exist,
  195. the one in the root of the project directory (if found) is copied; otherwise, a
  196. new file is created.
  197. The `package.json` is then processed by adding the values provided by the task
  198. properties, if not already present in the file itself. It is still possible to
  199. override one or more fields of the `package.json` file with the values provided
  200. by the task properties by adding one or more keys (e.g., `"version"`) to the
  201. `overriddenPackageJsonKeys` property.
  202. #### Task Properties
  203. Property Name | Type | Default Value | Description
  204. ------------- | ---- | ------------- | -----------
  205. `moduleAuthor` | `String` | `null` | The value of the [`author`](https://docs.npmjs.com/files/package.json#people-fields-author-contributors) field in the generated `package.json` file.
  206. `moduleBugsUrl` | `String` | `null` | The value of the [`bugs.url`](https://docs.npmjs.com/files/package.json#bugs) field in the generated `package.json` file.
  207. `moduleDescription` | `String` | `project.description` | The value of the [`description`](https://docs.npmjs.com/files/package.json#description-1) field in the generated `package.json` file.
  208. `moduleKeywords` | `List<String>` | `[]` | The value of the [`keywords`](https://docs.npmjs.com/files/package.json#keywords) field in the generated `package.json` file.
  209. `moduleLicense` | `String` | `null` | The value of the [`license`](https://docs.npmjs.com/files/package.json#license) field in the generated `package.json` file.
  210. `moduleMain` | `String` | `null` | The value of the [`main`](https://docs.npmjs.com/files/package.json#main) field in the generated `package.json` file.
  211. `moduleName` | `String` | Name based on [`osgiHelper.bundleSymbolicName`](https://github.com/gradle/gradle/blob/master/subprojects/osgi/src/main/java/org/gradle/api/internal/plugins/osgi/OsgiHelper.java): for example, if `osgiHelper.bundleSymbolicName` is `"com.liferay.gradle.plugins.node"`, the default value for the `moduleName` property is `"liferay-gradle-plugins-node"`. | The value of the [`name`](https://docs.npmjs.com/files/package.json#name) field in the generated `package.json` file.
  212. `moduleRepository` | `String` | `null` | The value of the [`repository`](https://docs.npmjs.com/files/package.json#repository) field in the generated `package.json` file.
  213. `moduleVersion` | `String` | `project.version` | The value of the [`version`](https://docs.npmjs.com/files/package.json#version) field in the generated `package.json` file.
  214. `npmEmailAddress` | `String` | `null` | The email address of the npmjs.com user that publishes the package.
  215. `npmPassword` | `String` | `null` | The password of the npmjs.com user that publishes the package.
  216. `npmUserName` | `String` | `null` | The name of the npmjs.com user that publishes the package.
  217. `overriddenPackageJsonKeys` | `Set<String>` | `[]` | The field values to override in the generated `package.json` file.
  218. #### Task Methods
  219. Method | Description
  220. ------ | -----------
  221. `PublishNodeModuleTask overriddenPackageJsonKeys(Iterable<String> overriddenPackageJsonKeys)` | Adds field values to override in the generated `package.json` file.
  222. `PublishNodeModuleTask overriddenPackageJsonKeys(String... overriddenPackageJsonKeys)` | Adds field values to override in the generated `package.json` file.
  223. ### packageRun${script} Task
  224. For each [script](https://docs.npmjs.com/misc/scripts) declared in the
  225. `package.json` file of the project, one task `packageRun${script}` of type
  226. [`ExecutePackageManagerTask`](#executepackagemanagertask) is added. Each of these tasks is
  227. automatically configured with sensible defaults:
  228. Property Name | Default Value
  229. ------------- | -------------
  230. `args` | `["run-script", "${script}"]`
  231. If the [`java`](https://docs.gradle.org/current/userguide/java_plugin.html)
  232. plugin is applied and the `package.json` file declares a script named `"build"`,
  233. the script is executed before the `classes` task but after the
  234. [`processResources`](https://docs.gradle.org/4.0/userguide/java_plugin.html#sec:java_resources)
  235. task.
  236. If the [`lifecycle-base`](https://docs.gradle.org/current/javadoc/org/gradle/language/base/plugins/LifecycleBasePlugin.html)
  237. plugin is applied and the `package.json` file declares a script named `test`,
  238. the script is executed when running the `check` task.