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

Add m2e support for npm, npx and yarn goals #955

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add m2e support for npm, npx and yarn goals
  • Loading branch information
David Mell committed Feb 12, 2021
commit d1138a96b965beb1f4eb4cc305c1308cfbb53f40
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

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

/**
* Grunt arguments. Default is empty (runs just the "grunt" command).
* Ember arguments. Default is empty (runs just the "ember" command).
*/
@Parameter(property = "frontend.ember.arguments")
private String arguments;
Expand Down Expand Up @@ -64,11 +65,13 @@ public synchronized void execute(FrontendPluginFactory factory) throws TaskRunne
factory.getEmberRunner().execute(arguments, environmentVariables);

if (outputdir != null) {
getLog().info("Refreshing files after ember: " + outputdir);
getLog().info("Refreshing files after 'ember" + (arguments != null ? " " + arguments : "") + "': " + outputdir);
buildContext.refresh(outputdir);
}
} else {
getLog().info("Skipping ember as no modified files in " + srcdir);
getLog().info("Skipping 'ember" + (arguments != null ? " " + arguments : "") + "' as "
+ triggerfiles.stream().map(Object::toString).collect(Collectors.joining(", "))
+ " unchanged" + (srcdir != null ? " and no modified files in: " + srcdir : ""));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Mojo(name="grunt", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public final class GruntMojo extends AbstractFrontendMojo {
Expand Down Expand Up @@ -64,11 +65,13 @@ public synchronized void execute(FrontendPluginFactory factory) throws TaskRunne
factory.getGruntRunner().execute(arguments, environmentVariables);

if (outputdir != null) {
getLog().info("Refreshing files after grunt: " + outputdir);
getLog().info("Refreshing files after 'grunt" + (arguments != null ? " " + arguments : "") + "': " + outputdir);
buildContext.refresh(outputdir);
}
} else {
getLog().info("Skipping grunt as no modified files in " + srcdir);
getLog().info("Skipping 'grunt" + (arguments != null ? " " + arguments : "") + "' as "
+ triggerfiles.stream().map(Object::toString).collect(Collectors.joining(", "))
+ " unchanged" + (srcdir != null ? " and no modified files in: " + srcdir : ""));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Mojo(name="gulp", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public final class GulpMojo extends AbstractFrontendMojo {
Expand Down Expand Up @@ -64,11 +65,13 @@ public synchronized void execute(FrontendPluginFactory factory) throws TaskRunne
factory.getGulpRunner().execute(arguments, environmentVariables);

if (outputdir != null) {
getLog().info("Refreshing files after gulp: " + outputdir);
getLog().info("Refreshing files after 'gulp" + (arguments != null ? " " + arguments : "") + "': " + outputdir);
buildContext.refresh(outputdir);
}
} else {
getLog().info("Skipping gulp as no modified files in " + srcdir);
getLog().info("Skipping 'gulp" + (arguments != null ? " " + arguments : "") + "' as "
+ triggerfiles.stream().map(Object::toString).collect(Collectors.joining(", "))
+ " unchanged" + (srcdir != null ? " and no modified files in: " + srcdir : ""));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static boolean shouldExecute(BuildContext buildContext, List<File> triggerfiles,
}

if (srcdir == null) {
return true;
return triggerfiles == null;
}

// Check for changes in the srcdir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import org.sonatype.plexus.build.incremental.BuildContext;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Mojo(name="npm", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public final class NpmMojo extends AbstractFrontendMojo {
Expand Down Expand Up @@ -43,6 +46,29 @@ public final class NpmMojo extends AbstractFrontendMojo {
@Component(role = SettingsDecrypter.class)
private SettingsDecrypter decrypter;

/**
* Files that should be checked for changes, in addition to the srcdir files.
* Defaults to package.json in the {@link #workingDirectory}.
*/
@Parameter(property = "triggerfiles")
private List<File> triggerfiles;

/**
* The directory containing front end files that will be processed by npm.
* If this is set then files in the directory will be checked for
* modifications before running npm.
*/
@Parameter(property = "srcdir")
private File srcdir;

/**
* The directory where front end files will be output by npm. If this is
* set then they will be refreshed so they correctly show as modified in
* Eclipse.
*/
@Parameter(property = "outputdir")
private File outputdir;

/**
* Skips execution of this mojo.
*/
Expand All @@ -56,15 +82,29 @@ protected boolean skipExecution() {

@Override
public synchronized void execute(FrontendPluginFactory factory) throws TaskRunnerException {
File packageJson = new File(workingDirectory, "package.json");
if (buildContext == null || buildContext.hasDelta(packageJson) || !buildContext.isIncremental()) {
if (shouldExecute()) {
ProxyConfig proxyConfig = getProxyConfig();
factory.getNpmRunner(proxyConfig, getRegistryUrl()).execute(arguments, environmentVariables);

if (outputdir != null) {
getLog().info("Refreshing files after 'npm" + (arguments != null ? " " + arguments : "") + "': " + outputdir);
buildContext.refresh(outputdir);
}
} else {
getLog().info("Skipping npm install as package.json unchanged");
getLog().info("Skipping 'npm" + (arguments != null ? " " + arguments : "") + "' as "
+ triggerfiles.stream().map(Object::toString).collect(Collectors.joining(", "))
+ " unchanged" + (srcdir != null ? " and no modified files in: " + srcdir : ""));
}
}

private boolean shouldExecute() {
if (triggerfiles == null || triggerfiles.isEmpty()) {
triggerfiles = Arrays.asList(new File(workingDirectory, "package.json"));
}

return MojoUtils.shouldExecute(buildContext, triggerfiles, srcdir);
}

private ProxyConfig getProxyConfig() {
if (npmInheritsProxyConfigFromMaven) {
return MojoUtils.getProxyConfig(session, decrypter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
import org.sonatype.plexus.build.incremental.BuildContext;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Mojo(name="npx", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
public final class NpxMojo extends AbstractFrontendMojo {

private static final String NPM_REGISTRY_URL = "npmRegistryURL";

/**
* npm arguments. Default is "install".
* npx arguments. Default is "install".
*/
@Parameter(defaultValue = "install", property = "frontend.npx.arguments", required = false)
private String arguments;
Expand All @@ -43,6 +46,29 @@ public final class NpxMojo extends AbstractFrontendMojo {
@Component(role = SettingsDecrypter.class)
private SettingsDecrypter decrypter;

/**
* Files that should be checked for changes, in addition to the srcdir files.
* Defaults to package.json in the {@link #workingDirectory}.
*/
@Parameter(property = "triggerfiles")
private List<File> triggerfiles;

/**
* The directory containing front end files that will be processed by npx.
* If this is set then files in the directory will be checked for
* modifications before running npx.
*/
@Parameter(property = "srcdir")
private File srcdir;

/**
* The directory where front end files will be output by npx. If this is
* set then they will be refreshed so they correctly show as modified in
* Eclipse.
*/
@Parameter(property = "outputdir")
private File outputdir;

/**
* Skips execution of this mojo.
*/
Expand All @@ -56,15 +82,29 @@ protected boolean skipExecution() {

@Override
public void execute(FrontendPluginFactory factory) throws TaskRunnerException {
File packageJson = new File(workingDirectory, "package.json");
if (buildContext == null || buildContext.hasDelta(packageJson) || !buildContext.isIncremental()) {
if (shouldExecute()) {
ProxyConfig proxyConfig = getProxyConfig();
factory.getNpxRunner(proxyConfig, getRegistryUrl()).execute(arguments, environmentVariables);

if (outputdir != null) {
getLog().info("Refreshing files after 'npx" + (arguments != null ? " " + arguments : "") + "': " + outputdir);
buildContext.refresh(outputdir);
}
} else {
getLog().info("Skipping npm install as package.json unchanged");
getLog().info("Skipping 'npx" + (arguments != null ? " " + arguments : "") + "' as "
+ triggerfiles.stream().map(Object::toString).collect(Collectors.joining(", "))
+ " unchanged" + (srcdir != null ? " and no modified files in: " + srcdir : ""));
}
}

private boolean shouldExecute() {
if (triggerfiles == null || triggerfiles.isEmpty()) {
triggerfiles = Arrays.asList(new File(workingDirectory, "package.json"));
}

return MojoUtils.shouldExecute(buildContext, triggerfiles, srcdir);
}

private ProxyConfig getProxyConfig() {
if (npmInheritsProxyConfigFromMaven) {
return MojoUtils.getProxyConfig(session, decrypter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Mojo(name="webpack", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public final class WebpackMojo extends AbstractFrontendMojo {
Expand Down Expand Up @@ -64,11 +65,13 @@ public synchronized void execute(FrontendPluginFactory factory) throws TaskRunne
factory.getWebpackRunner().execute(arguments, environmentVariables);

if (outputdir != null) {
getLog().info("Refreshing files after webpack: " + outputdir);
getLog().info("Refreshing files after 'webpack" + (arguments != null ? " " + arguments : "") + "': " + outputdir);
buildContext.refresh(outputdir);
}
} else {
getLog().info("Skipping webpack as no modified files in " + srcdir);
getLog().info("Skipping 'webpack" + (arguments != null ? " " + arguments : "") + "' as "
+ triggerfiles.stream().map(Object::toString).collect(Collectors.joining(", "))
+ " unchanged" + (srcdir != null ? " and no modified files in: " + srcdir : ""));
}
}

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

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.Component;
Expand All @@ -21,7 +24,7 @@ public final class YarnMojo extends AbstractFrontendMojo {
private static final String NPM_REGISTRY_URL = "npmRegistryURL";

/**
* npm arguments. Default is "install".
* yarn arguments. Default is "install".
*/
@Parameter(defaultValue = "", property = "frontend.yarn.arguments", required = false)
private String arguments;
Expand All @@ -45,6 +48,29 @@ public final class YarnMojo extends AbstractFrontendMojo {
@Component(role = SettingsDecrypter.class)
private SettingsDecrypter decrypter;

/**
* Files that should be checked for changes, in addition to the srcdir files.
* Defaults to package.json in the {@link #workingDirectory}.
*/
@Parameter(property = "triggerfiles")
private List<File> triggerfiles;

/**
* The directory containing front end files that will be processed by yarn.
* If this is set then files in the directory will be checked for
* modifications before running yarn.
*/
@Parameter(property = "srcdir")
private File srcdir;

/**
* The directory where front end files will be output by yarn. If this is
* set then they will be refreshed so they correctly show as modified in
* Eclipse.
*/
@Parameter(property = "outputdir")
private File outputdir;

/**
* Skips execution of this mojo.
*/
Expand All @@ -58,20 +84,32 @@ protected boolean skipExecution() {

@Override
public synchronized void execute(FrontendPluginFactory factory) throws TaskRunnerException {
File packageJson = new File(this.workingDirectory, "package.json");
if (this.buildContext == null || this.buildContext.hasDelta(packageJson)
|| !this.buildContext.isIncremental()) {
if (shouldExecute()) {
ProxyConfig proxyConfig = getProxyConfig();
factory.getYarnRunner(proxyConfig, getRegistryUrl()).execute(this.arguments,
this.environmentVariables);
factory.getYarnRunner(proxyConfig, getRegistryUrl()).execute(arguments, environmentVariables);

if (outputdir != null) {
getLog().info("Refreshing files after 'yarn" + (arguments != null ? " " + arguments : "") + "': " + outputdir);
buildContext.refresh(outputdir);
}
} else {
getLog().info("Skipping yarn install as package.json unchanged");
getLog().info("Skipping 'yarn" + (arguments != null ? " " + arguments : "") + "' as "
+ triggerfiles.stream().map(Object::toString).collect(Collectors.joining(", "))
+ " unchanged" + (srcdir != null ? " and no modified files in: " + srcdir : ""));
}
}

private boolean shouldExecute() {
if (triggerfiles == null || triggerfiles.isEmpty()) {
triggerfiles = Arrays.asList(new File(workingDirectory, "package.json"));
}

return MojoUtils.shouldExecute(buildContext, triggerfiles, srcdir);
}

private ProxyConfig getProxyConfig() {
if (this.yarnInheritsProxyConfigFromMaven) {
return MojoUtils.getProxyConfig(this.session, this.decrypter);
return MojoUtils.getProxyConfig(session, decrypter);
} else {
getLog().info("yarn not inheriting proxy config from Maven");
return new ProxyConfig(Collections.<ProxyConfig.Proxy>emptyList());
Expand All @@ -80,6 +118,6 @@ private ProxyConfig getProxyConfig() {

private String getRegistryUrl() {
// check to see if overridden via `-D`, otherwise fallback to pom value
return System.getProperty(NPM_REGISTRY_URL, this.npmRegistryURL);
return System.getProperty(NPM_REGISTRY_URL, npmRegistryURL);
}
}