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-artifact-plugin.git
commit 7f053e52763e0845a982d628740adaefcf9cc75e Author: Elliotte Rusty Harold <elh...@ibiblio.org> AuthorDate: Thu Dec 12 18:16:44 2024 -0500 Prefer Guice constrcutor injection --- .../artifact/buildinfo/AbstractBuildinfoMojo.java | 39 +++++++++++++--------- .../plugins/artifact/buildinfo/BuildinfoMojo.java | 21 ++++++++++-- .../artifact/buildinfo/CheckBuildPlanMojo.java | 19 +++++++---- .../plugins/artifact/buildinfo/CompareMojo.java | 25 ++++++++++---- .../buildinfo/DescribeBuildOutputMojo.java | 16 ++++++++- 5 files changed, 86 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java index 76f8678..952e5f5 100644 --- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java +++ b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java @@ -43,7 +43,6 @@ import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; -import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import org.apache.maven.rtinfo.RuntimeInformation; @@ -57,11 +56,6 @@ import org.eclipse.aether.artifact.Artifact; * @since 3.2.0 */ public abstract class AbstractBuildinfoMojo extends AbstractMojo { - /** - * The Maven project. - */ - @Component - protected MavenProject project; /** * Location of the generated buildinfo file. @@ -109,12 +103,6 @@ public abstract class AbstractBuildinfoMojo extends AbstractMojo { @Parameter(property = "buildinfo.reproducible", defaultValue = "false") private boolean reproducible; - /** - * The current build session instance. This is used for toolchain manager API calls. - */ - @Component - protected MavenSession session; - /** * Timestamp for reproducible output archive entries, either formatted as ISO 8601 * <code>yyyy-MM-dd'T'HH:mm:ssXXX</code> or as an int representing seconds since the epoch (like @@ -136,11 +124,30 @@ public abstract class AbstractBuildinfoMojo extends AbstractMojo { /** * To obtain a toolchain if possible. */ - @Component - private ToolchainManager toolchainManager; + private final ToolchainManager toolchainManager; + + protected final RuntimeInformation rtInformation; + + /** + * The Maven project. + */ + protected final MavenProject project; - @Component - protected RuntimeInformation rtInformation; + /** + * The current build session instance. This is used for toolchain manager API calls. + */ + protected final MavenSession session; + + protected AbstractBuildinfoMojo( + ToolchainManager toolchainManager, + RuntimeInformation rtInformation, + MavenProject project, + MavenSession session) { + this.toolchainManager = toolchainManager; + this.rtInformation = rtInformation; + this.project = project; + this.session = session; + } @Override public void execute() throws MojoExecutionException { diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildinfoMojo.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildinfoMojo.java index 38a4b4e..8ceb512 100644 --- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildinfoMojo.java +++ b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildinfoMojo.java @@ -18,14 +18,19 @@ */ package org.apache.maven.plugins.artifact.buildinfo; +import javax.inject.Inject; + import java.util.Map; +import org.apache.maven.execution.MavenSession; 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.project.MavenProject; import org.apache.maven.project.MavenProjectHelper; +import org.apache.maven.rtinfo.RuntimeInformation; +import org.apache.maven.toolchain.ToolchainManager; import org.eclipse.aether.artifact.Artifact; /** @@ -44,8 +49,18 @@ public class BuildinfoMojo extends AbstractBuildinfoMojo { /** * Used for attaching the buildinfo file in the project. */ - @Component - private MavenProjectHelper projectHelper; + private final MavenProjectHelper projectHelper; + + @Inject + public BuildinfoMojo( + ToolchainManager toolchainManager, + RuntimeInformation runtimeInformation, + MavenProject project, + MavenSession session, + MavenProjectHelper projectHelper) { + super(toolchainManager, runtimeInformation, project, session); + this.projectHelper = projectHelper; + } @Override public void execute(Map<Artifact, String> artifacts) throws MojoExecutionException { diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CheckBuildPlanMojo.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CheckBuildPlanMojo.java index 637fd12..00a2fab 100644 --- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CheckBuildPlanMojo.java +++ b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CheckBuildPlanMojo.java @@ -18,6 +18,8 @@ */ package org.apache.maven.plugins.artifact.buildinfo; +import javax.inject.Inject; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -34,7 +36,6 @@ import org.apache.maven.model.Plugin; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; @@ -50,14 +51,11 @@ import org.eclipse.aether.version.VersionScheme; */ @Mojo(name = "check-buildplan", threadSafe = true, requiresProject = true) public class CheckBuildPlanMojo extends AbstractMojo { - @Component - private MavenProject project; + private final MavenProject project; - @Component - private MavenSession session; + private final MavenSession session; - @Component - private LifecycleExecutor lifecycleExecutor; + private final LifecycleExecutor lifecycleExecutor; /** Allow to specify which goals/phases will be used to calculate execution plan. */ @Parameter(property = "check.buildplan.tasks", defaultValue = "deploy") @@ -93,6 +91,13 @@ public class CheckBuildPlanMojo extends AbstractMojo { private final VersionScheme versionScheme = new GenericVersionScheme(); + @Inject + public CheckBuildPlanMojo(MavenProject project, MavenSession session, LifecycleExecutor lifecycleExecutor) { + this.project = project; + this.session = session; + this.lifecycleExecutor = lifecycleExecutor; + } + protected MavenExecutionPlan calculateExecutionPlan() throws MojoExecutionException { try { return lifecycleExecutor.calculateExecutionPlan(session, tasks); diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CompareMojo.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CompareMojo.java index 9cfd262..08d64d9 100644 --- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CompareMojo.java +++ b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CompareMojo.java @@ -31,12 +31,14 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; +import org.apache.maven.rtinfo.RuntimeInformation; import org.apache.maven.shared.utils.logging.MessageUtils; +import org.apache.maven.toolchain.ToolchainManager; import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; @@ -74,12 +76,6 @@ public class CompareMojo extends AbstractBuildinfoMojo { @Parameter(property = "compare.aggregate.only", defaultValue = "false") private boolean aggregateOnly; - /** - * The entry point to Maven Artifact Resolver, i.e. the component doing all the work. - */ - @Component - private RepositorySystem repoSystem; - /** * The current repository/network configuration of Maven. */ @@ -99,6 +95,21 @@ public class CompareMojo extends AbstractBuildinfoMojo { @Parameter(property = "compare.fail", defaultValue = "true") private boolean fail; + /** + * The entry point to Maven Artifact Resolver, i.e. the component doing all the work. + */ + private final RepositorySystem repoSystem; + + protected CompareMojo( + ToolchainManager toolchainManager, + RuntimeInformation rtInformation, + MavenProject project, + MavenSession session, + RepositorySystem repoSystem) { + super(toolchainManager, rtInformation, project, session); + this.repoSystem = repoSystem; + } + @Override public void execute(Map<Artifact, String> artifacts) throws MojoExecutionException { getLog().info("Checking against reference build from " + referenceRepo + "..."); diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/DescribeBuildOutputMojo.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/DescribeBuildOutputMojo.java index 88f6681..ae7362f 100644 --- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/DescribeBuildOutputMojo.java +++ b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/DescribeBuildOutputMojo.java @@ -18,6 +18,8 @@ */ package org.apache.maven.plugins.artifact.buildinfo; +import javax.inject.Inject; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -35,22 +37,34 @@ import java.util.stream.Collectors; import org.apache.commons.codec.digest.DigestUtils; import org.apache.maven.RepositoryUtils; import org.apache.maven.archiver.MavenArchiver; +import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.project.MavenProject; +import org.apache.maven.rtinfo.RuntimeInformation; import org.apache.maven.shared.utils.logging.MessageUtils; +import org.apache.maven.toolchain.ToolchainManager; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.DefaultArtifact; /** * Describe build output (experimental). - * It is expected to be used aggregator used from CLI, ie run at root after everything has run, but not bound to any build + * It is expected to be used aggregator used from CLI; that is, run at root after everything has run, but not bound to any build * phase, where it would be run at root before modules. * @since 3.5.2 */ @Mojo(name = "describe-build-output", aggregator = true, threadSafe = true) public class DescribeBuildOutputMojo extends AbstractBuildinfoMojo { + @Inject + public DescribeBuildOutputMojo( + ToolchainManager toolchainManager, + RuntimeInformation rtInformation, + MavenProject project, + MavenSession session) { + super(toolchainManager, rtInformation, project, session); + } + @Override public void execute() throws MojoExecutionException { // super.execute(); // do not generate buildinfo, just reuse logic from abstract class