Skip to content

Commit

Permalink
Begin converting this plugin to Guice constructor injection (#346)
Browse files Browse the repository at this point in the history
* Prefer Guice injection
  • Loading branch information
elharo authored Dec 15, 2024
1 parent 301bc33 commit c1c0a69
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
6 changes: 6 additions & 0 deletions maven-plugin-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@
<artifactId>maven-resolver-util</artifactId>
<version>${resolverVersion}</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>

<!-- plexus -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

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;

Expand All @@ -34,11 +33,6 @@
*
*/
public abstract class AbstractGeneratorMojo extends AbstractMojo {
/**
* The project currently being built.
*/
@Component
protected MavenProject project;

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

/**
* The project currently being built.
*/
protected final MavenProject project;

protected AbstractGeneratorMojo(MavenProject project) {
this.project = project;
}

protected abstract void generate() throws MojoExecutionException;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.maven.plugin.plugin;

import javax.inject.Inject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -43,6 +45,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
import org.apache.maven.tools.plugin.ExtendedPluginDescriptor;
Expand Down Expand Up @@ -280,11 +283,16 @@ public class DescriptorGeneratorMojo extends AbstractGeneratorMojo {
/**
* The component used for scanning the source tree for mojos.
*/
@Component
private MojoScanner mojoScanner;
private final MojoScanner mojoScanner;

@Component
protected BuildContext buildContext;
protected final BuildContext buildContext;

@Inject
public DescriptorGeneratorMojo(MavenProject project, MojoScanner mojoScanner, BuildContext buildContext) {
super(project);
this.mojoScanner = mojoScanner;
this.buildContext = buildContext;
}

public void generate() throws MojoExecutionException {
if (!"maven-plugin".equalsIgnoreCase(project.getArtifactId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
*/
package org.apache.maven.plugin.plugin;

import javax.inject.Inject;
import javax.lang.model.SourceVersion;

import java.io.File;
import java.util.Arrays;
import java.util.stream.Collectors;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.tools.plugin.generator.GeneratorException;
import org.apache.maven.tools.plugin.generator.PluginHelpGenerator;
import org.codehaus.plexus.util.StringUtils;
Expand Down Expand Up @@ -71,8 +72,13 @@ public class HelpGeneratorMojo extends AbstractGeneratorMojo {
/**
* Velocity component.
*/
@Component
private VelocityComponent velocity;
private final VelocityComponent velocity;

@Inject
public HelpGeneratorMojo(MavenProject project, VelocityComponent velocity) {
super(project);
this.velocity = velocity;
}

String getHelpPackageName() {
String packageName = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public static Stream<Arguments> packageNameShouldBeCorrect() {
@ParameterizedTest
@MethodSource
void packageNameShouldBeCorrect(MavenProject project, String expectedPackageName) {
HelpGeneratorMojo mojo = new HelpGeneratorMojo();
mojo.project = project;
HelpGeneratorMojo mojo = new HelpGeneratorMojo(project, null);

String packageName = mojo.getHelpPackageName();
assertEquals(expectedPackageName, packageName);
Expand Down

0 comments on commit c1c0a69

Please sign in to comment.