You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by bm...@apache.org on 2024/03/03 12:09:21 UTC
(maven-scripting-plugin) branch test_bindings_and_romains_script created (now 8b2e016)
This is an automated email from the ASF dual-hosted git repository.
bmarwell pushed a change to branch test_bindings_and_romains_script
in repository https://gitbox.apache.org/repos/asf/maven-scripting-plugin.git
at 8b2e016 (MOCKUP) use context's logger instead
This branch includes the following new commits:
new ac112f5 [MSCRIPTING-11] Enhanced script context by MavenSession, MojoExecution, PluginDescriptor and Settings
new 8b2e016 (MOCKUP) use context's logger instead
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(maven-scripting-plugin) 01/02: [MSCRIPTING-11] Enhanced script context by MavenSession, MojoExecution, PluginDescriptor and Settings
Posted by bm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
bmarwell pushed a commit to branch test_bindings_and_romains_script
in repository https://gitbox.apache.org/repos/asf/maven-scripting-plugin.git
commit ac112f5b1137907ea7633a22d72b971be71dd012
Author: XKartalH <xk...@hdpgmbh.de>
AuthorDate: Thu Jan 26 16:14:24 2023 +0100
[MSCRIPTING-11] Enhanced script context by MavenSession, MojoExecution, PluginDescriptor and Settings
---
.../org/apache/maven/plugins/scripting/EvalMojo.java | 20 ++++++++++++++++++++
src/site/markdown/script-context.md | 6 +++++-
2 files changed, 25 insertions(+), 1 deletion(-)
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..63fec56 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
@@ -24,12 +24,16 @@ 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
@@ -64,14 +68,30 @@ public class EvalMojo extends AbstractMojo {
@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());
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`
(maven-scripting-plugin) 02/02: (MOCKUP) use context's logger instead
Posted by bm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
bmarwell pushed a commit to branch test_bindings_and_romains_script
in repository https://gitbox.apache.org/repos/asf/maven-scripting-plugin.git
commit 8b2e016c2beab616a8c937430a0bf996fe8ad11a
Author: Benjamin Marwell <bm...@apache.org>
AuthorDate: Sun Mar 3 13:07:51 2024 +0100
(MOCKUP) use context's logger instead
---
.../plugins/scripting/AbstractScriptEvaluator.java | 9 +---
.../apache/maven/plugins/scripting/EvalMojo.java | 2 +-
.../scripting/engine/ContextAwareEngine.java | 49 ----------------------
.../maven/plugins/scripting/engine/JavaEngine.java | 29 ++++++-------
4 files changed, 15 insertions(+), 74 deletions(-)
diff --git a/src/main/java/org/apache/maven/plugins/scripting/AbstractScriptEvaluator.java b/src/main/java/org/apache/maven/plugins/scripting/AbstractScriptEvaluator.java
index 80d6d7e..a8cdb9b 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/AbstractScriptEvaluator.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/AbstractScriptEvaluator.java
@@ -24,9 +24,6 @@ import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
-import org.apache.maven.plugin.logging.Log;
-import org.apache.maven.plugins.scripting.engine.ContextAwareEngine;
-
/**
* Evaluates a script in the appropriate context and return its possibly null result
* @author Rusi Popov
@@ -35,17 +32,13 @@ abstract class AbstractScriptEvaluator {
/**
* @param bindings not null bindings to provide to the script to execute
- * @param log engine logger if context aware.
* @return the possibly null result the script produced
* @throws UnsupportedScriptEngineException when the engine is not configured correctly
* @throws ScriptException if an error occurs in script.
*/
- public final Object eval(Bindings bindings, Log log) throws ScriptException, UnsupportedScriptEngineException {
+ public final Object eval(Bindings bindings) throws ScriptException, UnsupportedScriptEngineException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = getEngine(manager);
- if (ContextAwareEngine.class.isInstance(engine)) {
- ContextAwareEngine.class.cast(engine).setLog(log);
- }
ScriptContext context = engine.getContext();
context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
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 63fec56..d166090 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
@@ -93,7 +93,7 @@ public class EvalMojo extends AbstractMojo {
bindings.put("mojoExecution", mojoExecution);
bindings.put("settings", settings);
- Object result = execute.eval(bindings, getLog());
+ Object result = execute.eval(bindings);
getLog().info("Result:");
if (result != null) {
diff --git a/src/main/java/org/apache/maven/plugins/scripting/engine/ContextAwareEngine.java b/src/main/java/org/apache/maven/plugins/scripting/engine/ContextAwareEngine.java
deleted file mode 100644
index 926431d..0000000
--- a/src/main/java/org/apache/maven/plugins/scripting/engine/ContextAwareEngine.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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.engine;
-
-/*
- * 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.
- */
-
-import org.apache.maven.plugin.logging.Log;
-
-/**
- * Enables a custom engine to get some mojo context.
- */
-public interface ContextAwareEngine {
- default void setLog(Log log) {
- // no-op
- }
-}
diff --git a/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java b/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java
index e9d06af..fc26af0 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java
@@ -73,20 +73,13 @@ import static java.util.stream.Collectors.joining;
/**
* The java engine implementation.
*/
-public class JavaEngine extends AbstractScriptEngine implements Compilable, ContextAwareEngine {
+public class JavaEngine extends AbstractScriptEngine implements Compilable {
private final ScriptEngineFactory factory;
- private Log log;
-
public JavaEngine(ScriptEngineFactory factory) {
this.factory = factory;
}
- @Override
- public void setLog(Log log) {
- this.log = log;
- }
-
@Override
public CompiledScript compile(String script) throws ScriptException {
// plexus compiler is great but overkill there so don't bring it just for that
@@ -159,8 +152,8 @@ public class JavaEngine extends AbstractScriptEngine implements Compilable, Cont
}
});
} catch (IOException e) {
- if (log != null) {
- log.debug(e);
+ if (getLog() != null) {
+ getLog().debug(e);
}
}
}
@@ -170,8 +163,8 @@ public class JavaEngine extends AbstractScriptEngine implements Compilable, Cont
private String mavenClasspathPrefix() {
final String home = System.getProperty("maven.home");
if (home == null) {
- if (log != null) {
- log.debug("No maven.home set");
+ if (getLog() != null) {
+ getLog().debug("No maven.home set");
}
return "";
}
@@ -183,8 +176,8 @@ public class JavaEngine extends AbstractScriptEngine implements Compilable, Cont
.map(Path::toString)
.collect(joining(File.pathSeparator, "", File.pathSeparator));
} catch (IOException e) {
- if (log != null) {
- log.debug(e);
+ if (getLog() != null) {
+ getLog().debug(e);
}
return "";
}
@@ -320,6 +313,10 @@ public class JavaEngine extends AbstractScriptEngine implements Compilable, Cont
return factory;
}
+ private Log getLog() {
+ return (Log) this.getContext().getAttribute("log");
+ }
+
private void doClose(final CompiledScript compile) {
if (!AutoCloseable.class.isInstance(compile)) {
return;
@@ -327,8 +324,8 @@ public class JavaEngine extends AbstractScriptEngine implements Compilable, Cont
try {
AutoCloseable.class.cast(compile).close();
} catch (Exception e) {
- if (log != null) {
- log.debug(e);
+ if (getLog() != null) {
+ getLog().debug(e);
}
}
}