1   package com.atlassian.maven.plugins.ampsdispatcher;
2   
3   import java.util.HashSet;
4   import java.util.Iterator;
5   import java.util.List;
6   import java.util.Set;
7   
8   import com.atlassian.maven.plugins.amps.product.ProductHandlerFactory;
9   import com.atlassian.maven.plugins.amps.util.VersionUtils;
10  
11  import org.apache.maven.execution.MavenSession;
12  import org.apache.maven.model.Plugin;
13  import org.apache.maven.plugin.AbstractMojo;
14  import org.apache.maven.plugin.MojoExecutionException;
15  import org.apache.maven.plugin.MojoFailureException;
16  import org.apache.maven.plugin.PluginManager;
17  import org.apache.maven.plugins.annotations.Component;
18  import org.apache.maven.plugins.annotations.Parameter;
19  import org.apache.maven.project.MavenProject;
20  
21  import static org.twdata.maven.mojoexecutor.MojoExecutor.artifactId;
22  import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration;
23  import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo;
24  import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment;
25  import static org.twdata.maven.mojoexecutor.MojoExecutor.goal;
26  import static org.twdata.maven.mojoexecutor.MojoExecutor.groupId;
27  import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin;
28  import static org.twdata.maven.mojoexecutor.MojoExecutor.version;
29  
30  /**
31   * Dispatches to the appropriate amps product-specific plugin by detecting the plugin in the project.
32   *
33   * @since 3.0-beta2
34   */
35  public abstract class AbstractAmpsDispatcherMojo extends AbstractMojo
36  {
37  
38      /**
39       * The Maven Project Object
40       */
41      @Parameter(property = "project", required = true, readonly = true)
42      MavenProject project;
43  
44      /**
45       * The Maven Session Object
46       */
47      @Parameter(property = "session", required = true, readonly = true)
48      MavenSession session;
49  
50      /**
51       * The Maven PluginManager Object
52       */
53      @Component
54      PluginManager pluginManager;
55  
56      public final void execute() throws MojoExecutionException, MojoFailureException
57      {
58          String targetArtifactId = detectAmpsProduct();
59  
60          if (targetArtifactId != null && session.getGoals().size() > 0)
61          {
62              // We only pass in the first goal since we know the shell scripts only pass in one goal
63              String goal = determineGoal();
64  
65              executeMojo(
66                  plugin(
67                          groupId("com.atlassian.maven.plugins"),
68                          artifactId(targetArtifactId),
69                          version(VersionUtils.getVersion())  //ignored anyway
70                  ),
71                  goal(goal),
72                  configuration(),
73                  executionEnvironment(project, session, pluginManager));
74          }
75          else
76          {
77              throw new MojoFailureException("Couldn't detect an AMPS product to dispatch to");
78          }
79      }
80  
81      final String determineGoal()
82      {
83          String goal = (String) session.getGoals().get(0);
84          goal = goal.substring(goal.lastIndexOf(":") + 1);
85          return goal;
86      }
87  
88      final String detectAmpsProduct()
89      {
90          List buildPlugins = project.getBuildPlugins();
91  
92          Set<String> possiblePluginTypes = new HashSet<String>(ProductHandlerFactory.getIds());
93          possiblePluginTypes.add("amps");
94  
95          if (buildPlugins != null)
96          {
97              for (Iterator iterator = buildPlugins.iterator(); iterator.hasNext();)
98              {
99                  Plugin pomPlugin = (Plugin) iterator.next();
100 
101                 if ("com.atlassian.maven.plugins".equals(pomPlugin.getGroupId()))
102                 {
103                     for (String type : possiblePluginTypes)
104                     {
105                         if (("maven-" + type + "-plugin").equals(pomPlugin.getArtifactId()))
106                         {
107                             return pomPlugin.getArtifactId();
108                         }
109                     }
110                 }
111             }
112         }
113         return null;
114 
115     }
116 }