Skip to content

Commit a4297c6

Browse files
Use JSR-330 for component injection
1 parent 5d90977 commit a4297c6

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@
142142
</properties>
143143

144144
<dependencies>
145+
<dependency>
146+
<groupId>javax.inject</groupId>
147+
<artifactId>javax.inject</artifactId>
148+
<version>1</version>
149+
<scope>provided</scope>
150+
</dependency>
145151
<dependency>
146152
<groupId>org.apache.maven</groupId>
147153
<artifactId>maven-model</artifactId>

src/main/java/org/codehaus/mojo/exec/AbstractExecMojo.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Collections;
2626
import java.util.HashSet;
2727
import java.util.List;
28+
import java.util.Objects;
2829
import java.util.Set;
2930
import java.util.stream.Collectors;
3031

@@ -34,7 +35,6 @@
3435
import org.apache.maven.model.Resource;
3536
import org.apache.maven.plugin.AbstractMojo;
3637
import org.apache.maven.plugin.MojoExecutionException;
37-
import org.apache.maven.plugins.annotations.Component;
3838
import org.apache.maven.plugins.annotations.Parameter;
3939
import org.apache.maven.project.MavenProject;
4040
import org.codehaus.plexus.util.cli.CommandLineUtils;
@@ -57,8 +57,7 @@
5757
*/
5858
public abstract class AbstractExecMojo extends AbstractMojo {
5959

60-
@Component
61-
protected RepositorySystem repositorySystem;
60+
protected final RepositorySystem repositorySystem;
6261

6362
/**
6463
* The enclosing project.
@@ -168,6 +167,10 @@ public abstract class AbstractExecMojo extends AbstractMojo {
168167
@Parameter(property = "exec.includePluginsDependencies", defaultValue = "false")
169168
protected boolean includePluginDependencies;
170169

170+
protected AbstractExecMojo(RepositorySystem repositorySystem) {
171+
this.repositorySystem = Objects.requireNonNull(repositorySystem);
172+
}
173+
171174
/**
172175
* Collects the project artifacts in the specified List and the project specific classpath (build output and build
173176
* test output) Files in the specified List, depending on the plugin classpathScope value.

src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.codehaus.mojo.exec;
22

3+
import javax.inject.Inject;
4+
35
import java.io.IOException;
46
import java.lang.invoke.MethodHandle;
57
import java.lang.invoke.MethodHandles;
@@ -15,6 +17,7 @@
1517
import java.util.Comparator;
1618
import java.util.HashSet;
1719
import java.util.List;
20+
import java.util.Objects;
1821
import java.util.Properties;
1922
import java.util.Set;
2023
import java.util.concurrent.CountDownLatch;
@@ -27,12 +30,12 @@
2730
import org.apache.maven.artifact.Artifact;
2831
import org.apache.maven.plugin.MojoExecutionException;
2932
import org.apache.maven.plugin.MojoFailureException;
30-
import org.apache.maven.plugins.annotations.Component;
3133
import org.apache.maven.plugins.annotations.Mojo;
3234
import org.apache.maven.plugins.annotations.Parameter;
3335
import org.apache.maven.plugins.annotations.ResolutionScope;
3436
import org.codehaus.plexus.PlexusContainer;
3537
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
38+
import org.eclipse.aether.RepositorySystem;
3639
import org.eclipse.aether.artifact.DefaultArtifact;
3740
import org.eclipse.aether.resolution.VersionRangeRequest;
3841
import org.eclipse.aether.resolution.VersionRangeResolutionException;
@@ -226,8 +229,14 @@ public class ExecJavaMojo extends AbstractExecMojo {
226229
@Parameter(property = "exec.blockSystemExit", defaultValue = "false")
227230
private boolean blockSystemExit;
228231

229-
@Component // todo: for maven4 move to Lookup instead
230-
private PlexusContainer container;
232+
// todo: for maven4 move to Lookup instead
233+
private final PlexusContainer container;
234+
235+
@Inject
236+
protected ExecJavaMojo(RepositorySystem repositorySystem, PlexusContainer container) {
237+
super(repositorySystem);
238+
this.container = Objects.requireNonNull(container);
239+
}
231240

232241
/**
233242
* Execute goal.

src/main/java/org/codehaus/mojo/exec/ExecMojo.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* under the License.
2020
*/
2121

22+
import javax.inject.Inject;
23+
2224
import java.io.BufferedOutputStream;
2325
import java.io.BufferedReader;
2426
import java.io.File;
@@ -61,7 +63,6 @@
6163
import org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter;
6264
import org.apache.maven.execution.MavenSession;
6365
import org.apache.maven.plugin.MojoExecutionException;
64-
import org.apache.maven.plugins.annotations.Component;
6566
import org.apache.maven.plugins.annotations.Mojo;
6667
import org.apache.maven.plugins.annotations.Parameter;
6768
import org.apache.maven.plugins.annotations.ResolutionScope;
@@ -74,6 +75,7 @@
7475
import org.codehaus.plexus.util.cli.Commandline;
7576
import org.codehaus.plexus.util.cli.DefaultConsumer;
7677
import org.codehaus.plexus.util.cli.StreamConsumer;
78+
import org.eclipse.aether.RepositorySystem;
7779

7880
/**
7981
* A Plugin for executing external programs.
@@ -350,13 +352,18 @@ public class ExecMojo extends AbstractExecMojo {
350352
@Parameter(property = "exec.toolchainJavaHomeEnvName", defaultValue = "TOOLCHAIN_JAVA_HOME")
351353
private String toolchainJavaHomeEnvName = "TOOLCHAIN_JAVA_HOME";
352354

353-
@Component
354-
private ToolchainManager toolchainManager;
355+
private final ToolchainManager toolchainManager;
355356

356357
public static final String CLASSPATH_TOKEN = "%classpath";
357358

358359
public static final String MODULEPATH_TOKEN = "%modulepath";
359360

361+
@Inject
362+
protected ExecMojo(RepositorySystem repositorySystem, ToolchainManager toolchainManager) {
363+
super(repositorySystem);
364+
this.toolchainManager = Objects.requireNonNull(toolchainManager);
365+
}
366+
360367
/**
361368
* priority in the execute method will be to use System properties arguments over the pom specification.
362369
*

src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.maven.plugin.MojoExecutionException;
3232
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
3333
import org.apache.maven.toolchain.ToolchainManager;
34+
import org.eclipse.aether.RepositorySystem;
3435

3536
import static java.util.Collections.emptyMap;
3637
import static org.mockito.ArgumentMatchers.any;
@@ -45,6 +46,7 @@
4546
public class ExecMojoTest extends AbstractMojoTestCase {
4647
private MavenSession session = mock(MavenSession.class);
4748
private ToolchainManager toolchainManager = mock(ToolchainManager.class);
49+
private RepositorySystem repositorySystem = mock(RepositorySystem.class);
4850

4951
private static final File LOCAL_REPO = new File("src/test/repository");
5052

@@ -65,6 +67,10 @@ static class MockExecMojo extends ExecMojo {
6567

6668
public Map<String, String> systemProperties = new HashMap<>();
6769

70+
protected MockExecMojo(RepositorySystem repositorySystem, ToolchainManager toolchainManager) {
71+
super(repositorySystem, toolchainManager);
72+
}
73+
6874
protected int executeCommandLine(
6975
Executor exec, CommandLine commandLine, Map enviro, OutputStream out, OutputStream err)
7076
throws ExecuteException {
@@ -90,7 +96,7 @@ CommandLine getExecutedCommandline(int index) {
9096

9197
public void setUp() throws Exception {
9298
super.setUp();
93-
mojo = new MockExecMojo();
99+
mojo = new MockExecMojo(repositorySystem, toolchainManager);
94100
// note: most of the tests below assume that the specified
95101
// executable path is not fully specicied. See ExecMojo#getExecutablePath
96102
mojo.setExecutable(SOME_EXECUTABLE);
@@ -121,7 +127,7 @@ public void testRunOK() throws MojoExecutionException {
121127
// MEXEC-12, MEXEC-72
122128
public void testGetExecutablePath() throws IOException {
123129

124-
ExecMojo realMojo = new ExecMojo();
130+
ExecMojo realMojo = new ExecMojo(repositorySystem, toolchainManager);
125131

126132
File workdir = new File(System.getProperty("user.dir"));
127133
Map<String, String> enviro = new HashMap<>();
@@ -177,7 +183,7 @@ public void testGetExecutablePathPreferExecutableExtensionsOnWindows() throws IO
177183
if (!OS.isFamilyWindows()) {
178184
return;
179185
}
180-
final ExecMojo realMojo = new ExecMojo();
186+
final ExecMojo realMojo = new ExecMojo(repositorySystem, toolchainManager);
181187

182188
final String tmp = System.getProperty("java.io.tmpdir");
183189
final File workdir = new File(tmp, "testGetExecutablePathPreferExecutableExtensionsOnWindows");
@@ -251,7 +257,7 @@ public void testOverrides3() throws MojoExecutionException {
251257
}
252258

253259
public void testIsResultCodeAFailure() {
254-
ExecMojo execMojo = new ExecMojo();
260+
ExecMojo execMojo = new ExecMojo(repositorySystem, toolchainManager);
255261
assertTrue(execMojo.isResultCodeAFailure(1));
256262
assertFalse(execMojo.isResultCodeAFailure(0));
257263

@@ -268,7 +274,7 @@ public void testIsResultCodeAFailure() {
268274

269275
// MEXEC-81
270276
public void testParseCommandlineOSWin() throws Exception {
271-
ExecMojo execMojo = new ExecMojo();
277+
ExecMojo execMojo = new ExecMojo(repositorySystem, toolchainManager);
272278
final String javaHome = "C:\\Java\\jdk1.5.0_15";
273279
// can only be set by expression or plugin-configuration
274280
setVariableValueToObject(execMojo, "commandlineArgs", javaHome);
@@ -278,7 +284,7 @@ public void testParseCommandlineOSWin() throws Exception {
278284

279285
public void test_exec_receives_all_parameters() throws MojoExecutionException {
280286
// given
281-
ExecMojo execMojo = new ExecMojo();
287+
ExecMojo execMojo = new ExecMojo(repositorySystem, toolchainManager);
282288
execMojo.setExecutable("mkdir");
283289
execMojo.setArguments(Arrays.asList("-p", "dist/mails"));
284290
execMojo.setBasedir(new File("target"));
@@ -358,7 +364,7 @@ private String getCommandLineAsString(CommandLine commandline) {
358364
}
359365

360366
public void testGetShebang() throws Exception {
361-
ExecMojo execMojo = new ExecMojo();
367+
ExecMojo execMojo = new ExecMojo(repositorySystem, toolchainManager);
362368

363369
// without shebang
364370
File noShebang = Files.createTempFile("noShebang", ".sh").toFile();
@@ -384,7 +390,7 @@ public void testGetShebang() throws Exception {
384390
}
385391

386392
public void testCreateEnvWrapperFile() throws Exception {
387-
ExecMojo execMojo = new ExecMojo();
393+
ExecMojo execMojo = new ExecMojo(repositorySystem, toolchainManager);
388394

389395
// without shebang
390396
File envScript = Files.createTempFile("envScript", ".sh").toFile();

0 commit comments

Comments
 (0)