Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support corepack #1157

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ to see how it should be set up: https://github.com/eirslett/frontend-maven-plugi

- [Installing node and npm](#installing-node-and-npm)
- [Installing node and yarn](#installing-node-and-yarn)
- [Installing node and corepack](#installing-node-and-corepack)
- Running
- [npm](#running-npm)
- [yarn](#running-yarn)
- [corepack](#running-corepack)
- [bower](#running-bower)
- [grunt](#running-grunt)
- [gulp](#running-gulp)
Expand Down Expand Up @@ -171,6 +173,45 @@ https://github.com/eirslett/frontend-maven-plugin/blob/master/frontend-maven-plu
</plugin>
```

### Installing node and corepack

You can choose to let corepack manage the package manager version in use. Node is
downloaded from `https://nodejs.org/dist`, and corepack currently comes from
`https://repository.npmjs.org`, extracted and put into a `node` folder created
in your installation directory.

Node/corepack and any package managers will only be "installed" locally to your project.
It will not be installed globally on the whole system (and it will not interfere with any
Node/corepack installations already present).

Have a look at the example `POM` to see how it should be set up with corepack:
https://github.com/eirslett/frontend-maven-plugin/blob/master/frontend-maven-plugin/src/it/corepack-integration/pom.xml


```xml
<plugin>
...
<execution>
<!-- optional: you don't really need execution ids, but it looks nice in your build log. -->
<id>install-node-and-corepack</id>
<goals>
<goal>install-node-and-corepack</goal>
</goals>
<!-- optional: default phase is "generate-resources" -->
<phase>generate-resources</phase>
</execution>
<configuration>
<nodeVersion>v20.12.2</nodeVersion>
<corepackVersion>v0.25.2</corepackVersion>

<!-- optional: where to download node from. Defaults to https://nodejs.org/dist/ -->
<nodeDownloadRoot>http:https://myproxy.example.org/nodejs/</nodeDownloadRoot>
<!-- optional: where to download corepack from. Defaults to https://registry.npmjs.org/corepack/-/ -->
<corepackDownloadRoot>http:https://myproxy.example.org/corepack/</corepackDownloadRoot>
</configuration>
</plugin>
```

### Running npm

All node packaged modules will be installed in the `node_modules` folder in your [working directory](#working-directory).
Expand Down Expand Up @@ -274,6 +315,55 @@ Also you can set a registry using a tag `npmRegistryURL`
</execution>
```

### Running corepack

If your `packageManager` specifies `yarn`, then you'll want to have something like:


```xml
<execution>
<id>install</id>
<goals>
<goal>corepack</goal>
</goals>
<configuration>
<arguments>yarn install</arguments>
</configuration>
</execution>
<execution>
<id>build</id>
<goals>
<goal>corepack</goal>
</goals>
<configuration>
<arguments>yarn build</arguments>
</configuration>
</execution>
```

and if you're using `pnpm` instead, you'll want something like

```xml
<execution>
<id>install</id>
<goals>
<goal>corepack</goal>
</goals>
<configuration>
<arguments>pnpm install</arguments>
</configuration>
</execution>
<execution>
<id>build</id>
<goals>
<goal>corepack</goal>
</goals>
<configuration>
<arguments>pnpm build</arguments>
</configuration>
</execution>
```

### Running bower

All bower dependencies will be installed in the `bower_components` folder in your working directory.
Expand Down
3 changes: 3 additions & 0 deletions frontend-maven-plugin/src/it/corepack-integration/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enableGlobalCache: false

nodeLinker: node-modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "example",
"version": "0.0.1",
"dependencies": {
"less": "~3.0.2"
},
"packageManager": "[email protected]"
}
49 changes: 49 additions & 0 deletions frontend-maven-plugin/src/it/corepack-integration/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http:https://maven.apache.org/POM/4.0.0" xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.eirslett</groupId>
<artifactId>example</artifactId>
<version>0</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<!-- NB! Set <version> to the latest released version of frontend-maven-plugin, like in README.md -->
<version>@project.version@</version>

<configuration>
<installDirectory>target</installDirectory>
</configuration>

<executions>

<execution>
<id>install node and corepack</id>
<goals>
<goal>install-node-and-corepack</goal>
</goals>
<configuration>
<nodeVersion>v20.12.2</nodeVersion>
<corepackVersion>0.25.2</corepackVersion>
</configuration>
</execution>

<execution>
<id>yarn install</id>
<goals>
<goal>corepack</goal>
</goals>
<configuration>
<arguments>yarn install --no-immutable</arguments>
</configuration>
</execution>

</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
assert new File(basedir, 'target/node').exists() : "Node was not installed in the custom install directory";
assert new File(basedir, 'node_modules').exists() : "Node modules were not installed in the base directory";
assert new File(basedir, 'node_modules/less/package.json').exists() : "Less dependency has not been installed successfully";

String buildLog = new File(basedir, 'build.log').text
assert buildLog.contains('BUILD SUCCESS') : 'build was not successful'
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.eirslett.maven.plugins.frontend.mojo;

import java.io.File;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.sonatype.plexus.build.incremental.BuildContext;

import com.github.eirslett.maven.plugins.frontend.lib.FrontendPluginFactory;
import com.github.eirslett.maven.plugins.frontend.lib.TaskRunnerException;

@Mojo(name="corepack", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public final class CorepackMojo extends AbstractFrontendMojo {

/**
* corepack arguments. Default is "enable".
*/
@Parameter(defaultValue = "enable", property = "frontend.corepack.arguments", required = false)
private String arguments;

@Parameter(property = "session", defaultValue = "${session}", readonly = true)
private MavenSession session;

@Component
private BuildContext buildContext;

@Component(role = SettingsDecrypter.class)
private SettingsDecrypter decrypter;

/**
* Skips execution of this mojo.
*/
@Parameter(property = "skip.corepack", defaultValue = "${skip.corepack}")
private boolean skip;

@Override
protected boolean skipExecution() {
return this.skip;
}

@Override
public synchronized void execute(FrontendPluginFactory factory) throws TaskRunnerException {
File packageJson = new File(workingDirectory, "package.json");
if (buildContext == null || buildContext.hasDelta(packageJson) || !buildContext.isIncremental()) {
factory.getCorepackRunner().execute(arguments, environmentVariables);
} else {
getLog().info("Skipping corepack install as package.json unchanged");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.github.eirslett.maven.plugins.frontend.mojo;

import com.github.eirslett.maven.plugins.frontend.lib.CorepackInstaller;
import com.github.eirslett.maven.plugins.frontend.lib.FrontendPluginFactory;
import com.github.eirslett.maven.plugins.frontend.lib.InstallationException;
import com.github.eirslett.maven.plugins.frontend.lib.ProxyConfig;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.apache.maven.settings.Server;

@Mojo(name="install-node-and-corepack", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public final class InstallNodeAndCorepackMojo extends AbstractFrontendMojo {

/**
* Where to download Node.js binary from. Defaults to https://nodejs.org/dist/
*/
@Parameter(property = "nodeDownloadRoot", required = false)
private String nodeDownloadRoot;

/**
* Where to download corepack binary from. Defaults to https://registry.npmjs.org/corepack/-/
*/
@Parameter(property = "corepackDownloadRoot", required = false, defaultValue = CorepackInstaller.DEFAULT_COREPACK_DOWNLOAD_ROOT)
private String corepackDownloadRoot;

/**
* The version of Node.js to install. IMPORTANT! Most Node.js version names start with 'v', for example 'v0.10.18'
*/
@Parameter(property="nodeVersion", required = true)
private String nodeVersion;

/**
* The version of corepack to install. Note that the version string can optionally be prefixed with
* 'v' (i.e., both 'v1.2.3' and '1.2.3' are valid).
*/
@Parameter(property = "corepackVersion", required = true)
private String corepackVersion;

/**
* Server Id for download username and password
*/
@Parameter(property = "serverId", defaultValue = "")
private String serverId;

@Parameter(property = "session", defaultValue = "${session}", readonly = true)
private MavenSession session;

/**
* Skips execution of this mojo.
*/
@Parameter(property = "skip.installnodecorepack", defaultValue = "${skip.installnodecorepack}")
private boolean skip;

@Component(role = SettingsDecrypter.class)
private SettingsDecrypter decrypter;

@Override
protected boolean skipExecution() {
return this.skip;
}

@Override
public void execute(FrontendPluginFactory factory) throws InstallationException {
ProxyConfig proxyConfig = MojoUtils.getProxyConfig(session, decrypter);
String resolvedNodeDownloadRoot = getNodeDownloadRoot();
String resolvedCorepackDownloadRoot = getCorepackDownloadRoot();
Server server = MojoUtils.decryptServer(serverId, session, decrypter);
if (null != server) {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeDownloadRoot(resolvedNodeDownloadRoot)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
factory.getCorepackInstaller(proxyConfig)
.setCorepackVersion(corepackVersion)
.setCorepackDownloadRoot(resolvedCorepackDownloadRoot)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
} else {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeDownloadRoot(resolvedNodeDownloadRoot)
.install();
factory.getCorepackInstaller(proxyConfig)
.setCorepackVersion(this.corepackVersion)
.setCorepackDownloadRoot(resolvedCorepackDownloadRoot)
.install();
}
}

private String getNodeDownloadRoot() {
return nodeDownloadRoot;
}

private String getCorepackDownloadRoot() {
return corepackDownloadRoot;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<pluginExecution>
<pluginExecutionFilter>
<goals>
<goal>install-node-and-corepack</goal>
<goal>install-node-and-npm</goal>
<goal>install-node-and-pnpm</goal>
<goal>install-node-and-yarn</goal>
Expand All @@ -20,6 +21,7 @@
<pluginExecution>
<pluginExecutionFilter>
<goals>
<goal>corepack</goal>
<goal>npm</goal>
<goal>npx</goal>
<goal>pnpm</goal>
Expand Down
Loading