Skip to content

Commit

Permalink
Convert Analyze Mojos to Guice constructor injection (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo authored Dec 30, 2024
1 parent 8b07cb3 commit 89744d0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.maven.plugin.AbstractMojo;
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.project.MavenProject;
import org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter;
Expand All @@ -55,19 +54,6 @@
public abstract class AbstractAnalyzeMojo extends AbstractMojo {
// fields -----------------------------------------------------------------

/**
* The plexusContainer to look-up the right {@link ProjectDependencyAnalyzer} implementation depending on the mojo
* configuration.
*/
@Component
private PlexusContainer plexusContainer;

/**
* The Maven project to analyze.
*/
@Component
private MavenProject project;

/**
* Specify the project dependency analyzer to use (plexus component role-hint). By default,
* <a href="/shared/maven-dependency-analyzer/">maven-dependency-analyzer</a> is used. To use this, you must declare
Expand Down Expand Up @@ -271,6 +257,22 @@ public abstract class AbstractAnalyzeMojo extends AbstractMojo {
@Parameter(property = "mdep.analyze.excludedClasses")
private Set<String> excludedClasses;

/**
* The plexusContainer to look up the {@link ProjectDependencyAnalyzer} implementation depending on the mojo
* configuration.
*/
private final PlexusContainer plexusContainer;

/**
* The Maven project to analyze.
*/
private final MavenProject project;

protected AbstractAnalyzeMojo(PlexusContainer plexusContainer, MavenProject project) {
this.plexusContainer = plexusContainer;
this.project = project;
}

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

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

import javax.inject.Inject;

import org.apache.maven.plugins.annotations.Execute;
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.project.MavenProject;
import org.codehaus.plexus.PlexusContainer;

/**
* Analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused
* and declared. This goal is intended to be used standalone, thus it always executes the <code>test-compile</code>
* phase - use the <code>dependency:analyze-only</code> goal instead when participating in the build lifecycle.
* and declared. This goal is intended to be used standalone. Thus, it always executes the <code>test-compile</code>
* phase. Use the <code>dependency:analyze-only</code> goal instead when participating in the build lifecycle.
* <p>
* By default, <a href="http://maven.apache.org/shared/maven-dependency-analyzer/">maven-dependency-analyzer</a> is used
* By default, <a href="https://maven.apache.org/shared/maven-dependency-analyzer/">maven-dependency-analyzer</a> is used
* to perform the analysis, with limitations due to the fact that it works at bytecode level, but any analyzer can be
* plugged in through <code>analyzer</code> parameter.
* </p>
Expand All @@ -41,4 +45,9 @@
@Execute(phase = LifecyclePhase.TEST_COMPILE)
public class AnalyzeMojo extends AbstractAnalyzeMojo {
// subclassed to provide annotations

@Inject
public AnalyzeMojo(PlexusContainer plexusContainer, MavenProject project) {
super(plexusContainer, project);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@
*/
package org.apache.maven.plugins.dependency.analyze;

import javax.inject.Inject;

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.project.MavenProject;
import org.codehaus.plexus.PlexusContainer;

/**
* Analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused
* and declared. This goal is intended to be used in the build lifecycle, thus it assumes that the
* <code>test-compile</code> phase has been executed - use the <code>dependency:analyze</code> goal instead when running
* standalone.
* <p>
* By default, <a href="http://maven.apache.org/shared/maven-dependency-analyzer/">maven-dependency-analyzer</a> is used
* By default, <a href="https://maven.apache.org/shared/maven-dependency-analyzer/">maven-dependency-analyzer</a> is used
* to perform the analysis, with limitations due to the fact that it works at bytecode level, but any analyzer can be
* plugged in through <code>analyzer</code> parameter.
* </p>
Expand All @@ -46,4 +50,9 @@
// @formatter:on
public class AnalyzeOnlyMojo extends AbstractAnalyzeMojo {
// subclassed to provide annotations

@Inject
public AnalyzeOnlyMojo(PlexusContainer plexusContainer, MavenProject project) {
super(plexusContainer, project);
}
}

0 comments on commit 89744d0

Please sign in to comment.