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 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Last public release: [![Maven Central](https://maven-badges.herokuapp.com/maven-

## Changelog

### 1.12.0

* Add m2e support for npm, npx and yarn goals

### 1.11.0

* Upgrade Jackson dependency to Jackson 2.9.10
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,10 @@ Tools and property to enable skipping
## Eclipse M2E support

This plugin contains support for M2E, including lifecycle mappings and support for incremental builds in Eclipse.
The `install-node-and-npm` goal will only run on a full project build. The other goals support incremental builds
to avoid doing unnecessary work. During an incremental build the `npm` goal will only run if the `package.json` file
has been changed. The `grunt` and `gulp` goals have new `srcdir` and `triggerfiles` optional configuration options; if
these are set they check for changes in your source files before being run. See the wiki for more information.
The `install-node-and-npm` and `install-node-and-yarn` goals will only run on a full project build. The other goals support incremental builds
to avoid doing unnecessary work. Most goals have `srcdir` and `triggerfiles` optional configuration options; if
these are set they check for changes in your source files before being run. By default, during an incremental build
the `npm` and `yarn` goals will only run if the `package.json` file has been changed. See the wiki for more information.

## Helper scripts

Expand Down
2 changes: 1 addition & 1 deletion frontend-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<version>3.3</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
Expand Down
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
Loading