Skip to content

Commit

Permalink
Prefer Guice injection
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Dec 26, 2024
1 parent 5901ecd commit b5f78af
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 53 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,6 @@ under the License.
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.plexus</artifactId>
<scope>provided</scope>
</dependency>

<!-- doxia -->
<dependency>
<groupId>org.apache.maven.doxia</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.plugin.MojoExecutionException;
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.DependencyStatusSets;
Expand Down Expand Up @@ -60,15 +59,6 @@
*/
public abstract class AbstractDependencyFilterMojo extends AbstractDependencyMojo {

@Component
private ResolverUtil resolverUtil;

@Component
private DependencyResolver dependencyResolver;

@Component
private RepositoryManager repositoryManager;

/**
* Overwrite release artifacts
*
Expand Down Expand Up @@ -239,11 +229,28 @@ public abstract class AbstractDependencyFilterMojo extends AbstractDependencyMoj
@Parameter(property = "mdep.prependGroupId", defaultValue = "false")
protected boolean prependGroupId = false;

@Component
private ProjectBuilder projectBuilder;
private final ResolverUtil resolverUtil;

private final DependencyResolver dependencyResolver;

@Component
private ArtifactHandlerManager artifactHandlerManager;
private final RepositoryManager repositoryManager;

private final ProjectBuilder projectBuilder;

private final ArtifactHandlerManager artifactHandlerManager;

protected AbstractDependencyFilterMojo(
ResolverUtil resolverUtil,
DependencyResolver dependencyResolver,
RepositoryManager repositoryManager,
ProjectBuilder projectBuilder,
ArtifactHandlerManager artifactHandlerManager) {
this.resolverUtil = resolverUtil;
this.dependencyResolver = dependencyResolver;
this.repositoryManager = repositoryManager;
this.projectBuilder = projectBuilder;
this.artifactHandlerManager = artifactHandlerManager;
}

/**
* Return an {@link ArtifactsFilter} indicating which artifacts must be filtered out.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

import java.io.File;

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.ProjectBuilder;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
import org.apache.maven.shared.transfer.repository.RepositoryManager;

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

protected AbstractFromDependenciesMojo(
ResolverUtil resolverUtil,
DependencyResolver dependencyResolver,
RepositoryManager repositoryManager,
ProjectBuilder projectBuilder,
ArtifactHandlerManager artifactHandlerManager) {
super(resolverUtil, dependencyResolver, repositoryManager, projectBuilder, artifactHandlerManager);
}

/**
* @return Returns the outputDirectory.
* @return returns the outputDirectory
*/
public File getOutputDirectory() {
return this.outputDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.plugin.MojoExecutionException;
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.plugins.dependency.utils.DependencyUtil;
import org.apache.maven.plugins.dependency.utils.ResolverUtil;
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;

/**
Expand Down Expand Up @@ -160,17 +164,18 @@ public class BuildClasspathMojo extends AbstractDependencyFilterMojo implements
@Parameter(property = "mdep.useBaseVersion", defaultValue = "true")
private boolean useBaseVersion = true;

/**
* Maven ProjectHelper
*/
private MavenProjectHelper projectHelper;

private RepositoryManager repositoryManager;
private final MavenProjectHelper projectHelper;

@Inject
public BuildClasspathMojo(MavenProjectHelper projectHelper, RepositoryManager repositoryManager) {
protected BuildClasspathMojo(
MavenProjectHelper projectHelper,
ResolverUtil resolverUtil,
DependencyResolver dependencyResolver,
RepositoryManager repositoryManager,
ProjectBuilder projectBuilder,
ArtifactHandlerManager artifactHandlerManager) {
super(resolverUtil, dependencyResolver, repositoryManager, projectBuilder, artifactHandlerManager);
this.projectHelper = projectHelper;
this.repositoryManager = repositoryManager;
}

/**
Expand Down Expand Up @@ -248,8 +253,8 @@ protected void doExecute() throws MojoExecutionException {
}

/**
* @param cpString The classpath.
* @throws MojoExecutionException in case of an error.
* @param cpString the classpath
* @throws MojoExecutionException in case of an error
*/
protected void attachFile(String cpString) throws MojoExecutionException {
File attachedFile = new File(getProject().getBuild().getDirectory(), "classpath");
Expand All @@ -259,7 +264,7 @@ protected void attachFile(String cpString) throws MojoExecutionException {
}

/**
* Appends the artifact path into the specified StringBuilder.
* Appends the artifact path to the specified StringBuilder.
*
* @param art {@link Artifact}
* @param sb {@link StringBuilder}
Expand All @@ -270,7 +275,7 @@ protected void appendArtifactPath(Artifact art, StringBuilder sb) {
// substitute the property for the local repo path to make the classpath file portable.
if (localRepoProperty != null && !localRepoProperty.isEmpty()) {
ProjectBuildingRequest projectBuildingRequest = session.getProjectBuildingRequest();
File localBasedir = repositoryManager.getLocalRepositoryBasedir(projectBuildingRequest);
File localBasedir = getRepositoryManager().getLocalRepositoryBasedir(projectBuildingRequest);

file = StringUtils.replace(file, localBasedir.getAbsolutePath(), localRepoProperty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,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.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand All @@ -37,11 +38,15 @@
import org.apache.maven.plugins.dependency.utils.CopyUtil;
import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
import org.apache.maven.plugins.dependency.utils.ResolverUtil;
import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
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.artifact.install.ArtifactInstaller;
import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
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.eclipse.aether.util.artifact.SubArtifact;

Expand Down Expand Up @@ -74,12 +79,6 @@ public class CopyDependenciesMojo extends AbstractFromDependenciesMojo {

private final ArtifactInstaller installer;

@Inject
public CopyDependenciesMojo(CopyUtil copyUtil, ArtifactInstaller installer) {
this.copyUtil = copyUtil;
this.installer = installer;
}

/**
* Either append the artifact's baseVersion or uniqueVersion to the filename. Will only be used if
* {@link #isStripVersion()} is {@code false}.
Expand All @@ -97,6 +96,20 @@ public CopyDependenciesMojo(CopyUtil copyUtil, ArtifactInstaller installer) {
@Parameter(property = "mdep.addParentPoms", defaultValue = "false")
protected boolean addParentPoms;

@Inject
public CopyDependenciesMojo(
CopyUtil copyUtil,
ArtifactInstaller installer,
ResolverUtil resolverUtil,
DependencyResolver dependencyResolver,
RepositoryManager repositoryManager,
ProjectBuilder projectBuilder,
ArtifactHandlerManager artifactHandlerManager) {
super(resolverUtil, dependencyResolver, repositoryManager, projectBuilder, artifactHandlerManager);
this.copyUtil = copyUtil;
this.installer = installer;
}

/**
* Main entry into mojo. Gets the list of dependencies and iterates through calling copyArtifact.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@
import java.io.File;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.plugin.MojoExecutionException;
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.plugins.dependency.utils.DependencyStatusSets;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
import org.apache.maven.plugins.dependency.utils.ResolverUtil;
import org.apache.maven.plugins.dependency.utils.UnpackUtil;
import org.apache.maven.plugins.dependency.utils.filters.MarkerFileFilter;
import org.apache.maven.plugins.dependency.utils.markers.DefaultFileMarkerHandler;
import org.apache.maven.project.ProjectBuilder;
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.codehaus.plexus.components.io.filemappers.FileMapper;

/**
Expand All @@ -51,13 +56,6 @@
// CHECKSTYLE_ON: LineLength
public class UnpackDependenciesMojo extends AbstractFromDependenciesMojo {

private final UnpackUtil unpackUtil;

@Inject
public UnpackDependenciesMojo(UnpackUtil unpackUtil) {
this.unpackUtil = unpackUtil;
}

/**
* A comma separated list of file patterns to include when unpacking the artifact. i.e.
* <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns override the includes. (component code =
Expand Down Expand Up @@ -102,6 +100,20 @@ public UnpackDependenciesMojo(UnpackUtil unpackUtil) {
@Parameter(property = "mdep.unpack.filemappers")
private FileMapper[] fileMappers;

private final UnpackUtil unpackUtil;

@Inject
public UnpackDependenciesMojo(
UnpackUtil unpackUtil,
ResolverUtil resolverUtil,
DependencyResolver dependencyResolver,
RepositoryManager repositoryManager,
ProjectBuilder projectBuilder,
ArtifactHandlerManager artifactHandlerManager) {
super(resolverUtil, dependencyResolver, repositoryManager, projectBuilder, artifactHandlerManager);
this.unpackUtil = unpackUtil;
}

/**
* Main entry into mojo. This method gets the dependencies and iterates through each one passing it to
* DependencyUtil.unpackFile().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@

import java.io.File;

import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.dependency.fromDependencies.AbstractDependencyFilterMojo;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
import org.apache.maven.plugins.dependency.utils.ResolverUtil;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
import org.apache.maven.shared.transfer.repository.RepositoryManager;

/**
* @author <a href="mailto:[email protected]">Brian Fox</a>
Expand Down Expand Up @@ -60,6 +65,15 @@ public abstract class AbstractResolveMojo extends AbstractDependencyFilterMojo {
@Parameter(property = "excludeReactor", defaultValue = "true")
protected boolean excludeReactor;

protected AbstractResolveMojo(
ResolverUtil resolverUtil,
DependencyResolver dependencyResolver,
RepositoryManager repositoryManager,
ProjectBuilder projectBuilder,
ArtifactHandlerManager artifactHandlerManager) {
super(resolverUtil, dependencyResolver, repositoryManager, projectBuilder, artifactHandlerManager);
}

/**
* @return {@link FilterArtifacts}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@
*/
package org.apache.maven.plugins.dependency.resolvers;

import javax.inject.Inject;

import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugins.dependency.utils.ResolverUtil;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
import org.apache.maven.shared.transfer.repository.RepositoryManager;

/**
* <p>
Expand All @@ -45,4 +52,15 @@
requiresDependencyCollection = ResolutionScope.TEST,
defaultPhase = LifecyclePhase.GENERATE_SOURCES,
threadSafe = true)
public class CollectDependenciesMojo extends ResolveDependenciesMojo {}
public class CollectDependenciesMojo extends ResolveDependenciesMojo {

@Inject
public CollectDependenciesMojo(
ResolverUtil resolverUtil,
DependencyResolver dependencyResolver,
RepositoryManager repositoryManager,
ProjectBuilder projectBuilder,
ArtifactHandlerManager artifactHandlerManager) {
super(resolverUtil, dependencyResolver, repositoryManager, projectBuilder, artifactHandlerManager);
}
}
Loading

0 comments on commit b5f78af

Please sign in to comment.