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.model.Plugin;
27  import org.apache.maven.plugin.descriptor.MojoDescriptor;
28  import org.codehaus.plexus.util.xml.Xpp3Dom;
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 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, Xpp3Dom configuration )
107     {
108         this.mojoDescriptor = mojoDescriptor;
109         this.configuration = configuration;
110         this.executionId = null;
111     }
112 
113     /**
114      * Gets the source of this execution.
115      *
116      * @return The source of this execution or {@code null} if unknown.
117      */
118     public Source getSource()
119     {
120         return source;
121     }
122 
123     public String getExecutionId()
124     {
125         return executionId;
126     }
127 
128     public Plugin getPlugin()
129     {
130         if ( mojoDescriptor != null )
131         {
132             return mojoDescriptor.getPluginDescriptor().getPlugin();
133         }
134 
135         return plugin;
136     }
137 
138     public MojoDescriptor getMojoDescriptor()
139     {
140         return mojoDescriptor;
141     }
142 
143     public Xpp3Dom getConfiguration()
144     {
145         return configuration;
146     }
147 
148     public void setConfiguration( Xpp3Dom configuration )
149     {
150         this.configuration = configuration;
151     }
152 
153     public String identify()
154     {
155         StringBuilder sb = new StringBuilder( 256 );
156 
157         sb.append( executionId );
158         sb.append( configuration.toString() );
159 
160         return sb.toString();
161     }
162 
163     public String getLifecyclePhase()
164     {
165         return lifecyclePhase;
166     }
167 
168     public void setLifecyclePhase( String lifecyclePhase )
169     {
170         this.lifecyclePhase = lifecyclePhase;
171     }
172 
173     @Override
174     public String toString()
175     {
176         StringBuilder buffer = new StringBuilder( 128 );
177         if ( mojoDescriptor != null )
178         {
179             buffer.append( mojoDescriptor.getId() );
180         }
181         buffer.append( " {execution: " ).append( executionId ).append( '}' );
182         return buffer.toString();
183     }
184 
185     public String getGroupId()
186     {
187         if ( mojoDescriptor != null )
188         {
189             return mojoDescriptor.getPluginDescriptor().getGroupId();
190         }
191 
192         return plugin.getGroupId();
193     }
194 
195     public String getArtifactId()
196     {
197         if ( mojoDescriptor != null )
198         {
199             return mojoDescriptor.getPluginDescriptor().getArtifactId();
200         }
201 
202         return plugin.getArtifactId();
203     }
204 
205     public String getVersion()
206     {
207         if ( mojoDescriptor != null )
208         {
209             return mojoDescriptor.getPluginDescriptor().getVersion();
210         }
211 
212         return plugin.getVersion();
213     }
214 
215     public String getGoal()
216     {
217         if ( mojoDescriptor != null )
218         {
219             return mojoDescriptor.getGoal();
220         }
221 
222         return goal;
223     }
224 
225     public void setMojoDescriptor( MojoDescriptor mojoDescriptor )
226     {
227         this.mojoDescriptor = mojoDescriptor;
228     }
229 
230     public Map<String, List<MojoExecution>> getForkedExecutions()
231     {
232         return forkedExecutions;
233     }
234 
235     public void setForkedExecutions( String projectKey, List<MojoExecution> forkedExecutions )
236     {
237         this.forkedExecutions.put( projectKey, forkedExecutions );
238     }
239 
240 }