View Javadoc
1   /*
2    * Copyright 2017-2022 original authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Provides methods to execute goals on the current project.
36   *
37   * @author Álvaro Sánchez-Mariscal
38   * @since 1.1
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       * Executes the given goal from the given plugin coordinates.
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       * Executes a goal using the given arguments.
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  }