Skip to content

Commit 5a2b3db

Browse files
authored
Convert to Guice @Inject (#42)
* Convert to Guice @Inject
1 parent 724409a commit 5a2b3db

File tree

5 files changed

+66
-16
lines changed

5 files changed

+66
-16
lines changed

pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,29 @@ under the License.
120120
<artifactId>maven-jarsigner</artifactId>
121121
<version>3.1.0</version>
122122
</dependency>
123+
<dependency>
124+
<groupId>javax.inject</groupId>
125+
<artifactId>javax.inject</artifactId>
126+
<version>1</version>
127+
<scope>provided</scope>
128+
</dependency>
123129
<dependency>
124130
<groupId>junit</groupId>
125131
<artifactId>junit</artifactId>
126132
<version>4.13.2</version>
127133
<scope>test</scope>
128134
</dependency>
135+
<dependency>
136+
<groupId>org.slf4j</groupId>
137+
<artifactId>slf4j-api</artifactId>
138+
<version>1.7.36</version>
139+
<scope>test</scope>
140+
</dependency>
129141
<!-- Used for test cases to perform simple logging without any SLF4J warning -->
130142
<dependency>
131143
<groupId>org.slf4j</groupId>
132144
<artifactId>slf4j-simple</artifactId>
133-
<version>2.0.9</version>
145+
<version>1.7.36</version>
134146
<scope>test</scope>
135147
</dependency>
136148
<dependency>

src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.apache.maven.execution.MavenSession;
3434
import org.apache.maven.plugin.AbstractMojo;
3535
import org.apache.maven.plugin.MojoExecutionException;
36-
import org.apache.maven.plugins.annotations.Component;
3736
import org.apache.maven.plugins.annotations.Parameter;
3837
import org.apache.maven.project.MavenProject;
3938
import org.apache.maven.settings.Settings;
@@ -237,11 +236,6 @@ public abstract class AbstractJarsignerMojo extends AbstractMojo {
237236
@Parameter(defaultValue = "${project.basedir}")
238237
private File workingDirectory;
239238

240-
/**
241-
*/
242-
@Component
243-
private JarSigner jarSigner;
244-
245239
/**
246240
* The current build session instance. This is used for
247241
* toolchain manager API calls.
@@ -251,19 +245,26 @@ public abstract class AbstractJarsignerMojo extends AbstractMojo {
251245
@Parameter(defaultValue = "${session}", readonly = true, required = true)
252246
private MavenSession session;
253247

248+
private final JarSigner jarSigner;
249+
254250
/**
255251
* To obtain a toolchain if possible.
256252
*
257253
* @since 1.3
258254
*/
259-
@Component
260-
private ToolchainManager toolchainManager;
255+
private final ToolchainManager toolchainManager;
261256

262257
/**
263258
* @since 1.3.2
264259
*/
265-
@Component(hint = "mng-4384")
266-
private SecDispatcher securityDispatcher;
260+
private final SecDispatcher securityDispatcher;
261+
262+
protected AbstractJarsignerMojo(
263+
JarSigner jarSigner, ToolchainManager toolchainManager, SecDispatcher securityDispatcher) {
264+
this.jarSigner = jarSigner;
265+
this.toolchainManager = toolchainManager;
266+
this.securityDispatcher = securityDispatcher;
267+
}
267268

268269
@Override
269270
public final void execute() throws MojoExecutionException {

src/main/java/org/apache/maven/plugins/jarsigner/JarsignerSignMojo.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package org.apache.maven.plugins.jarsigner;
2020

21+
import javax.inject.Inject;
22+
import javax.inject.Named;
23+
2124
import java.io.File;
2225
import java.io.IOException;
2326
import java.time.Duration;
@@ -42,6 +45,8 @@
4245
import org.apache.maven.shared.utils.cli.Commandline;
4346
import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
4447
import org.apache.maven.shared.utils.cli.javatool.JavaToolResult;
48+
import org.apache.maven.toolchain.ToolchainManager;
49+
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
4550

4651
/**
4752
* Signs a project artifact and attachments using jarsigner.
@@ -225,6 +230,19 @@ public class JarsignerSignMojo extends AbstractJarsignerMojo {
225230
/** Exponent limit for exponential wait after failure function. 2^20 = 1048576 sec ~= 12 days. */
226231
private static final int MAX_WAIT_EXPONENT_ATTEMPT = 20;
227232

233+
@Inject
234+
public JarsignerSignMojo(
235+
JarSigner jarSigner,
236+
ToolchainManager toolchainManager,
237+
@Named("mng-4384") SecDispatcher securityDispatcher) {
238+
super(jarSigner, toolchainManager, securityDispatcher);
239+
}
240+
241+
// for testing; invoked via reflection
242+
JarsignerSignMojo() {
243+
super(null, null, null);
244+
}
245+
228246
@Override
229247
protected String getCommandlineInfo(final Commandline commandLine) {
230248
String commandLineInfo = commandLine != null ? commandLine.toString() : null;

src/main/java/org/apache/maven/plugins/jarsigner/JarsignerVerifyMojo.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package org.apache.maven.plugins.jarsigner;
2020

21+
import javax.inject.Inject;
22+
import javax.inject.Named;
23+
2124
import java.io.File;
2225
import java.io.IOException;
2326

@@ -31,6 +34,8 @@
3134
import org.apache.maven.shared.jarsigner.JarSignerVerifyRequest;
3235
import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
3336
import org.apache.maven.shared.utils.cli.javatool.JavaToolResult;
37+
import org.apache.maven.toolchain.ToolchainManager;
38+
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
3439

3540
/**
3641
* Checks the signatures of a project artifact and attachments using jarsigner.
@@ -49,15 +54,28 @@ public class JarsignerVerifyMojo extends AbstractJarsignerMojo {
4954

5055
/**
5156
* When <code>true</code> this will make the execute() operation fail,
52-
* throwing an exception, when verifying a non signed jar.
57+
* throwing an exception, when verifying an unsigned jar.
5358
* Primarily to keep backwards compatibility with existing code, and allow reusing the
54-
* bean in unattended operations when set to <code>false</code>.
59+
* mojo in unattended operations when set to <code>false</code>.
5560
*
5661
* @since 1.3
5762
**/
5863
@Parameter(property = "jarsigner.errorWhenNotSigned", defaultValue = "false")
5964
private boolean errorWhenNotSigned;
6065

66+
@Inject
67+
public JarsignerVerifyMojo(
68+
JarSigner jarSigner,
69+
ToolchainManager toolchainManager,
70+
@Named("mng-4384") SecDispatcher securityDispatcher) {
71+
super(jarSigner, toolchainManager, securityDispatcher);
72+
}
73+
74+
// for testing; invoked via reflection
75+
JarsignerVerifyMojo() {
76+
super(null, null, null);
77+
}
78+
6179
/**
6280
* {@inheritDoc}
6381
*/

src/test/java/org/apache/maven/plugins/jarsigner/MojoTestCreator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ public void setLog(Log log) {
8585

8686
/**
8787
* Creates and configures the Mojo instance.
88-
* @param configuration user supplied configuration.
88+
*
89+
* @param configuration user supplied configuration
8990
*/
90-
public T configure(Map<String, String> configuration) throws Exception {
91-
T mojo = clazz.getDeclaredConstructor().newInstance();
91+
public T configure(Map<String, String> configuration, Class<?>... parameterTypes) throws Exception {
92+
T mojo = clazz.getDeclaredConstructor(parameterTypes).newInstance();
9293
setDefaultValues(mojo);
9394

9495
setAttribute(mojo, "project", project);

0 commit comments

Comments
 (0)