Skip to content

Commit

Permalink
Convert Mojos to Guice constructor injection
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Dec 30, 2024
1 parent 89744d0 commit 17019be
Show file tree
Hide file tree
Showing 21 changed files with 318 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,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 Down Expand Up @@ -102,6 +81,32 @@ public abstract class AbstractDependencyMojo extends AbstractMojo {
@Parameter(property = "mdep.skip", defaultValue = "false")
private boolean skip;

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

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

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

protected AbstractDependencyMojo(
BuildContext buildContext, boolean skipDuringIncrementalBuild, MavenProject project) {
this.buildContext = buildContext;
this.skipDuringIncrementalBuild = skipDuringIncrementalBuild;
this.project = project;
}

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

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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 +44,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,7 +59,12 @@ public class ListRepositoriesMojo extends AbstractDependencyMojo {
private final RepositorySystem repositorySystem;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
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 +41,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 +111,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(
BuildContext buildContext,
boolean skipDuringIncrementalBuild,
MavenProject project,
ArtifactResolver artifactResolver,
RepositoryManager repositoryManager,
ArtifactHandlerManager artifactHandlerManager) {
super(buildContext, skipDuringIncrementalBuild, 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 @@ -18,13 +18,12 @@
*/
package org.apache.maven.plugins.dependency.fromConfiguration;

import javax.inject.Inject;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -33,6 +32,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 @@ -81,8 +84,21 @@ public class CopyMojo extends AbstractFromConfigurationMojo {
@Parameter(property = "artifact")
private String artifact;

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

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

import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -33,7 +34,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 +103,21 @@ public class UnpackMojo extends AbstractFromConfigurationMojo {
private String artifact;

@Inject
public UnpackMojo(UnpackUtil unpackUtil) {
public UnpackMojo(
BuildContext buildContext,
boolean skipDuringIncrementalBuild,
MavenProject project,
ArtifactResolver artifactResolver,
RepositoryManager repositoryManager,
ArtifactHandlerManager artifactHandlerManager,
UnpackUtil unpackUtil) {
super(
buildContext,
skipDuringIncrementalBuild,
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 Down Expand Up @@ -50,6 +52,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 +242,30 @@ public abstract class AbstractDependencyFilterMojo extends AbstractDependencyMoj

private final ArtifactHandlerManager artifactHandlerManager;

@Inject
// CHECKSTYLE_OFF: ParameterNumber
protected AbstractDependencyFilterMojo(
BuildContext buildContext,
boolean skipDuringIncrementalBuild,
MavenProject project,
ResolverUtil resolverUtil,
DependencyResolver dependencyResolver,
RepositoryManager repositoryManager,
ProjectBuilder projectBuilder,
ArtifactHandlerManager artifactHandlerManager) {
super(buildContext, skipDuringIncrementalBuild, 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 @@ -23,9 +23,11 @@
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
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,14 +113,27 @@ public abstract class AbstractFromDependenciesMojo extends AbstractDependencyFil
@Parameter(property = "mdep.failOnMissingClassifierArtifact", defaultValue = "false")
protected boolean failOnMissingClassifierArtifact;

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

/**
* @return returns the outputDirectory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,26 @@
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
import org.apache.maven.plugins.dependency.utils.ResolverUtil;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
* This goal outputs a classpath string of dependencies from the local repository to a file or log.
*
* @author ankostis
* @since 2.0-alpha-2
*/
// CHECKSTYLE_OFF: LineLength
@Mojo(
name = "build-classpath",
requiresDependencyResolution = ResolutionScope.TEST,
defaultPhase = LifecyclePhase.GENERATE_SOURCES,
threadSafe = true)
// CHECKSTYLE_ON: LineLength
public class BuildClasspathMojo extends AbstractDependencyFilterMojo implements Comparator<Artifact> {

@Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}")
Expand Down Expand Up @@ -167,16 +167,29 @@ public class BuildClasspathMojo extends AbstractDependencyFilterMojo implements
private final MavenProjectHelper projectHelper;

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

/**
* Main entry into mojo. Gets the list of dependencies and iterates to create a classpath.
Expand Down
Loading

0 comments on commit 17019be

Please sign in to comment.