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

Added Yarn Berry compatibility flag #1012

Merged
merged 4 commits into from
Dec 10, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Last public release: [![Maven Central](https://maven-badges.herokuapp.com/maven-

* update Dependency: Jackson (2.13.0), Mockito (4.1.0), JUnit (5.8.1), Hamcrest (2.2; now a direct dependency)
* remove Dependency: Powermock
* Added better support for Yarn 2.x and above (Berry)

### 1.11.4
* Support node arm64 binaries since v16 major release
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ Node/Yarn 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/Yarn installations already
present).

If your project is using Yarn Berry (2.x or above), the Yarn version is handled per project but a Yarn 1.x install is still needed as a "bootstrap".
The plugin will try to detect `.yarnrc.yml` file in the current Maven project/module folder, at the root of the multi-module project if relevant, and in the folder from which the `mvn` command was run.
If detected, the plugin will assume your project is using Yarn Berry. It will install the 1.x Yarn version you specify with `yarnVersion` as bootstrap, then hand over to your project-specific version.

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.eirslett.maven.plugins.frontend.mojo;

import java.io.File;
import java.util.stream.Stream;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -16,6 +18,8 @@
@Mojo(name = "install-node-and-yarn", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public final class InstallNodeAndYarnMojo extends AbstractFrontendMojo {

private static final String YARNRC_YAML_FILE_NAME = ".yarnrc.yml";
neo1203 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Where to download Node.js binary from. Defaults to https://nodejs.org/dist/
*/
Expand Down Expand Up @@ -65,6 +69,22 @@ protected boolean skipExecution() {
return this.skip;
}

/**
* Checks whether a .yarnrc.yml file exists at the project root (in multi-module builds, it will be the Reactor project)
*
* @return true if the .yarnrc.yml file exists, false otherwise
*/
private boolean isYarnrcYamlFilePresent() {
Stream<File> filesToCheck = Stream.of(
new File(session.getCurrentProject().getBasedir(), YARNRC_YAML_FILE_NAME),
new File(session.getRequest().getMultiModuleProjectDirectory(), YARNRC_YAML_FILE_NAME),
new File(session.getExecutionRootDirectory(), YARNRC_YAML_FILE_NAME)
);

return filesToCheck
.anyMatch(File::exists);
}

@Override
public void execute(FrontendPluginFactory factory) throws InstallationException {
ProxyConfig proxyConfig = MojoUtils.getProxyConfig(this.session, this.decrypter);
Expand All @@ -75,12 +95,12 @@ public void execute(FrontendPluginFactory factory) throws InstallationException
.setUserName(server.getUsername()).install();
factory.getYarnInstaller(proxyConfig).setYarnDownloadRoot(this.yarnDownloadRoot)
.setYarnVersion(this.yarnVersion).setUserName(server.getUsername())
.setPassword(server.getPassword()).install();
.setPassword(server.getPassword()).setIsYarnBerry(isYarnrcYamlFilePresent()).install();
} else {
factory.getNodeInstaller(proxyConfig).setNodeDownloadRoot(this.nodeDownloadRoot)
.setNodeVersion(this.nodeVersion).install();
factory.getYarnInstaller(proxyConfig).setYarnDownloadRoot(this.yarnDownloadRoot)
.setYarnVersion(this.yarnVersion).install();
.setYarnVersion(this.yarnVersion).setIsYarnBerry(isYarnrcYamlFilePresent()).install();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class YarnInstaller {

private String yarnVersion, yarnDownloadRoot, userName, password;

private boolean isYarnBerry;

private final Logger logger;

private final InstallConfig config;
Expand All @@ -43,6 +45,11 @@ public YarnInstaller setYarnVersion(String yarnVersion) {
return this;
}

public YarnInstaller setIsYarnBerry(boolean isYarnBerry) {
this.isYarnBerry = isYarnBerry;
return this;
}

public YarnInstaller setYarnDownloadRoot(String yarnDownloadRoot) {
this.yarnDownloadRoot = yarnDownloadRoot;
return this;
Expand Down Expand Up @@ -85,8 +92,13 @@ private boolean yarnIsAlreadyInstalled() {
logger.info("Yarn {} is already installed.", version);
return true;
} else {
logger.info("Yarn {} was installed, but we need version {}", version, yarnVersion);
return false;
if (isYarnBerry && Integer.parseInt(version.split("\\.")[0]) > 1) {
logger.info("Yarn Berry {} is installed.", version);
return true;
} else{
logger.info("Yarn {} was installed, but we need version {}", version, yarnVersion);
return false;
}
}
} else {
return false;
Expand Down