From 5a2b3db2ba49916914dd9bdb207cc61363c4ceb7 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 11 Dec 2024 13:06:10 +0000 Subject: [PATCH] Convert to Guice @Inject (#42) * Convert to Guice @Inject --- pom.xml | 14 +++++++++++- .../jarsigner/AbstractJarsignerMojo.java | 21 +++++++++--------- .../plugins/jarsigner/JarsignerSignMojo.java | 18 +++++++++++++++ .../jarsigner/JarsignerVerifyMojo.java | 22 +++++++++++++++++-- .../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. maven-jarsigner 3.1.0 + + javax.inject + javax.inject + 1 + provided + junit junit 4.13.2 test + + org.slf4j + slf4j-api + 1.7.36 + test + org.slf4j slf4j-simple - 2.0.9 + 1.7.36 test 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.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.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.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 true 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 false. + * mojo in unattended operations when set to false. * * @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 void setLog(Log log) { /** * Creates and configures the Mojo instance. - * @param configuration user supplied configuration. + * + * @param configuration user supplied configuration */ - public T configure(Map configuration) throws Exception { - T mojo = clazz.getDeclaredConstructor().newInstance(); + public T configure(Map configuration, Class... parameterTypes) throws Exception { + T mojo = clazz.getDeclaredConstructor(parameterTypes).newInstance(); setDefaultValues(mojo); setAttribute(mojo, "project", project);