Skip to content

Commit 73f78dc

Browse files
elharoslawekjaranowski
authored andcommitted
Begin converting this plugin to Guice constructor injection
* Prefer Guice injection (cherry picked from commit c1c0a69)
1 parent 23242f4 commit 73f78dc

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed

maven-plugin-plugin/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@
130130
<artifactId>maven-resolver-util</artifactId>
131131
<version>${resolverVersion}</version>
132132
</dependency>
133+
<dependency>
134+
<groupId>javax.inject</groupId>
135+
<artifactId>javax.inject</artifactId>
136+
<version>1</version>
137+
<scope>provided</scope>
138+
</dependency>
133139

134140
<!-- plexus -->
135141
<dependency>

maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/AbstractGeneratorMojo.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.apache.maven.plugin.AbstractMojo;
2525
import org.apache.maven.plugin.MojoExecutionException;
26-
import org.apache.maven.plugins.annotations.Component;
2726
import org.apache.maven.plugins.annotations.Parameter;
2827
import org.apache.maven.project.MavenProject;
2928

@@ -34,11 +33,6 @@
3433
*
3534
*/
3635
public abstract class AbstractGeneratorMojo extends AbstractMojo {
37-
/**
38-
* The project currently being built.
39-
*/
40-
@Component
41-
protected MavenProject project;
4236

4337
/**
4438
* The goal prefix that will appear before the ":".
@@ -67,6 +61,15 @@ public abstract class AbstractGeneratorMojo extends AbstractMojo {
6761
*/
6862
protected static final String LS = System.lineSeparator();
6963

64+
/**
65+
* The project currently being built.
66+
*/
67+
protected final MavenProject project;
68+
69+
protected AbstractGeneratorMojo(MavenProject project) {
70+
this.project = project;
71+
}
72+
7073
protected abstract void generate() throws MojoExecutionException;
7174

7275
@Override

maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/DescriptorGeneratorMojo.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.apache.maven.plugin.plugin;
2020

21+
import javax.inject.Inject;
22+
2123
import java.io.File;
2224
import java.net.URI;
2325
import java.util.Arrays;
@@ -38,6 +40,7 @@
3840
import org.apache.maven.plugins.annotations.Mojo;
3941
import org.apache.maven.plugins.annotations.Parameter;
4042
import org.apache.maven.plugins.annotations.ResolutionScope;
43+
import org.apache.maven.project.MavenProject;
4144
import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
4245
import org.apache.maven.tools.plugin.ExtendedPluginDescriptor;
4346
import org.apache.maven.tools.plugin.PluginToolsRequest;
@@ -262,11 +265,16 @@ public class DescriptorGeneratorMojo extends AbstractGeneratorMojo {
262265
/**
263266
* The component used for scanning the source tree for mojos.
264267
*/
265-
@Component
266-
private MojoScanner mojoScanner;
268+
private final MojoScanner mojoScanner;
267269

268-
@Component
269-
protected BuildContext buildContext;
270+
protected final BuildContext buildContext;
271+
272+
@Inject
273+
public DescriptorGeneratorMojo(MavenProject project, MojoScanner mojoScanner, BuildContext buildContext) {
274+
super(project);
275+
this.mojoScanner = mojoScanner;
276+
this.buildContext = buildContext;
277+
}
270278

271279
public void generate() throws MojoExecutionException {
272280

maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/HelpGeneratorMojo.java

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

21+
import javax.inject.Inject;
2122
import javax.lang.model.SourceVersion;
2223

2324
import java.io.File;
2425
import java.util.Arrays;
2526
import java.util.stream.Collectors;
2627

2728
import org.apache.maven.plugin.MojoExecutionException;
28-
import org.apache.maven.plugins.annotations.Component;
2929
import org.apache.maven.plugins.annotations.LifecyclePhase;
3030
import org.apache.maven.plugins.annotations.Mojo;
3131
import org.apache.maven.plugins.annotations.Parameter;
3232
import org.apache.maven.plugins.annotations.ResolutionScope;
33+
import org.apache.maven.project.MavenProject;
3334
import org.apache.maven.tools.plugin.generator.GeneratorException;
3435
import org.apache.maven.tools.plugin.generator.PluginHelpGenerator;
3536
import org.codehaus.plexus.util.StringUtils;
@@ -71,8 +72,13 @@ public class HelpGeneratorMojo extends AbstractGeneratorMojo {
7172
/**
7273
* Velocity component.
7374
*/
74-
@Component
75-
private VelocityComponent velocity;
75+
private final VelocityComponent velocity;
76+
77+
@Inject
78+
public HelpGeneratorMojo(MavenProject project, VelocityComponent velocity) {
79+
super(project);
80+
this.velocity = velocity;
81+
}
7682

7783
String getHelpPackageName() {
7884
String packageName = null;

maven-plugin-plugin/src/test/java/org/apache/maven/plugin/plugin/HelpGeneratorMojoTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public static Stream<Arguments> packageNameShouldBeCorrect() {
4040
@ParameterizedTest
4141
@MethodSource
4242
void packageNameShouldBeCorrect(MavenProject project, String expectedPackageName) {
43-
HelpGeneratorMojo mojo = new HelpGeneratorMojo();
44-
mojo.project = project;
43+
HelpGeneratorMojo mojo = new HelpGeneratorMojo(project, null);
4544

4645
String packageName = mojo.getHelpPackageName();
4746
assertEquals(expectedPackageName, packageName);

0 commit comments

Comments
 (0)