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