Skip to content

Commit

Permalink
Switch to Guice constructor injection (#266)
Browse files Browse the repository at this point in the history
* Switch to Guice injection
  • Loading branch information
elharo authored Dec 24, 2024
1 parent 39d6844 commit 7ad7463
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 28 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ under the License.
<artifactId>maven-shared-utils</artifactId>
<version>3.4.2</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import org.apache.maven.plugin.MojoExecution;
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.invoker.model.BuildJob;
import org.apache.maven.plugins.invoker.model.io.xpp3.BuildJobXpp3Writer;
Expand Down Expand Up @@ -746,23 +745,30 @@ public abstract class AbstractInvokerMojo extends AbstractMojo {
@Parameter(defaultValue = "${settings}", readonly = true, required = true)
private Settings settings;

@Component
private Invoker invoker;
private final Invoker invoker;

@Component
private SettingsBuilder settingsBuilder;
private final SettingsBuilder settingsBuilder;

@Component
private ToolchainManagerPrivate toolchainManagerPrivate;
private final ToolchainManagerPrivate toolchainManagerPrivate;

@Component
private InterpolatorUtils interpolatorUtils;
private final InterpolatorUtils interpolatorUtils;

public AbstractInvokerMojo(
Invoker invoker,
SettingsBuilder settingsBuilder,
ToolchainManagerPrivate toolchainManagerPrivate,
InterpolatorUtils interpolatorUtils) {
this.invoker = invoker;
this.settingsBuilder = settingsBuilder;
this.toolchainManagerPrivate = toolchainManagerPrivate;
this.interpolatorUtils = interpolatorUtils;
}

/**
* Invokes Maven on the configured test projects.
*
* @throws org.apache.maven.plugin.MojoExecutionException If the goal encountered severe errors.
* @throws org.apache.maven.plugin.MojoFailureException If any of the Maven builds failed.
* @throws org.apache.maven.plugin.MojoExecutionException if the goal encountered severe errors
* @throws org.apache.maven.plugin.MojoFailureException if any of the Maven builds failed
*/
public void execute() throws MojoExecutionException, MojoFailureException {
if (skipInvocation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
*/
package org.apache.maven.plugins.invoker;

import javax.inject.Inject;

import org.apache.maven.plugin.MojoFailureException;
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.settings.building.SettingsBuilder;
import org.apache.maven.shared.invoker.Invoker;
import org.apache.maven.toolchain.ToolchainManagerPrivate;

/**
* Searches for integration test Maven projects, and executes each, collecting a log in the project directory, will
Expand All @@ -36,10 +41,19 @@
defaultPhase = LifecyclePhase.INTEGRATION_TEST,
requiresDependencyResolution = ResolutionScope.TEST,
threadSafe = true)
// CHECKSTYLE_ON: LineLength
public class IntegrationTestMojo extends AbstractInvokerMojo {

@Inject
public IntegrationTestMojo(
Invoker invoker,
SettingsBuilder settingsBuilder,
ToolchainManagerPrivate toolchainManagerPrivate,
InterpolatorUtils interpolatorUtils) {
super(invoker, settingsBuilder, toolchainManagerPrivate, interpolatorUtils);
}

void processResults(InvokerSession invokerSession) throws MojoFailureException {
// do nothing
}
}
// CHECKSTYLE_ON: LineLength
14 changes: 14 additions & 0 deletions src/main/java/org/apache/maven/plugins/invoker/InvokerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
*/
package org.apache.maven.plugins.invoker;

import javax.inject.Inject;

import org.apache.maven.plugin.MojoFailureException;
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.settings.building.SettingsBuilder;
import org.apache.maven.shared.invoker.Invoker;
import org.apache.maven.toolchain.ToolchainManagerPrivate;

/**
* Searches for integration test Maven projects, and executes each, collecting a log in the project directory, and
Expand Down Expand Up @@ -66,6 +71,15 @@ public class InvokerMojo extends AbstractInvokerMojo {
@Parameter(property = "invoker.streamLogsOnFailures", defaultValue = "false")
private boolean streamLogsOnFailures;

@Inject
public InvokerMojo(
Invoker invoker,
SettingsBuilder settingsBuilder,
ToolchainManagerPrivate toolchainManagerPrivate,
InterpolatorUtils interpolaterUtils) {
super(invoker, settingsBuilder, toolchainManagerPrivate, interpolaterUtils);
}

void processResults(InvokerSession invokerSession) throws MojoFailureException {
if (streamLogsOnFailures) {
invokerSession.logFailedBuildLog(getLog(), ignoreFailures);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
*/
package org.apache.maven.plugins.invoker;

import javax.inject.Inject;

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

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.plugins.invoker.model.BuildJob;
Expand Down Expand Up @@ -55,8 +56,12 @@ public class InvokerReport extends AbstractMavenReport {
/**
* Internationalization component
*/
@Component
protected I18N i18n;
protected final I18N i18n;

@Inject
public InvokerReport(I18N i18n) {
this.i18n = i18n;
}

protected void executeReport(Locale locale) throws MavenReportException {
File[] reportFiles = getReportFiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ private MavenProject buildMavenProjectStub() {

@Test
void testCompositeMap() {

Map<String, Object> properties = new HashMap<>();
properties.put("foo", "bar");
properties.put("version", "2.0-SNAPSHOT");
Expand All @@ -70,8 +69,7 @@ void testCompositeMap() {

@Test
void testPomInterpolation() throws Exception {
File interpolatedPomFile;
InvokerMojo invokerMojo = new InvokerMojo();
InvokerMojo invokerMojo = new InvokerMojo(null, null, null, null);
TestUtil.setVariableValueToObject(invokerMojo, "project", buildMavenProjectStub());
TestUtil.setVariableValueToObject(invokerMojo, "settings", new Settings());
Properties properties = new Properties();
Expand All @@ -81,7 +79,7 @@ void testPomInterpolation() throws Exception {
String dirPath = TestUtil.getBasedir() + File.separatorChar + "src" + File.separatorChar + "test"
+ File.separatorChar + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";

interpolatedPomFile = new File(TestUtil.getBasedir(), "target/interpolated-pom.xml");
File interpolatedPomFile = new File(TestUtil.getBasedir(), "target/interpolated-pom.xml");
invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile);
try (Reader reader = new XmlStreamReader(interpolatedPomFile)) {
String content = IOUtil.toString(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class InvokerMojoTest {
private static final String INTERPOLATION_PROJECT = "interpolation" + File.separator + "pom.xml";
private static final String WITHOUT_POM_PROJECT = "without-pom-project-dir";

private final InvokerMojo invokerMojo = new InvokerMojo(null, null, null, null);

private MavenProject getMavenProject() {
MavenProject mavenProject = new MavenProject();
mavenProject.setFile(new File("target/foo.txt"));
Expand All @@ -52,7 +54,6 @@ private MavenProject getMavenProject() {
void testSingleInvokerTest() throws Exception {
// given
MavenProject mavenProject = getMavenProject();
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
Expand All @@ -72,7 +73,6 @@ void testSingleInvokerTest() throws Exception {
void testMultiInvokerTest() throws Exception {
// given
MavenProject mavenProject = getMavenProject();
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
Expand All @@ -92,7 +92,6 @@ void testMultiInvokerTest() throws Exception {
void testFullPatternInvokerTest() throws Exception {
// given
MavenProject mavenProject = getMavenProject();
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
Expand All @@ -115,7 +114,6 @@ void testFullPatternInvokerTest() throws Exception {
void testSetupInProjectList() throws Exception {
// given
MavenProject mavenProject = getMavenProject();
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
Expand Down Expand Up @@ -144,11 +142,10 @@ void testSetupInProjectList() throws Exception {
@Test
void testSetupProjectIsFiltered() throws Exception {
// given
MavenProject mavenProject = getMavenProject();
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
MavenProject mavenProject = getMavenProject();
setVariableValueToObject(invokerMojo, "project", mavenProject);
setVariableValueToObject(invokerMojo, "interpolatorUtils", new InterpolatorUtils(mavenProject));
setVariableValueToObject(invokerMojo, "settings", new Settings());
Expand Down Expand Up @@ -189,8 +186,6 @@ void testParallelThreadsSettings() throws Exception {
{"2.5C", (int) (Double.parseDouble("2.5") * Runtime.getRuntime().availableProcessors())}
};

InvokerMojo invokerMojo = new InvokerMojo();

for (Object[] testValue : testValues) {
String parallelThreads = (String) testValue[0];
int expectedParallelThreads = (Integer) testValue[1];
Expand Down

0 comments on commit 7ad7463

Please sign in to comment.