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

New Mojo to run node with arbitrary arguments #997

Open
wants to merge 5 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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ to see how it should be set up: https://github.com/eirslett/frontend-maven-plugi
- [jspm](#running-jspm)
- [karma](#running-karma)
- [webpack](#running-webpack)
- [node](#running-node)
- Configuration
- [Working Directory](#working-directory)
- [Installation Directory](#installation-directory)
Expand Down Expand Up @@ -399,6 +400,25 @@ will help to separate your frontend and backend builds even more.
</execution>
```

### Running node
You may need to run `node` with arbitrary arguments. Check this Mojo :
```xml
<execution>
<id>compile-xsl</id>
<goals>
<goal>node</goal>
</goals>

<!-- optional: the default phase is "compile" -->
<phase>compile</phase>

<!-- required: arguments to launch node with -->
<configuration>
<arguments>node_modules/xslt3/xslt3.js -xsl:src/main/xsl/md-to-xml.xsl -export:target/webapp/md-to-xml.sef.json</arguments>
</configuration>
</execution>
```

### Optional Configuration

#### Working directory
Expand Down Expand Up @@ -529,6 +549,7 @@ Tools and property to enable skipping
* jspm `-Dskip.jspm`
* karma `-Dskip.karma`
* webpack `-Dskip.webpack`
* node `-Dskip.node`

## Eclipse M2E support

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

import com.github.eirslett.maven.plugins.frontend.lib.FrontendException;
import com.github.eirslett.maven.plugins.frontend.lib.FrontendPluginFactory;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

@Mojo(name="node", defaultPhase = LifecyclePhase.COMPILE, threadSafe = true)
public class NodeMojo extends AbstractFrontendMojo {
/**
* npm arguments. Default is "install".
*/
@Parameter(defaultValue = "", property = "frontend.node.arguments", required = false)
private String arguments;

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

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

@Override
protected void execute(FrontendPluginFactory factory) throws FrontendException {
getLog().info("Running NodeMojo");
factory.getNodeRunner().execute(arguments, environmentVariables);
}

@Override
protected boolean skipExecution() {
return skip;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ private InstallConfig getInstallConfig() {
private static final CacheResolver getDefaultCacheResolver(File root) {
return new DirectoryCacheResolver(new File(root, DEFAULT_CACHE_PATH));
}

public NodeRunner getNodeRunner() {
return new NodeRunner(getExecutorConfig());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.eirslett.maven.plugins.frontend.lib;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public class NodeRunner {
private final NodeExecutorConfig config;
private final ArgumentsParser argumentsParser;
private final Logger logger = LoggerFactory.getLogger(NodeRunner.class);

public NodeRunner(NodeExecutorConfig config) {
super();
this.config = config;
argumentsParser = new ArgumentsParser();
}

public void execute(String arguments, Map<String, String> environmentVariables) throws FrontendException {
NodeExecutor nodeExecutor = new NodeExecutor(config, getArguments(arguments), environmentVariables);
try {
nodeExecutor.executeAndRedirectOutput(logger);
} catch (ProcessExecutionException e) {
throw new FrontendException("executing node "+arguments, e);
}
}

private List<String> getArguments(String arguments) {
return argumentsParser.parse(arguments);
}
}