1   package com.atlassian.maven.plugins.amps;
2   
3   import com.atlassian.maven.plugins.amps.product.ProductHandlerFactory;
4   import org.apache.maven.execution.MavenSession;
5   import org.apache.maven.model.Plugin;
6   import org.apache.maven.plugin.AbstractMojo;
7   import org.apache.maven.plugin.PluginManager;
8   import org.apache.maven.project.MavenProject;
9   import org.jfrog.maven.annomojo.annotations.MojoComponent;
10  import org.jfrog.maven.annomojo.annotations.MojoParameter;
11  
12  import java.util.Collection;
13  import java.util.List;
14  import java.util.ArrayList;
15  
16  public abstract class AbstractAmpsMojo extends AbstractMojo
17  {
18      /**
19       * The Maven Project Object
20       */
21      @MojoParameter (expression = "${project}", required = true, readonly = true)
22      private MavenProject project;
23  
24      /**
25       * The list of modules being built, the reactor
26       */
27      @MojoParameter (expression = "${reactorProjects}", required = true, readonly = true)
28      private List<MavenProject> reactor;
29  
30      /**
31       * The Maven Session Object
32       */
33      @MojoParameter (expression = "${session}", required = true, readonly = true)
34      private MavenSession session;
35  
36      /**
37       * The Maven PluginManager Object
38       */
39      @MojoComponent
40      private PluginManager pluginManager;
41  
42      /**
43       * the maven context
44       */
45      private MavenContext mavenContext;
46  
47      /**
48       * the maven goals
49       */
50      private MavenGoals mavenGoals;
51  
52      /**
53       * Information about the currently used plugin
54       */
55      private PluginInformation pluginInformation;
56  
57      protected MavenContext getMavenContext()
58      {
59          if (mavenContext == null)
60          {
61              mavenContext = new MavenContext(project, reactor, session, pluginManager, getLog());
62          }
63          return mavenContext;
64      }
65  
66      protected MavenGoals getMavenGoals()
67      {
68          if (mavenGoals == null)
69          {
70              mavenGoals = new MavenGoals(getMavenContext());
71          }
72          return mavenGoals;
73      }
74  
75      protected PluginInformation getPluginInformation()
76      {
77          if (pluginInformation != null)
78          {
79              return pluginInformation;
80          }
81  
82          if (project != null)
83          {
84              for (Plugin plugin : (List<Plugin>) project.getBuild().getPlugins())
85              {
86                  if ("com.atlassian.maven.plugins".equals(plugin.getGroupId()))
87                  {
88                      Collection<String> pluginIds = new ArrayList<String>(ProductHandlerFactory.getIds());
89                      pluginIds.add("amps");
90                      for (String pluginId : pluginIds)
91                      {
92                          if (("maven-" + pluginId + "-plugin").equals(plugin.getArtifactId()))
93                          {
94                              pluginInformation = new PluginInformation(pluginId, plugin.getVersion() != null ? plugin.getVersion() : "RELEASE");
95                              return pluginInformation;
96                          }
97                      }
98  
99                  }
100             }
101         }
102         return new PluginInformation(null, "RELEASE");
103     }
104 }