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

elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-jarsigner-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 5a2b3db  Convert to Guice @Inject (#42)
5a2b3db is described below

commit 5a2b3db2ba49916914dd9bdb207cc61363c4ceb7
Author: Elliotte Rusty Harold <elh...@users.noreply.github.com>
AuthorDate: Wed Dec 11 13:06:10 2024 +0000

    Convert to Guice @Inject (#42)
    
    * Convert to Guice @Inject
---
 pom.xml                                            | 14 +++++++++++++-
 .../plugins/jarsigner/AbstractJarsignerMojo.java   | 21 +++++++++++----------
 .../maven/plugins/jarsigner/JarsignerSignMojo.java | 18 ++++++++++++++++++
 .../plugins/jarsigner/JarsignerVerifyMojo.java     | 22 ++++++++++++++++++++--
 .../maven/plugins/jarsigner/MojoTestCreator.java   |  7 ++++---
 5 files changed, 66 insertions(+), 16 deletions(-)

diff --git a/pom.xml b/pom.xml
index 0a3c1ab..10213fd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -120,17 +120,29 @@ under the License.
       <artifactId>maven-jarsigner</artifactId>
       <version>3.1.0</version>
     </dependency>
+    <dependency>
+      <groupId>javax.inject</groupId>
+      <artifactId>javax.inject</artifactId>
+      <version>1</version>
+      <scope>provided</scope>
+    </dependency>
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.13.2</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+      <version>1.7.36</version>
+      <scope>test</scope>
+    </dependency>
     <!-- Used for test cases to perform simple logging without any SLF4J 
warning -->
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-simple</artifactId>
-      <version>2.0.9</version>
+      <version>1.7.36</version>
       <scope>test</scope>
     </dependency>
     <dependency>
diff --git 
a/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java 
b/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java
index db583e9..f6ae5b8 100644
--- 
a/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java
@@ -33,7 +33,6 @@ import org.apache.maven.artifact.Artifact;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.settings.Settings;
@@ -237,11 +236,6 @@ public abstract class AbstractJarsignerMojo extends 
AbstractMojo {
     @Parameter(defaultValue = "${project.basedir}")
     private File workingDirectory;
 
-    /**
-     */
-    @Component
-    private JarSigner jarSigner;
-
     /**
      * The current build session instance. This is used for
      * toolchain manager API calls.
@@ -251,19 +245,26 @@ public abstract class AbstractJarsignerMojo extends 
AbstractMojo {
     @Parameter(defaultValue = "${session}", readonly = true, required = true)
     private MavenSession session;
 
+    private final JarSigner jarSigner;
+
     /**
      * To obtain a toolchain if possible.
      *
      * @since 1.3
      */
-    @Component
-    private ToolchainManager toolchainManager;
+    private final ToolchainManager toolchainManager;
 
     /**
      * @since 1.3.2
      */
-    @Component(hint = "mng-4384")
-    private SecDispatcher securityDispatcher;
+    private final SecDispatcher securityDispatcher;
+
+    protected AbstractJarsignerMojo(
+            JarSigner jarSigner, ToolchainManager toolchainManager, 
SecDispatcher securityDispatcher) {
+        this.jarSigner = jarSigner;
+        this.toolchainManager = toolchainManager;
+        this.securityDispatcher = securityDispatcher;
+    }
 
     @Override
     public final void execute() throws MojoExecutionException {
diff --git 
a/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerSignMojo.java 
b/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerSignMojo.java
index 0a5a417..b0e6c39 100644
--- a/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerSignMojo.java
+++ b/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerSignMojo.java
@@ -18,6 +18,9 @@
  */
 package org.apache.maven.plugins.jarsigner;
 
+import javax.inject.Inject;
+import javax.inject.Named;
+
 import java.io.File;
 import java.io.IOException;
 import java.time.Duration;
@@ -42,6 +45,8 @@ import org.apache.maven.shared.utils.StringUtils;
 import org.apache.maven.shared.utils.cli.Commandline;
 import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
 import org.apache.maven.shared.utils.cli.javatool.JavaToolResult;
+import org.apache.maven.toolchain.ToolchainManager;
+import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
 
 /**
  * Signs a project artifact and attachments using jarsigner.
@@ -225,6 +230,19 @@ public class JarsignerSignMojo extends 
AbstractJarsignerMojo {
     /** Exponent limit for exponential wait after failure function. 2^20 = 
1048576 sec ~= 12 days. */
     private static final int MAX_WAIT_EXPONENT_ATTEMPT = 20;
 
+    @Inject
+    public JarsignerSignMojo(
+            JarSigner jarSigner,
+            ToolchainManager toolchainManager,
+            @Named("mng-4384") SecDispatcher securityDispatcher) {
+        super(jarSigner, toolchainManager, securityDispatcher);
+    }
+
+    // for testing; invoked via reflection
+    JarsignerSignMojo() {
+        super(null, null, null);
+    }
+
     @Override
     protected String getCommandlineInfo(final Commandline commandLine) {
         String commandLineInfo = commandLine != null ? commandLine.toString() 
: null;
diff --git 
a/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerVerifyMojo.java 
b/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerVerifyMojo.java
index 91edc21..b4793a9 100644
--- a/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerVerifyMojo.java
+++ b/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerVerifyMojo.java
@@ -18,6 +18,9 @@
  */
 package org.apache.maven.plugins.jarsigner;
 
+import javax.inject.Inject;
+import javax.inject.Named;
+
 import java.io.File;
 import java.io.IOException;
 
@@ -31,6 +34,8 @@ import org.apache.maven.shared.jarsigner.JarSignerUtil;
 import org.apache.maven.shared.jarsigner.JarSignerVerifyRequest;
 import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
 import org.apache.maven.shared.utils.cli.javatool.JavaToolResult;
+import org.apache.maven.toolchain.ToolchainManager;
+import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
 
 /**
  * Checks the signatures of a project artifact and attachments using jarsigner.
@@ -49,15 +54,28 @@ public class JarsignerVerifyMojo extends 
AbstractJarsignerMojo {
 
     /**
      * When <code>true</code> this will make the execute() operation fail,
-     * throwing an exception, when verifying a non signed jar.
+     * throwing an exception, when verifying an unsigned jar.
      * Primarily to keep backwards compatibility with existing code, and allow 
reusing the
-     * bean in unattended operations when set to <code>false</code>.
+     * mojo in unattended operations when set to <code>false</code>.
      *
      * @since 1.3
      **/
     @Parameter(property = "jarsigner.errorWhenNotSigned", defaultValue = 
"false")
     private boolean errorWhenNotSigned;
 
+    @Inject
+    public JarsignerVerifyMojo(
+            JarSigner jarSigner,
+            ToolchainManager toolchainManager,
+            @Named("mng-4384") SecDispatcher securityDispatcher) {
+        super(jarSigner, toolchainManager, securityDispatcher);
+    }
+
+    // for testing; invoked via reflection
+    JarsignerVerifyMojo() {
+        super(null, null, null);
+    }
+
     /**
      * {@inheritDoc}
      */
diff --git 
a/src/test/java/org/apache/maven/plugins/jarsigner/MojoTestCreator.java 
b/src/test/java/org/apache/maven/plugins/jarsigner/MojoTestCreator.java
index ff5c0f9..b476de6 100644
--- a/src/test/java/org/apache/maven/plugins/jarsigner/MojoTestCreator.java
+++ b/src/test/java/org/apache/maven/plugins/jarsigner/MojoTestCreator.java
@@ -85,10 +85,11 @@ public class MojoTestCreator<T extends 
AbstractJarsignerMojo> {
 
     /**
      * Creates and configures the Mojo instance.
-     * @param configuration user supplied configuration.
+     *
+     * @param configuration user supplied configuration
      */
-    public T configure(Map<String, String> configuration) throws Exception {
-        T mojo = clazz.getDeclaredConstructor().newInstance();
+    public T configure(Map<String, String> configuration, Class<?>... 
parameterTypes) throws Exception {
+        T mojo = clazz.getDeclaredConstructor(parameterTypes).newInstance();
         setDefaultValues(mojo);
 
         setAttribute(mojo, "project", project);

Reply via email to