From 14ef0a8d473c36be9d270d4e0de6b17c6d1540b7 Mon Sep 17 00:00:00 2001 From: Christophe Marchand Date: Mon, 30 Aug 2021 23:21:03 +0200 Subject: [PATCH 1/5] Issue 996: new Mojo to execute arbitrary node commands --- .../maven/plugins/frontend/mojo/NodeMojo.java | 37 +++++++++++++++++++ .../frontend/lib/FrontendPluginFactory.java | 4 ++ .../plugins/frontend/lib/NodeRunner.java | 34 +++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 frontend-maven-plugin/src/main/java/com/github/eirslett/maven/plugins/frontend/mojo/NodeMojo.java create mode 100644 frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeRunner.java diff --git a/frontend-maven-plugin/src/main/java/com/github/eirslett/maven/plugins/frontend/mojo/NodeMojo.java b/frontend-maven-plugin/src/main/java/com/github/eirslett/maven/plugins/frontend/mojo/NodeMojo.java new file mode 100644 index 000000000..754734f36 --- /dev/null +++ b/frontend-maven-plugin/src/main/java/com/github/eirslett/maven/plugins/frontend/mojo/NodeMojo.java @@ -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; + } +} diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/FrontendPluginFactory.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/FrontendPluginFactory.java index 4ed8b1a6b..f2aa862a4 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/FrontendPluginFactory.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/FrontendPluginFactory.java @@ -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()); + } } diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeRunner.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeRunner.java new file mode 100644 index 000000000..869839fd1 --- /dev/null +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeRunner.java @@ -0,0 +1,34 @@ +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 { + public static final String TASK_NAME = "node"; + public static final String TASK_LOCATION = "node/node"; + 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 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 getArguments(String arguments) { + return argumentsParser.parse(arguments); + } +} \ No newline at end of file From ab1b39e0ce0d49cfb248d4056e8c710971072b99 Mon Sep 17 00:00:00 2001 From: Christophe Marchand Date: Mon, 30 Aug 2021 23:30:32 +0200 Subject: [PATCH 2/5] added use-case to README --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index b0eb33b28..85f4503cf 100644 --- a/README.md +++ b/README.md @@ -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) @@ -399,6 +400,23 @@ will help to separate your frontend and backend builds even more. ``` +### Running node +You may need to run `node` with arbitrary arguments. Check this Mojo : +```xml + + compile-xsl + + node + + + compile + + + node_modules/xslt3/xslt3.js -xsl:src/main/xsl/md-to-xml.xsl -export:target/webapp/md-to-xml.sef.json + + +``` + ### Optional Configuration #### Working directory From 4c84e7716ec7032673a9ce66ad06e95a1e2c8593 Mon Sep 17 00:00:00 2001 From: Christophe Marchand Date: Mon, 30 Aug 2021 23:41:19 +0200 Subject: [PATCH 3/5] removed unused code --- .../github/eirslett/maven/plugins/frontend/lib/NodeRunner.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeRunner.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeRunner.java index 869839fd1..095064178 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeRunner.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeRunner.java @@ -7,8 +7,6 @@ import java.util.Map; public class NodeRunner { - public static final String TASK_NAME = "node"; - public static final String TASK_LOCATION = "node/node"; private final NodeExecutorConfig config; private final ArgumentsParser argumentsParser; private final Logger logger = LoggerFactory.getLogger(NodeRunner.class); From facef54e01785c72fc43bc320e876cecfbe1dfd2 Mon Sep 17 00:00:00 2001 From: Christophe Marchand Date: Mon, 30 Aug 2021 23:41:44 +0200 Subject: [PATCH 4/5] document phase and arguments --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 85f4503cf..5d469ac55 100644 --- a/README.md +++ b/README.md @@ -408,9 +408,11 @@ You may need to run `node` with arbitrary arguments. Check this Mojo : node - + + compile + node_modules/xslt3/xslt3.js -xsl:src/main/xsl/md-to-xml.xsl -export:target/webapp/md-to-xml.sef.json From 3712232c4c51f24566d0efb1f8b61d4647897b86 Mon Sep 17 00:00:00 2001 From: Christophe Marchand Date: Mon, 30 Aug 2021 23:47:44 +0200 Subject: [PATCH 5/5] added skip.node --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5d469ac55..e83f52faf 100644 --- a/README.md +++ b/README.md @@ -549,6 +549,7 @@ Tools and property to enable skipping * jspm `-Dskip.jspm` * karma `-Dskip.karma` * webpack `-Dskip.webpack` +* node `-Dskip.node` ## Eclipse M2E support