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

Merged
merged 4 commits into from
Aug 11, 2024
Merged
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
96 changes: 96 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,51 @@ 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 either comes provided with
Node, or will currently be downloaded 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-provided-integration/pom.xml
or
https://github.com/eirslett/frontend-maven-plugin/blob/master/frontend-maven-plugin/src/it/corepack-integration/pom.xml
if you need to override the version of corepack in use.


```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>

<!-- Optional - only needed if Node <16.9, or if you need to use a version different
from the one packaged with Node -->
<corepackVersion>v0.25.2</corepackVersion>

<!-- optional: where to download node from. Defaults to https://nodejs.org/dist/ -->
<nodeDownloadRoot>http://myproxy.example.org/nodejs/</nodeDownloadRoot>
<!-- optional: where to download corepack from. Defaults to https://registry.npmjs.org/corepack/-/ -->
<corepackDownloadRoot>http://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 +321,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": "yarn@4.1.1"
}
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://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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,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": "yarn@4.1.1"
}
48 changes: 48 additions & 0 deletions frontend-maven-plugin/src/it/corepack-provided-integration/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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>
</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");
}
}
}
Loading