View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin;
20  
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  import java.util.Map;
24  import org.apache.maven.api.xml.Dom;
25  import org.apache.maven.model.Plugin;
26  import org.apache.maven.plugin.descriptor.MojoDescriptor;
27  
28  /**
29   * MojoExecution
30   */
31  public class MojoExecution {
32  
33      private Plugin plugin;
34  
35      private String goal;
36  
37      private String executionId;
38  
39      private MojoDescriptor mojoDescriptor;
40  
41      private org.codehaus.plexus.util.xml.Xpp3Dom configuration;
42  
43      /**
44       * Describes the source of an execution.
45       */
46      public enum Source {
47  
48          /**
49           * An execution that originates from the direct invocation of a goal from the CLI.
50           */
51          CLI,
52  
53          /**
54           * An execution that originates from a goal bound to a lifecycle phase.
55           */
56          LIFECYCLE,
57      }
58  
59      private Source source = Source.LIFECYCLE;
60  
61      /**
62       * The phase may or may not have been bound to a phase but once the plan has been calculated we know what phase
63       * this mojo execution is going to run in.
64       */
65      private String lifecyclePhase;
66  
67      /**
68       * The executions to fork before this execution, indexed by the groupId:artifactId:version of the project on which
69       * the forked execution are to be run and in reactor build order.
70       */
71      private Map<String, List<MojoExecution>> forkedExecutions = new LinkedHashMap<>();
72  
73      public MojoExecution(Plugin plugin, String goal, String executionId) {
74          this.plugin = plugin;
75          this.goal = goal;
76          this.executionId = executionId;
77      }
78  
79      public MojoExecution(MojoDescriptor mojoDescriptor) {
80          this.mojoDescriptor = mojoDescriptor;
81          this.executionId = null;
82          this.configuration = null;
83      }
84  
85      public MojoExecution(MojoDescriptor mojoDescriptor, String executionId, Source source) {
86          this.mojoDescriptor = mojoDescriptor;
87          this.executionId = executionId;
88          this.configuration = null;
89          this.source = source;
90      }
91  
92      public MojoExecution(MojoDescriptor mojoDescriptor, String executionId) {
93          this.mojoDescriptor = mojoDescriptor;
94          this.executionId = executionId;
95          this.configuration = null;
96      }
97  
98      public MojoExecution(MojoDescriptor mojoDescriptor, org.codehaus.plexus.util.xml.Xpp3Dom configuration) {
99          this.mojoDescriptor = mojoDescriptor;
100         this.configuration = configuration;
101         this.executionId = null;
102     }
103 
104     public MojoExecution(MojoDescriptor mojoDescriptor, Dom configuration) {
105         this.mojoDescriptor = mojoDescriptor;
106         this.configuration = new org.codehaus.plexus.util.xml.Xpp3Dom(configuration);
107         this.executionId = null;
108     }
109 
110     /**
111      * Gets the source of this execution.
112      *
113      * @return The source of this execution or {@code null} if unknown.
114      */
115     public Source getSource() {
116         return source;
117     }
118 
119     public String getExecutionId() {
120         return executionId;
121     }
122 
123     public Plugin getPlugin() {
124         if (mojoDescriptor != null) {
125             return mojoDescriptor.getPluginDescriptor().getPlugin();
126         }
127 
128         return plugin;
129     }
130 
131     public MojoDescriptor getMojoDescriptor() {
132         return mojoDescriptor;
133     }
134 
135     public org.codehaus.plexus.util.xml.Xpp3Dom getConfiguration() {
136         return configuration;
137     }
138 
139     public void setConfiguration(org.codehaus.plexus.util.xml.Xpp3Dom configuration) {
140         this.configuration = configuration;
141     }
142 
143     public void setConfiguration(Dom configuration) {
144         this.configuration = configuration != null ? new org.codehaus.plexus.util.xml.Xpp3Dom(configuration) : null;
145     }
146 
147     public String identify() {
148         StringBuilder sb = new StringBuilder(256);
149 
150         sb.append(executionId);
151         sb.append(configuration.toString());
152 
153         return sb.toString();
154     }
155 
156     public String getLifecyclePhase() {
157         return lifecyclePhase;
158     }
159 
160     public void setLifecyclePhase(String lifecyclePhase) {
161         this.lifecyclePhase = lifecyclePhase;
162     }
163 
164     @Override
165     public String toString() {
166         StringBuilder buffer = new StringBuilder(128);
167         if (mojoDescriptor != null) {
168             buffer.append(mojoDescriptor.getId());
169         }
170         buffer.append(" {execution: ").append(executionId).append('}');
171         return buffer.toString();
172     }
173 
174     public String getGroupId() {
175         if (mojoDescriptor != null) {
176             return mojoDescriptor.getPluginDescriptor().getGroupId();
177         }
178 
179         return plugin.getGroupId();
180     }
181 
182     public String getArtifactId() {
183         if (mojoDescriptor != null) {
184             return mojoDescriptor.getPluginDescriptor().getArtifactId();
185         }
186 
187         return plugin.getArtifactId();
188     }
189 
190     public String getVersion() {
191         if (mojoDescriptor != null) {
192             return mojoDescriptor.getPluginDescriptor().getVersion();
193         }
194 
195         return plugin.getVersion();
196     }
197 
198     public String getGoal() {
199         if (mojoDescriptor != null) {
200             return mojoDescriptor.getGoal();
201         }
202 
203         return goal;
204     }
205 
206     public void setMojoDescriptor(MojoDescriptor mojoDescriptor) {
207         this.mojoDescriptor = mojoDescriptor;
208     }
209 
210     public Map<String, List<MojoExecution>> getForkedExecutions() {
211         return forkedExecutions;
212     }
213 
214     public void setForkedExecutions(String projectKey, List<MojoExecution> forkedExecutions) {
215         this.forkedExecutions.put(projectKey, forkedExecutions);
216     }
217 }