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