Skip to content

Commit

Permalink
Switch to Guice injection
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Dec 6, 2024
1 parent ffb89ce commit 94563cb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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 @@ -728,20 +727,24 @@ 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;

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

/**
* 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,16 @@
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) {
super(invoker, settingsBuilder, toolchainManagerPrivate);
}

void processResults(InvokerSession invokerSession) throws MojoFailureException {
// do nothing
}
}
// CHECKSTYLE_ON: LineLength
11 changes: 11 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,12 @@ public class InvokerMojo extends AbstractInvokerMojo {
@Parameter(property = "invoker.streamLogsOnFailures", defaultValue = "false")
private boolean streamLogsOnFailures;

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

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 @@ -56,8 +57,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 @@ -67,7 +67,7 @@ public void testPomInterpolation() throws Exception {
Reader reader = null;
File interpolatedPomFile;
try {
InvokerMojo invokerMojo = new InvokerMojo();
InvokerMojo invokerMojo = new InvokerMojo(null, null, null);
setVariableValueToObject(invokerMojo, "project", buildMavenProjectStub());
setVariableValueToObject(invokerMojo, "settings", new Settings());
Properties properties = new Properties();
Expand All @@ -84,7 +84,6 @@ public void testPomInterpolation() throws Exception {
String content = IOUtil.toString(reader);
assertTrue(content.indexOf("<interpolateValue>bar</interpolateValue>") > 0);
reader.close();
reader = null;
// recreate it to test delete if exists before creation
invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile);
reader = ReaderFactory.newXmlReader(interpolatedPomFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public class InvokerMojoTest extends AbstractMojoTestCase {
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);

private MavenProject getMavenProject() {
MavenProject mavenProject = new MavenProject();
mavenProject.setFile(new File("target/foo.txt"));
return mavenProject;
}

public void testSingleInvokerTest() throws Exception {
// given
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
Expand All @@ -64,8 +64,6 @@ public void testSingleInvokerTest() throws Exception {
}

public void testMultiInvokerTest() throws Exception {
// given
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
Expand All @@ -82,7 +80,6 @@ public void testMultiInvokerTest() throws Exception {

public void testFullPatternInvokerTest() throws Exception {
// given
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
Expand All @@ -101,8 +98,6 @@ public void testFullPatternInvokerTest() throws Exception {
}

public void testSetupInProjectList() throws Exception {
// given
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
Expand All @@ -129,7 +124,6 @@ public void testSetupInProjectList() throws Exception {

public void testSetupProjectIsFiltered() throws Exception {
// given
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 @@ -166,8 +160,6 @@ public void testParallelThreadsSettings() throws IllegalAccessException {
{"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 94563cb

Please sign in to comment.