Skip to content

Commit

Permalink
[MDEP-966] Convert remaining Mojos to Guice injection (#500)
Browse files Browse the repository at this point in the history
* Convert Mojos to Guice constructor injection
  • Loading branch information
elharo authored Jan 4, 2025
1 parent 89744d0 commit 8b74a28
Show file tree
Hide file tree
Showing 21 changed files with 322 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.dependency.utils.DependencySilentLog;
import org.apache.maven.project.DefaultProjectBuildingRequest;
Expand All @@ -39,27 +38,6 @@
*/
public abstract class AbstractDependencyMojo extends AbstractMojo {

/**
* For IDE build support
*/
@Component
private BuildContext buildContext;

/**
* Skip plugin execution only during incremental builds (e.g. triggered from M2E).
*
* @since 3.4.0
* @see #skip
*/
@Parameter(defaultValue = "false")
private boolean skipDuringIncrementalBuild;

/**
* POM
*/
@Component
private MavenProject project;

/**
* Remote repositories which will be searched for artifacts.
*/
Expand All @@ -81,8 +59,7 @@ public abstract class AbstractDependencyMojo extends AbstractMojo {
/**
* The Maven session
*/
@Component
protected MavenSession session;
protected final MavenSession session;

/**
* If the plugin should be silent.
Expand All @@ -102,6 +79,31 @@ public abstract class AbstractDependencyMojo extends AbstractMojo {
@Parameter(property = "mdep.skip", defaultValue = "false")
private boolean skip;

/**
* Skip plugin execution only during incremental builds (e.g. triggered from M2E).
*
* @since 3.4.0
* @see #skip
*/
@Parameter(defaultValue = "false")
private boolean skipDuringIncrementalBuild;

/**
* For IDE build support
*/
private final BuildContext buildContext;

/**
* POM
*/
private final MavenProject project;

protected AbstractDependencyMojo(MavenSession session, BuildContext buildContext, MavenProject project) {
this.session = session;
this.buildContext = buildContext;
this.project = project;
}

// Mojo methods -----------------------------------------------------------

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
import java.util.stream.Collectors;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.collection.CollectRequest;
Expand All @@ -43,6 +45,7 @@
import org.eclipse.aether.graph.DependencyVisitor;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.util.graph.visitor.TreeDependencyVisitor;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
* Goal that collects all project dependencies and then lists the repositories used by the build and by the transitive
Expand All @@ -57,14 +60,16 @@ public class ListRepositoriesMojo extends AbstractDependencyMojo {
private final RepositorySystem repositorySystem;

@Inject
public ListRepositoriesMojo(RepositorySystem repositorySystem) {
public ListRepositoriesMojo(
MavenSession session, BuildContext buildContext, MavenProject project, RepositorySystem repositorySystem) {
super(session, buildContext, project);
this.repositorySystem = repositorySystem;
}

/**
* Displays a list of the repositories used by this build.
*
* @throws MojoExecutionException with a message if an error occurs.
* @throws MojoExecutionException with a message if an error occurs
*/
@Override
protected void doExecute() throws MojoExecutionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.dependency.AbstractDependencyMojo;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
Expand All @@ -42,6 +42,7 @@
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
* Abstract parent class used by mojos that get Artifact information from the plugin configuration as an ArrayList of
Expand Down Expand Up @@ -111,14 +112,24 @@ public abstract class AbstractFromConfigurationMojo extends AbstractDependencyMo
@Parameter
private File localRepositoryDirectory;

@Component
private ArtifactResolver artifactResolver;
private final ArtifactResolver artifactResolver;

@Component
private RepositoryManager repositoryManager;
private final RepositoryManager repositoryManager;

@Component
private ArtifactHandlerManager artifactHandlerManager;
private final ArtifactHandlerManager artifactHandlerManager;

protected AbstractFromConfigurationMojo(
MavenSession session,
BuildContext buildContext,
MavenProject project,
ArtifactResolver artifactResolver,
RepositoryManager repositoryManager,
ArtifactHandlerManager artifactHandlerManager) {
super(session, buildContext, project);
this.artifactResolver = artifactResolver;
this.repositoryManager = repositoryManager;
this.artifactHandlerManager = artifactHandlerManager;
}

abstract ArtifactItemFilter getMarkedArtifactFilter(ArtifactItem item);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -33,6 +35,10 @@
import org.apache.maven.plugins.dependency.utils.CopyUtil;
import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
* Goal that copies a list of artifacts from the repository to defined locations.
Expand Down Expand Up @@ -82,7 +88,15 @@ public class CopyMojo extends AbstractFromConfigurationMojo {
private String artifact;

@Inject
public CopyMojo(CopyUtil copyUtil) {
public CopyMojo(
MavenSession session,
BuildContext buildContext,
MavenProject project,
ArtifactResolver artifactResolver,
RepositoryManager repositoryManager,
ArtifactHandlerManager artifactHandlerManager,
CopyUtil copyUtil) {
super(session, buildContext, project, artifactResolver, repositoryManager, artifactHandlerManager);
this.copyUtil = copyUtil;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.io.File;
import java.util.List;

import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -33,7 +35,11 @@
import org.apache.maven.plugins.dependency.utils.filters.MarkerFileFilter;
import org.apache.maven.plugins.dependency.utils.markers.MarkerHandler;
import org.apache.maven.plugins.dependency.utils.markers.UnpackFileMarkerHandler;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.codehaus.plexus.components.io.filemappers.FileMapper;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
* Goal that retrieves a list of artifacts from the repository and unpacks them in a defined location.
Expand Down Expand Up @@ -98,7 +104,15 @@ public class UnpackMojo extends AbstractFromConfigurationMojo {
private String artifact;

@Inject
public UnpackMojo(UnpackUtil unpackUtil) {
public UnpackMojo(
MavenSession session,
BuildContext buildContext,
MavenProject project,
ArtifactResolver artifactResolver,
RepositoryManager repositoryManager,
ArtifactHandlerManager artifactHandlerManager,
UnpackUtil unpackUtil) {
super(session, buildContext, project, artifactResolver, repositoryManager, artifactHandlerManager);
this.unpackUtil = unpackUtil;
}

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

import javax.inject.Inject;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -27,6 +29,7 @@
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.dependency.AbstractDependencyMojo;
Expand All @@ -50,6 +53,7 @@
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
* Class that encapsulates the plugin parameters, and contains methods that handle dependency filtering
Expand Down Expand Up @@ -239,23 +243,30 @@ public abstract class AbstractDependencyFilterMojo extends AbstractDependencyMoj

private final ArtifactHandlerManager artifactHandlerManager;

@Inject
// CHECKSTYLE_OFF: ParameterNumber
protected AbstractDependencyFilterMojo(
MavenSession session,
BuildContext buildContext,
MavenProject project,
ResolverUtil resolverUtil,
DependencyResolver dependencyResolver,
RepositoryManager repositoryManager,
ProjectBuilder projectBuilder,
ArtifactHandlerManager artifactHandlerManager) {
super(session, buildContext, project);
this.resolverUtil = resolverUtil;
this.dependencyResolver = dependencyResolver;
this.repositoryManager = repositoryManager;
this.projectBuilder = projectBuilder;
this.artifactHandlerManager = artifactHandlerManager;
}
// CHECKSTYLE_ON: ParameterNumber

/**
* Return an {@link ArtifactsFilter} indicating which artifacts must be filtered out.
*
* @return an {@link ArtifactsFilter} indicating which artifacts must be filtered out.
* @return an {@link ArtifactsFilter} indicating which artifacts must be filtered out
*/
protected abstract ArtifactsFilter getMarkedArtifactFilter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import java.io.File;

import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.dependency.utils.ResolverUtil;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
* Abstract Parent class used by mojos that get Artifact information from the project dependencies.
Expand Down Expand Up @@ -111,17 +114,30 @@ public abstract class AbstractFromDependenciesMojo extends AbstractDependencyFil
@Parameter(property = "mdep.failOnMissingClassifierArtifact", defaultValue = "false")
protected boolean failOnMissingClassifierArtifact;

// CHECKSTYLE_OFF: ParameterNumber
protected AbstractFromDependenciesMojo(
MavenSession session,
BuildContext buildContext,
MavenProject project,
ResolverUtil resolverUtil,
DependencyResolver dependencyResolver,
RepositoryManager repositoryManager,
ProjectBuilder projectBuilder,
ArtifactHandlerManager artifactHandlerManager) {
super(resolverUtil, dependencyResolver, repositoryManager, projectBuilder, artifactHandlerManager);
super(
session,
buildContext,
project,
resolverUtil,
dependencyResolver,
repositoryManager,
projectBuilder,
artifactHandlerManager);
}
// CHECKSTYLE_ON: ParameterNumber

/**
* @return returns the outputDirectory
* @return returns the output directory
*/
public File getOutputDirectory() {
return this.outputDirectory;
Expand Down
Loading

0 comments on commit 8b74a28

Please sign in to comment.