You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2024/12/13 19:53:45 UTC

(maven-plugin-tools) branch guice created (now 54b012af)

This is an automated email from the ASF dual-hosted git repository.

elharo pushed a change to branch guice
in repository https://gitbox.apache.org/repos/asf/maven-plugin-tools.git


      at 54b012af Prefer Guice injection

This branch includes the following new commits:

     new 54b012af Prefer Guice injection

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(maven-plugin-tools) 01/01: Prefer Guice injection

Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

elharo pushed a commit to branch guice
in repository https://gitbox.apache.org/repos/asf/maven-plugin-tools.git

commit 54b012af35eddfd5be85de56e2ef72518158740f
Author: Elliotte Rusty Harold <el...@ibiblio.org>
AuthorDate: Fri Dec 13 14:53:36 2024 -0500

    Prefer Guice injection
---
 maven-plugin-plugin/pom.xml                              |  6 ++++++
 .../maven/plugin/plugin/AbstractGeneratorMojo.java       | 15 +++++++++------
 .../maven/plugin/plugin/DescriptorGeneratorMojo.java     | 16 ++++++++++++----
 .../apache/maven/plugin/plugin/HelpGeneratorMojo.java    | 12 +++++++++---
 .../maven/plugin/plugin/HelpGeneratorMojoTest.java       |  3 +--
 5 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/maven-plugin-plugin/pom.xml b/maven-plugin-plugin/pom.xml
index daf2ecff..572d9c9f 100644
--- a/maven-plugin-plugin/pom.xml
+++ b/maven-plugin-plugin/pom.xml
@@ -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>
diff --git a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/AbstractGeneratorMojo.java b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/AbstractGeneratorMojo.java
index d6524aae..3dda7fd2 100644
--- a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/AbstractGeneratorMojo.java
+++ b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/AbstractGeneratorMojo.java
@@ -23,7 +23,6 @@ import java.util.List;
 
 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;
 
@@ -34,11 +33,6 @@ import org.apache.maven.project.MavenProject;
  *
  */
 public abstract class AbstractGeneratorMojo extends AbstractMojo {
-    /**
-     * The project currently being built.
-     */
-    @Component
-    protected MavenProject project;
 
     /**
      * The goal prefix that will appear before the ":".
@@ -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
diff --git a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/DescriptorGeneratorMojo.java b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/DescriptorGeneratorMojo.java
index 73a0e561..b55bf156 100644
--- a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/DescriptorGeneratorMojo.java
+++ b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/DescriptorGeneratorMojo.java
@@ -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;
@@ -43,6 +45,7 @@ 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.DefaultPluginToolsRequest;
 import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
 import org.apache.maven.tools.plugin.ExtendedPluginDescriptor;
@@ -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
+    protected 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())
diff --git a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/HelpGeneratorMojo.java b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/HelpGeneratorMojo.java
index eeadd65e..00a7f3d6 100644
--- a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/HelpGeneratorMojo.java
+++ b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/HelpGeneratorMojo.java
@@ -18,6 +18,7 @@
  */
 package org.apache.maven.plugin.plugin;
 
+import javax.inject.Inject;
 import javax.lang.model.SourceVersion;
 
 import java.io.File;
@@ -25,11 +26,11 @@ 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;
@@ -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;
diff --git a/maven-plugin-plugin/src/test/java/org/apache/maven/plugin/plugin/HelpGeneratorMojoTest.java b/maven-plugin-plugin/src/test/java/org/apache/maven/plugin/plugin/HelpGeneratorMojoTest.java
index 748a4ad2..3473ce35 100644
--- a/maven-plugin-plugin/src/test/java/org/apache/maven/plugin/plugin/HelpGeneratorMojoTest.java
+++ b/maven-plugin-plugin/src/test/java/org/apache/maven/plugin/plugin/HelpGeneratorMojoTest.java
@@ -40,8 +40,7 @@ class HelpGeneratorMojoTest {
     @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);