Skip to content

Commit

Permalink
#134 Explain how to use the plugin when the Gradle centralized reposi…
Browse files Browse the repository at this point in the history
…tories declaration is enabled. Add a test to ensure that the plugin works by default when the centralized repositories declaration is enabled and it does not need to download the Node.js bundle.
  • Loading branch information
bsautel committed Feb 3, 2021
1 parent d1d810a commit db0618a
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 58 deletions.
46 changes: 46 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,49 @@ yarn {
```

Note: the Gradle's up-to-date checking is much slower when using this option. See issue #63.

# Is this plugin compatible with centralized repositories declaration?

Gradle 6.8 improves the dependency resolution management by adding a way to [centralize repositories declaration](https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:centralized-repository-declaration). The `repositoryMode` option controls where the repositories can be declared (in the projects or in the settings).

By default, and when using the `PREFER_PROJECT` mode, the plugin works as expected. When using the `PREFER_SETTINGS` or `FAIL_ON_PROJECT_REPOS` modes, it will not work if it needs to download and install Node.js (i.e. if `download` is `true`). That's because it uses a repository to download the Node.js bundle and this configuration mode prevents it from adding a repository.

To get it work in this mode, you have to declare the repository yourself in the build settings and tell the plugin you declared the repository yourself.

In the `build.gradle` file:

```gradle
node {
download = true
// Do not declare the repository
distBaseUrl = null
}
```

In the `settings.gradle` file:

```gradle
dependencyResolutionManagement {
repositories {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
// Declare the Node.js download repository
ivy {
name = "Node.js"
setUrl("https://nodejs.org/dist/")
patternLayout {
artifact("v[revision]/[artifact](-v[revision]-[classifier]).[ext]")
ivy("v[revision]/ivy.xml")
}
metadataSources {
artifact()
}
content {
includeModule("org.nodejs", "node")
}
}
}
}
```

See issue [#134](https://github.com/node-gradle/gradle-node-plugin/issues/134).
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.gradle.node.task

import com.github.gradle.AbstractIntegTest

import org.gradle.testkit.runner.TaskOutcome
import org.gradle.util.GradleVersion
import org.junit.Assume
Expand Down Expand Up @@ -242,10 +241,10 @@ class NodeTask_integTest extends AbstractIntegTest {
result.output.contains("Cannot resolve external dependency org.nodejs:node:${DEFAULT_NODE_VERSION} because no repositories are defined.")
}

def 'make sure build works with FAIL_ON_PROJECT_REPOS'() {
def 'make sure build works with FAIL_ON_PROJECT_REPOS using a custom repository'() {
given:
Assume.assumeFalse(gradleVersion < GradleVersion.version("6.8"))
copyResources("fixtures/node-depresolutionmgmt")
copyResources("fixtures/node-fail-on-project-repos-download")

when:
def result = build("hello")
Expand All @@ -254,4 +253,17 @@ class NodeTask_integTest extends AbstractIntegTest {
result.task(":nodeSetup").outcome == TaskOutcome.SUCCESS
result.task(":hello").outcome == TaskOutcome.SUCCESS
}

def 'make sure build works with FAIL_ON_PROJECT_REPOS when using the global Node.js (no download)'() {
given:
Assume.assumeFalse(gradleVersion < GradleVersion.version("6.8"))
copyResources("fixtures/node-fail-on-project-repos-no-download")

when:
def result = build("hello")

then:
result.task(":nodeSetup").outcome == TaskOutcome.SKIPPED
result.task(":hello").outcome == TaskOutcome.SUCCESS
}
}
49 changes: 0 additions & 49 deletions src/test/resources/fixtures/node-depresolutionmgmt/build.gradle

This file was deleted.

5 changes: 0 additions & 5 deletions src/test/resources/fixtures/node-depresolutionmgmt/name.js

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
id "com.github.node-gradle.node"
}

node {
version = "12.13.0"
distBaseUrl = null
download = true
workDir = file("build/node")
}

task hello(type: NodeTask) {
script = file("simple.js")
args = []
outputs.upToDateWhen {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
id "com.github.node-gradle.node"
}

task hello(type: NodeTask) {
script = file("simple.js")
args = []
outputs.upToDateWhen {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import org.gradle.api.initialization.resolve.RepositoriesMode
import org.gradle.util.GradleVersion

if (GradleVersion.current() >= GradleVersion.version("6.8")) {
dependencyResolutionManagement {
repositories {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello World");

0 comments on commit db0618a

Please sign in to comment.