This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch support-java-scripting
in repository https://gitbox.apache.org/repos/asf/maven-scripting-plugin.git

commit 430872fc36427c181fadba92c49909b934dcd59a
Author: Romain Manni-Bucau <[email protected]>
AuthorDate: Mon Dec 29 15:53:50 2025 +0100

    rebase on master
---
 .../apache/maven/plugins/scripting/EvalMojo.java   |  16 +++
 .../maven/plugins/scripting/binding/Servers.java   |  55 ++++++++
 src/site/markdown/script-context.md                |   5 +-
 .../plugins/scripting/binding/ServersTest.java     | 153 +++++++++++++++++++++
 4 files changed, 228 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 43f9f22..d30b6a2 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
@@ -18,18 +18,23 @@
  */
 package org.apache.maven.plugins.scripting;
 
+import javax.inject.Inject;
 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.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.plugins.scripting.binding.Servers;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.settings.crypto.SettingsDecrypter;
 
 /**
  * Evaluate the specified script or scriptFile.
@@ -64,14 +69,25 @@ public class EvalMojo extends AbstractMojo {
     @Parameter(defaultValue = "${project}", readonly = true)
     private MavenProject project;
 
+    @Parameter(defaultValue = "${settings}", readonly = true)
+    private Settings settings;
+
+    @Inject
+    private SettingsDecrypter settingsDecrypter;
+
+    @Inject
+    private MavenSession session;
+
     @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("log", getLog());
+            bindings.put("servers", new Servers(settings, settingsDecrypter));
 
             Object result = execute.eval(bindings, getLog());
 
diff --git 
a/src/main/java/org/apache/maven/plugins/scripting/binding/Servers.java 
b/src/main/java/org/apache/maven/plugins/scripting/binding/Servers.java
new file mode 100644
index 0000000..ea3bd4e
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/scripting/binding/Servers.java
@@ -0,0 +1,55 @@
+/*
+ * 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.binding;
+
+import org.apache.maven.settings.Server;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.settings.building.SettingsProblem;
+import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
+import org.apache.maven.settings.crypto.SettingsDecrypter;
+import org.apache.maven.settings.crypto.SettingsDecryptionResult;
+
+import static java.util.stream.Collectors.joining;
+
+/**
+ * Binding which enables to work with servers (from settings.xml) and in 
particular decipher them transparently.
+ */
+public class Servers {
+    private final Settings settings;
+    private final SettingsDecrypter settingsDecrypter;
+
+    public Servers(Settings settings, SettingsDecrypter settingsDecrypter) {
+        this.settings = settings;
+        this.settingsDecrypter = settingsDecrypter;
+    }
+
+    public Server find(String id) {
+        final Server server = settings.getServer(id);
+        if (server == null) {
+            return null;
+        }
+        final SettingsDecryptionResult result = settingsDecrypter.decrypt(new 
DefaultSettingsDecryptionRequest(server));
+        if (!result.getProblems().isEmpty()) {
+            throw new IllegalStateException(
+                    
result.getProblems().stream().map(SettingsProblem::toString).collect(joining("\n")));
+        }
+        final Server decrypted = result.getServer();
+        return decrypted == null ? server : decrypted;
+    }
+}
diff --git a/src/site/markdown/script-context.md 
b/src/site/markdown/script-context.md
index 03ed850..1da4bdf 100644
--- a/src/site/markdown/script-context.md
+++ b/src/site/markdown/script-context.md
@@ -21,4 +21,7 @@ under the License.
 The following variables are available in the script context
 
  * `org.apache.maven.project.MavenProject project`
- * `org.apache.maven.plugin.logging.Log log`
\ No newline at end of file
+ * `org.apache.maven.plugin.logging.Log log`
+ * `org.apache.maven.plugin.logging.Log log`
+ * `org.apache.maven.execution.MavenSession session`
+ * `org.apache.maven.plugins.scripting.binding.Servers servers`: enables to 
access calling `servers.find(<id>)` a deciphered Server instance
\ No newline at end of file
diff --git 
a/src/test/java/org/apache/maven/plugins/scripting/binding/ServersTest.java 
b/src/test/java/org/apache/maven/plugins/scripting/binding/ServersTest.java
new file mode 100644
index 0000000..152f99b
--- /dev/null
+++ b/src/test/java/org/apache/maven/plugins/scripting/binding/ServersTest.java
@@ -0,0 +1,153 @@
+/*
+ * 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.binding;
+
+import java.util.List;
+
+import org.apache.maven.settings.Proxy;
+import org.apache.maven.settings.Server;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.settings.building.SettingsProblem;
+import org.apache.maven.settings.crypto.DefaultSettingsDecrypter;
+import org.apache.maven.settings.crypto.SettingsDecryptionRequest;
+import org.apache.maven.settings.crypto.SettingsDecryptionResult;
+import org.codehaus.plexus.PlexusContainerException;
+import org.junit.jupiter.api.Test;
+import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
+import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
+
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singletonList;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+// sanity check, decrypter impl is mocked for simplicity
+public class ServersTest {
+    @Test
+    public void missing() throws Exception {
+        assertNull(new Servers(
+                        new Settings(),
+                        new DefaultSettingsDecrypter(new 
DefaultSecDispatcher(new DefaultPlexusCipher())))
+                .find("test"));
+    }
+
+    @Test
+    public void clear() throws PlexusContainerException {
+        final Server server = new Server();
+        server.setId("test");
+        server.setUsername("clear");
+        server.setPassword("12345");
+        assertEquals(
+                "12345",
+                new Servers(
+                                new Settings() {
+                                    @Override
+                                    public List<Server> getServers() {
+                                        return singletonList(server);
+                                    }
+                                },
+                                new DefaultSettingsDecrypter(new 
DefaultSecDispatcher(new DefaultPlexusCipher())) {
+                                    @Override
+                                    public SettingsDecryptionResult 
decrypt(SettingsDecryptionRequest request) {
+                                        return new SettingsDecryptionResult() {
+                                            @Override
+                                            public Server getServer() {
+                                                return null;
+                                            }
+
+                                            @Override
+                                            public List<SettingsProblem> 
getProblems() {
+                                                return emptyList();
+                                            }
+
+                                            @Override
+                                            public List<Server> getServers() {
+                                                throw new 
UnsupportedOperationException();
+                                            }
+
+                                            @Override
+                                            public Proxy getProxy() {
+                                                throw new 
UnsupportedOperationException();
+                                            }
+
+                                            @Override
+                                            public List<Proxy> getProxies() {
+                                                throw new 
UnsupportedOperationException();
+                                            }
+                                        };
+                                    }
+                                })
+                        .find("test")
+                        .getPassword());
+    }
+
+    @Test
+    public void ciphered() throws PlexusContainerException {
+        final Server server = new Server();
+        server.setId("test");
+        server.setUsername("not-clear");
+        server.setPassword("ciphered");
+        assertEquals(
+                "12345",
+                new Servers(
+                                new Settings() {
+                                    @Override
+                                    public List<Server> getServers() {
+                                        return singletonList(server);
+                                    }
+                                },
+                                new DefaultSettingsDecrypter(new 
DefaultSecDispatcher(new DefaultPlexusCipher())) {
+                                    @Override
+                                    public SettingsDecryptionResult 
decrypt(SettingsDecryptionRequest request) {
+                                        return new SettingsDecryptionResult() {
+                                            @Override
+                                            public Server getServer() {
+                                                final Server result = new 
Server();
+                                                result.setId(server.getId());
+                                                
result.setUsername(server.getUsername());
+                                                result.setPassword("12345");
+                                                return result;
+                                            }
+
+                                            @Override
+                                            public List<SettingsProblem> 
getProblems() {
+                                                return emptyList();
+                                            }
+
+                                            @Override
+                                            public List<Server> getServers() {
+                                                throw new 
UnsupportedOperationException();
+                                            }
+
+                                            @Override
+                                            public Proxy getProxy() {
+                                                throw new 
UnsupportedOperationException();
+                                            }
+
+                                            @Override
+                                            public List<Proxy> getProxies() {
+                                                throw new 
UnsupportedOperationException();
+                                            }
+                                        };
+                                    }
+                                })
+                        .find("test")
+                        .getPassword());
+    }
+}

Reply via email to