Skip to content

Commit

Permalink
Convert to Guice @Inject (#42)
Browse files Browse the repository at this point in the history
* Convert to Guice @Inject
  • Loading branch information
elharo authored Dec 11, 2024
1 parent 724409a commit 5a2b3db
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 16 deletions.
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand All @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);
Expand Down

0 comments on commit 5a2b3db

Please sign in to comment.