1   package com.atlassian.maven.plugins.ampsdispatcher;
2   
3   import org.apache.maven.plugin.AbstractMojo;
4   import org.apache.maven.plugin.MojoExecutionException;
5   import org.apache.maven.plugin.MojoFailureException;
6   import org.apache.maven.plugin.PluginManager;
7   import org.apache.maven.project.MavenProject;
8   import org.apache.maven.execution.MavenSession;
9   import org.jfrog.maven.annomojo.annotations.MojoParameter;
10  import org.jfrog.maven.annomojo.annotations.MojoComponent;
11  import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo;
12  import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin;
13  import static org.twdata.maven.mojoexecutor.MojoExecutor.groupId;
14  import static org.twdata.maven.mojoexecutor.MojoExecutor.artifactId;
15  import static org.twdata.maven.mojoexecutor.MojoExecutor.version;
16  import static org.twdata.maven.mojoexecutor.MojoExecutor.goal;
17  import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration;
18  import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment;
19  
20  import com.atlassian.maven.plugins.amps.util.MavenPropertiesUtils;
21  import com.atlassian.maven.plugins.amps.util.VersionUtils;
22  
23  /**
24   * Dispatches to the appropriate amps product-specific plugin by detecting the plugin in the project.
25   *
26   * @since 3.0-beta2
27   */
28  public abstract class AbstractAmpsDispatcherMojo extends AbstractMojo
29  {
30  
31      /**
32       * The Maven Project Object
33       */
34      @MojoParameter(expression = "${project}", required = true, readonly = true)
35      MavenProject project;
36  
37      /**
38       * The Maven Session Object
39       */
40      @MojoParameter(expression = "${session}", required = true, readonly = true)
41      MavenSession session;
42  
43      /**
44       * The Maven PluginManager Object
45       */
46      @MojoComponent
47      PluginManager pluginManager;
48  
49      public final void execute() throws MojoExecutionException, MojoFailureException
50      {
51          String targetArtifact = MavenPropertiesUtils.detectAmpsProduct(project);
52  
53          if (targetArtifact != null && session.getGoals().size() > 0)
54          {
55              // We only pass in the first goal since we know the shell scripts only pass in one goal
56              String goal = determineGoal();
57  
58              executeMojo(
59                  plugin(
60                          groupId("com.atlassian.maven.plugins"),
61                          artifactId(targetArtifact),
62                          version(VersionUtils.getVersion())  //ignored anyway
63                  ),
64                  goal(goal),
65                  configuration(),
66                  executionEnvironment(project, session, pluginManager));
67          }
68          else
69          {
70              throw new MojoFailureException("Couldn't detect an AMPS product to dispatch to");
71          }
72      }
73  
74      final String determineGoal()
75      {
76          String goal = (String) session.getGoals().get(0);
77          goal = goal.substring(goal.lastIndexOf(":") + 1);
78          return goal;
79      }
80  }