From 2a17a176700d1f92a572977fce7726de95929459 Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Mon, 29 Apr 2024 13:29:07 -0600 Subject: [PATCH] resolves #40 add nodeDownloadUrl and nodeDownloadServerId parameters to control the download URL and server settings for retrieving Node.js distributions --- CHANGELOG.adoc | 4 ++ docs/modules/ROOT/pages/antora-goal.adoc | 44 +++++++++++++++++++ docs/modules/ROOT/pages/usage.adoc | 17 ++++--- .../org/antora/maven/AntoraMavenPlugin.java | 18 +++++++- .../org/antora/maven/NodeVersionResolver.java | 8 ++-- 5 files changed, 76 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index ec05cfb..54de552 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -7,6 +7,10 @@ This project utilizes semantic versioning. == Unreleased +=== Added + +* Add `nodeDownloadUrl` and `nodeDownloadServerId` parameters to control the download URL and server settings, respectively, for retrieving Node.js distributions (#40) + === Changed * Rename the `playbook` parameter and property to `playbookFile` and `antora.playbookFile`, respectively; map `playbook` as alias (#44) diff --git a/docs/modules/ROOT/pages/antora-goal.adoc b/docs/modules/ROOT/pages/antora-goal.adoc index e0d3662..395e536 100644 --- a/docs/modules/ROOT/pages/antora-goal.adoc +++ b/docs/modules/ROOT/pages/antora-goal.adoc @@ -75,6 +75,50 @@ Type:: Map Required:: false Alias:: env +[#nodeDownloadUrl] +=== + +The root URL from where the Node.js distributions are retrieved. +The trailing `/` is permitted, but not required. + +.pom.xml +[,xml] +---- + + https://download.example.org/nodejs + +---- + +.CLI + $ mvn antora:antora -Dnode.downloadUrl=https://download.example.org/nodejs + +[.properties] +Type:: String +Required:: true +Default value:: https://nodejs.org/dist +User property:: node.downloadUrl + +[#nodeDownloadServerId] +=== + +The ID of a Maven server declaration that supplies credentials for the URL from which Node.js distributions are downloaded (see the <> parameter). + +.pom.xml +[,xml] +---- + + download-server + +---- + +.CLI + $ mvn antora:antora -Dnode.downloadServerId=download-server + +[.properties] +Type:: String +Required:: false +User property:: node.downloadServerId + [#nodeExecutable] === diff --git a/docs/modules/ROOT/pages/usage.adoc b/docs/modules/ROOT/pages/usage.adoc index 7c44ca7..f0f4bee 100644 --- a/docs/modules/ROOT/pages/usage.adoc +++ b/docs/modules/ROOT/pages/usage.adoc @@ -221,28 +221,27 @@ Use with care. This plugin automatically downloads and installs Node.js and npm on demand (unless the `nodeExecutable` property is specified). By default, the plugin will download Node.js from the official location at https://nodejs.org. -If your environment mandates downloading Node.js from an alternate URL, you can override the default URL using the `nodeDownloadRoot` property. -You can define this property in the POM file as a custom property: +If your environment mandates downloading Node.js from an alternate URL, you can override the default URL using the `nodeDownloadUrl` parameter or `node.downloadUrl` property. +The download server is expected to mirror the storage structure used by the official download source (e.g., /20.12.2/node-20.12.2-linux-x64.tar.gz). + +You can define the `node.downloadUrl` property in the POM file as a custom property: .pom.xml [,xml] ---- - https://download.example.org/nodejs/dist/ + https://download.example.org/nodejs ---- or from the CLI using a user property: - $ mvn antora:antora -DnodeDownloadRoot=https://download.example.org/nodejs/ + $ mvn antora:antora -Dnode.downloadUrl=https://download.example.org/nodejs -If credentials are required, you can pass a Maven server configuration (defined in settings.xml) using the `serverId` property. - -The download server is expected to mirror the storage structure used by the official download source (e.g., /20.12.2/node-20.12.2-linux-x64.tar.gz). -The value of the `nodeDownloadRoot` property must end with `/`. +If credentials are required, you can pass a Maven server configuration (defined in settings.xml) using the `nodeDownloadServerId` parameter or `node.downloadServerId` property. The downloaded file is cached into the local Maven repository ($M2_REPO/com/github/eirslett/node/) so it doesn't have to be retrieved again. -Thus, an alternative to using the `nodeDownloadRoot` property is to use a separate script that seeds the local Maven repository with the downloaded file. +Thus, an alternative to using the `node.downloadUrl` property is to use a separate script that seeds the local Maven repository with the downloaded file. If the plugin finds the file cached in the Maven repository, it will not attempt to reach out to the download server. == Escape user properties diff --git a/src/main/java/org/antora/maven/AntoraMavenPlugin.java b/src/main/java/org/antora/maven/AntoraMavenPlugin.java index 2dcd679..f724043 100644 --- a/src/main/java/org/antora/maven/AntoraMavenPlugin.java +++ b/src/main/java/org/antora/maven/AntoraMavenPlugin.java @@ -15,6 +15,7 @@ import org.codehaus.plexus.util.xml.Xpp3Dom; import java.io.File; import java.io.IOException; +import java.net.URL; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; @@ -77,6 +78,12 @@ public class AntoraMavenPlugin extends AbstractMojo { @Parameter(alias = "env") private Map environmentVariables = Map.of(); + @Parameter(property = "node.downloadServerId") + private String nodeDownloadServerId; + + @Parameter(property = "node.downloadUrl", defaultValue = "https://nodejs.org/dist") + private URL nodeDownloadUrl; + /** * Sets the name or path of the Node.js executable to use. Implicitly selects which Node.js installation to use to * invoke npm and npx. When specified, Node.js is not installed locally and the nodeVersion parameter is ignored. @@ -176,10 +183,13 @@ public class AntoraMavenPlugin extends AbstractMojo { SystemNodeLinker systemNodeLinker = new SystemNodeLinker(getLog(), nodeHomeDir); if (this.nodeExecutable == null) { systemNodeLinker.unlinkNode(); - String resolvedNodeVersion = new NodeVersionResolver(getLog()).resolveVersion(this.nodeVersion); + String resolvedNodeVersion = + new NodeVersionResolver(getLog(), nodeDownloadUrl).resolveVersion(this.nodeVersion); frontendMojoExecutor.handleNodeVersionChange(nodeHomeDir, resolvedNodeVersion); npmCacheDir = new File(nodeHomeDir, "_npm"); - frontendMojoExecutor.executeMojo("install-node-and-npm", element("nodeVersion", resolvedNodeVersion)); + frontendMojoExecutor.executeMojo("install-node-and-npm", + element("nodeDownloadRoot", chomp(nodeDownloadUrl.toString(), "/") + "/"), + element("serverId", nodeDownloadServerId), element("nodeVersion", resolvedNodeVersion)); } else { systemNodeLinker.linkNode(Path.of(this.nodeExecutable).toString()); } @@ -403,6 +413,10 @@ public class AntoraMavenPlugin extends AbstractMojo { return result.isEmpty() ? null : result; } + private String chomp(String value, String tail) { + return value.endsWith(tail) ? value.substring(0, value.length() - tail.length()) : value; + } + private List createSingleValueList(String value) { List list = new ArrayList<>(1); list.add(value); diff --git a/src/main/java/org/antora/maven/NodeVersionResolver.java b/src/main/java/org/antora/maven/NodeVersionResolver.java index 88c050d..f3fed09 100644 --- a/src/main/java/org/antora/maven/NodeVersionResolver.java +++ b/src/main/java/org/antora/maven/NodeVersionResolver.java @@ -19,10 +19,10 @@ import java.util.stream.Stream; import java.util.stream.StreamSupport; public class NodeVersionResolver { - private static String DIST_BASE_URL = "https://nodejs.org/dist"; - private static String FALLBACK_VERSION = "v16.20.2"; + private static String RELEASES_DATA_URL = "https://nodejs.org/dist/index.json"; + private Log log; private FileDownloader fileDownloader; @@ -31,11 +31,11 @@ public class NodeVersionResolver { private URL releasesDataSource; - public NodeVersionResolver(Log log) { + public NodeVersionResolver(Log log, URL nodeDownloadUrl) { this.log = log; this.fileDownloader = new FileDownloader(); this.releasesDataCache = resolveReleasesCache(); - this.releasesDataSource = toURL(DIST_BASE_URL + "/index.json"); + this.releasesDataSource = toURL(RELEASES_DATA_URL); } public String resolveVersion(String version) { -- GitLab