1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.micronaut.build.services;
17
18 import org.apache.maven.execution.MavenSession;
19 import org.apache.maven.model.Plugin;
20 import org.apache.maven.model.PluginExecution;
21 import org.apache.maven.plugin.BuildPluginManager;
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.project.MavenProject;
24 import org.codehaus.plexus.util.xml.Xpp3Dom;
25 import org.twdata.maven.mojoexecutor.MojoExecutor;
26
27 import javax.inject.Inject;
28 import javax.inject.Singleton;
29 import java.util.Optional;
30 import java.util.concurrent.atomic.AtomicReference;
31
32 import static org.twdata.maven.mojoexecutor.MojoExecutor.*;
33
34
35
36
37
38
39
40 @Singleton
41 public class ExecutorService {
42
43 private final MojoExecutor.ExecutionEnvironment executionEnvironment;
44 private final MavenProject mavenProject;
45
46 @SuppressWarnings("CdiInjectionPointsInspection")
47 @Inject
48 public ExecutorService(MavenProject mavenProject, MavenSession mavenSession, BuildPluginManager pluginManager) {
49 this.executionEnvironment = executionEnvironment(mavenProject, mavenSession, pluginManager);
50 this.mavenProject = mavenProject;
51 }
52
53
54
55
56 public void executeGoal(String pluginKey, String goal) throws MojoExecutionException {
57 final Plugin plugin = mavenProject.getPlugin(pluginKey);
58 if (plugin != null) {
59 AtomicReference<String> executionId = new AtomicReference<>(goal);
60 if (goal != null && goal.indexOf('#') > -1) {
61 int pos = goal.indexOf('#');
62 executionId.set(goal.substring(pos + 1));
63 goal = goal.substring(0, pos);
64 }
65 Optional<PluginExecution> execution = plugin
66 .getExecutions()
67 .stream()
68 .filter(e -> e.getId().equals(executionId.get()))
69 .findFirst();
70 Xpp3Dom configuration;
71 if (execution.isPresent()) {
72 configuration = (Xpp3Dom) execution.get().getConfiguration();
73 } else if (plugin.getConfiguration() != null) {
74 configuration = (Xpp3Dom) plugin.getConfiguration();
75 } else {
76 configuration = configuration();
77 }
78 executeMojo(plugin, goal(goal), configuration, executionEnvironment);
79 } else {
80 throw new MojoExecutionException("Plugin not found: " + pluginKey);
81 }
82 }
83
84
85
86
87 public void executeGoal(String pluginGroup, String pluginArtifact, String pluginVersion, String goal, Xpp3Dom configuration) throws MojoExecutionException {
88 final Plugin plugin = plugin(pluginGroup, pluginArtifact, pluginVersion);
89 executeMojo(plugin, goal(goal), configuration, executionEnvironment);
90 }
91 }