From 7770cef03f853975d7f0ab9f5fb5b54e94f8cc3e Mon Sep 17 00:00:00 2001 From: XKartalH Date: Thu, 26 Jan 2023 16:14:24 +0100 Subject: [PATCH 1/3] [MSCRIPTING-11] Enhanced script context by MavenSession, MojoExecution, PluginDescriptor and Settings # Conflicts: # src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java --- .../maven/plugins/scripting/EvalMojo.java | 236 ++++++++++-------- src/site/markdown/script-context.md | 6 +- 2 files changed, 133 insertions(+), 109 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java index ec55f24..9a0fa4b 100644 --- a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java +++ b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java @@ -1,108 +1,128 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.plugins.scripting; - -import javax.script.Bindings; -import javax.script.ScriptException; -import javax.script.SimpleBindings; - -import java.io.File; - -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; - -/** - * Evaluate the specified script or scriptFile - * - * @author Robert Scholte - * @since 3.0.0 - */ -@Mojo(name = "eval") -public class EvalMojo extends AbstractMojo { - @Parameter - private String engineName; - - /** - * When used, also specify the engineName - */ - @Parameter - private String script; - - /** - * Provide the script as an external file as an alternative to <script>. - * When scriptFile provided the script is ignored. - * The file name extension identifies the script language to use, as of javax.script.ScriptEngineManager - * and {@linkplain "https://jcp.org/aboutJava/communityprocess/final/jsr223/index.html"} - */ - @Parameter - private File scriptFile; - - @Parameter - String scriptResource; - - // script variables - @Parameter(defaultValue = "${project}", readonly = true) - private MavenProject project; - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - try { - AbstractScriptEvaluator execute = constructExecute(); - - Bindings bindings = new SimpleBindings(); - bindings.put("project", project); - bindings.put("log", getLog()); - - Object result = execute.eval(bindings, getLog()); - - getLog().info("Result:"); - if (result != null) { - getLog().info(result.toString()); - } - } catch (ScriptException e) // configuring the plugin failed - { - throw new MojoExecutionException(e.getMessage(), e); - } catch (UnsupportedScriptEngineException e) // execution failure - { - throw new MojoFailureException(e.getMessage(), e); - } - } - - private AbstractScriptEvaluator constructExecute() throws IllegalArgumentException { - AbstractScriptEvaluator execute; - - if (scriptFile != null) { - execute = new FileScriptEvaluator(engineName, scriptFile); - - } else if (scriptResource != null) { - execute = new ResourceScriptEvaluator(engineName, scriptResource); - - } else if (script != null) { - execute = new StringScriptEvaluator(engineName, script); - - } else { - throw new IllegalArgumentException("Missing script or scriptFile provided"); - } - return execute; - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins.scripting; + +import javax.script.Bindings; +import javax.script.ScriptException; +import javax.script.SimpleBindings; + +import java.io.File; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; +import org.apache.maven.settings.Settings; + +/** + * Evaluate the specified script or scriptFile + * + * @author Robert Scholte + * @since 3.0.0 + */ +@Mojo(name = "eval") +public class EvalMojo extends AbstractMojo { + @Parameter + private String engineName; + + /** + * When used, also specify the engineName + */ + @Parameter + private String script; + + /** + * Provide the script as an external file as an alternative to <script>. + * When scriptFile provided the script is ignored. + * The file name extension identifies the script language to use, as of javax.script.ScriptEngineManager + * and {@linkplain "https://jcp.org/aboutJava/communityprocess/final/jsr223/index.html"} + */ + @Parameter + private File scriptFile; + + @Parameter + String scriptResource; + + // script variables + @Parameter(defaultValue = "${project}", readonly = true) + private MavenProject project; + + @Parameter( defaultValue = "${mojoExecution}", readonly = true ) + MojoExecution mojoExecution; + + @Parameter( defaultValue = "${pluginDescriptor}", readonly = true ) + private PluginDescriptor pluginDescriptor; + + @Parameter( defaultValue = "${session}", readonly = true ) + private MavenSession session; + + @Parameter( defaultValue = "${settings}", readonly = true ) + private Settings settings; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + try { + AbstractScriptEvaluator execute = constructExecute(); + + Bindings bindings = new SimpleBindings(); + bindings.put( "session", session ); + bindings.put("project", project); + bindings.put( "pluginDescriptor", pluginDescriptor ); + bindings.put("log", getLog()); + bindings.put( "mojoExecution", mojoExecution ); + bindings.put( "settings", settings ); + + Object result = execute.eval(bindings, getLog()); + + getLog().info("Result:"); + if (result != null) { + getLog().info(result.toString()); + } + } catch (ScriptException e) // configuring the plugin failed + { + throw new MojoExecutionException(e.getMessage(), e); + } catch (UnsupportedScriptEngineException e) // execution failure + { + throw new MojoFailureException(e.getMessage(), e); + } + } + + private AbstractScriptEvaluator constructExecute() throws IllegalArgumentException { + AbstractScriptEvaluator execute; + + if (scriptFile != null) { + execute = new FileScriptEvaluator(engineName, scriptFile); + + } else if (scriptResource != null) { + execute = new ResourceScriptEvaluator(engineName, scriptResource); + + } else if (script != null) { + execute = new StringScriptEvaluator(engineName, script); + + } else { + throw new IllegalArgumentException("Missing script or scriptFile provided"); + } + return execute; + } +} diff --git a/src/site/markdown/script-context.md b/src/site/markdown/script-context.md index 03ed850..5425422 100644 --- a/src/site/markdown/script-context.md +++ b/src/site/markdown/script-context.md @@ -20,5 +20,9 @@ under the License. The following variables are available in the script context + * `org.apache.maven.execution.MavenSession session` * `org.apache.maven.project.MavenProject project` - * `org.apache.maven.plugin.logging.Log log` \ No newline at end of file + * `org.apache.maven.plugin.descriptor.PluginDescriptor pluginDescriptor` + * `org.apache.maven.plugin.logging.Log log` + * `org.apache.maven.plugin.MojoExecution mojoExecution` + * `org.apache.maven.settings.Settings settings` From 9910bc5c9b9c9a09c645798c68cc7ccd4c34a6bb Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 29 Aug 2024 20:25:14 +0000 Subject: [PATCH 2/3] Bump org.apache.maven:maven-core from 3.6.3 to 3.8.1 Bumps [org.apache.maven:maven-core](https://github.com/apache/maven) from 3.6.3 to 3.8.1. - [Release notes](https://github.com/apache/maven/releases) - [Commits](https://github.com/apache/maven/compare/maven-3.6.3...maven-3.8.1) --- updated-dependencies: - dependency-name: org.apache.maven:maven-core dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 42b2161..86ba366 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ under the License. - 3.6.3 + 3.8.1 2021-02-24T19:52:25Z From 3a484a7c960c227e3069b917c36849c4a31a9010 Mon Sep 17 00:00:00 2001 From: "hk@hsyn.de" Date: Thu, 29 Aug 2024 23:03:23 +0200 Subject: [PATCH 3/3] [MSCRIPTING-11] applied spotless --- .../maven/plugins/scripting/EvalMojo.java | 256 +++++++++--------- 1 file changed, 128 insertions(+), 128 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java index 9a0fa4b..63fec56 100644 --- a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java +++ b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java @@ -1,128 +1,128 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.plugins.scripting; - -import javax.script.Bindings; -import javax.script.ScriptException; -import javax.script.SimpleBindings; - -import java.io.File; - -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecution; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugin.descriptor.PluginDescriptor; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; -import org.apache.maven.settings.Settings; - -/** - * Evaluate the specified script or scriptFile - * - * @author Robert Scholte - * @since 3.0.0 - */ -@Mojo(name = "eval") -public class EvalMojo extends AbstractMojo { - @Parameter - private String engineName; - - /** - * When used, also specify the engineName - */ - @Parameter - private String script; - - /** - * Provide the script as an external file as an alternative to <script>. - * When scriptFile provided the script is ignored. - * The file name extension identifies the script language to use, as of javax.script.ScriptEngineManager - * and {@linkplain "https://jcp.org/aboutJava/communityprocess/final/jsr223/index.html"} - */ - @Parameter - private File scriptFile; - - @Parameter - String scriptResource; - - // script variables - @Parameter(defaultValue = "${project}", readonly = true) - private MavenProject project; - - @Parameter( defaultValue = "${mojoExecution}", readonly = true ) - MojoExecution mojoExecution; - - @Parameter( defaultValue = "${pluginDescriptor}", readonly = true ) - private PluginDescriptor pluginDescriptor; - - @Parameter( defaultValue = "${session}", readonly = true ) - private MavenSession session; - - @Parameter( defaultValue = "${settings}", readonly = true ) - private Settings settings; - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - try { - AbstractScriptEvaluator execute = constructExecute(); - - Bindings bindings = new SimpleBindings(); - bindings.put( "session", session ); - bindings.put("project", project); - bindings.put( "pluginDescriptor", pluginDescriptor ); - bindings.put("log", getLog()); - bindings.put( "mojoExecution", mojoExecution ); - bindings.put( "settings", settings ); - - Object result = execute.eval(bindings, getLog()); - - getLog().info("Result:"); - if (result != null) { - getLog().info(result.toString()); - } - } catch (ScriptException e) // configuring the plugin failed - { - throw new MojoExecutionException(e.getMessage(), e); - } catch (UnsupportedScriptEngineException e) // execution failure - { - throw new MojoFailureException(e.getMessage(), e); - } - } - - private AbstractScriptEvaluator constructExecute() throws IllegalArgumentException { - AbstractScriptEvaluator execute; - - if (scriptFile != null) { - execute = new FileScriptEvaluator(engineName, scriptFile); - - } else if (scriptResource != null) { - execute = new ResourceScriptEvaluator(engineName, scriptResource); - - } else if (script != null) { - execute = new StringScriptEvaluator(engineName, script); - - } else { - throw new IllegalArgumentException("Missing script or scriptFile provided"); - } - return execute; - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins.scripting; + +import javax.script.Bindings; +import javax.script.ScriptException; +import javax.script.SimpleBindings; + +import java.io.File; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; +import org.apache.maven.settings.Settings; + +/** + * Evaluate the specified script or scriptFile + * + * @author Robert Scholte + * @since 3.0.0 + */ +@Mojo(name = "eval") +public class EvalMojo extends AbstractMojo { + @Parameter + private String engineName; + + /** + * When used, also specify the engineName + */ + @Parameter + private String script; + + /** + * Provide the script as an external file as an alternative to <script>. + * When scriptFile provided the script is ignored. + * The file name extension identifies the script language to use, as of javax.script.ScriptEngineManager + * and {@linkplain "https://jcp.org/aboutJava/communityprocess/final/jsr223/index.html"} + */ + @Parameter + private File scriptFile; + + @Parameter + String scriptResource; + + // script variables + @Parameter(defaultValue = "${project}", readonly = true) + private MavenProject project; + + @Parameter(defaultValue = "${mojoExecution}", readonly = true) + MojoExecution mojoExecution; + + @Parameter(defaultValue = "${pluginDescriptor}", readonly = true) + private PluginDescriptor pluginDescriptor; + + @Parameter(defaultValue = "${session}", readonly = true) + private MavenSession session; + + @Parameter(defaultValue = "${settings}", readonly = true) + private Settings settings; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + try { + AbstractScriptEvaluator execute = constructExecute(); + + Bindings bindings = new SimpleBindings(); + bindings.put("session", session); + bindings.put("project", project); + bindings.put("pluginDescriptor", pluginDescriptor); + bindings.put("log", getLog()); + bindings.put("mojoExecution", mojoExecution); + bindings.put("settings", settings); + + Object result = execute.eval(bindings, getLog()); + + getLog().info("Result:"); + if (result != null) { + getLog().info(result.toString()); + } + } catch (ScriptException e) // configuring the plugin failed + { + throw new MojoExecutionException(e.getMessage(), e); + } catch (UnsupportedScriptEngineException e) // execution failure + { + throw new MojoFailureException(e.getMessage(), e); + } + } + + private AbstractScriptEvaluator constructExecute() throws IllegalArgumentException { + AbstractScriptEvaluator execute; + + if (scriptFile != null) { + execute = new FileScriptEvaluator(engineName, scriptFile); + + } else if (scriptResource != null) { + execute = new ResourceScriptEvaluator(engineName, scriptResource); + + } else if (script != null) { + execute = new StringScriptEvaluator(engineName, script); + + } else { + throw new IllegalArgumentException("Missing script or scriptFile provided"); + } + return execute; + } +}