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