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  public class MojoExecution
31  {
32  
33      private Plugin plugin;
34  
35      private String goal;
36  
37      private String executionId;
38  
39      private MojoDescriptor mojoDescriptor;
40  
41      private Xpp3Dom configuration;
42  
43      /**
44       * Describes the source of an execution.
45       */
46      public enum Source
47      {
48  
49          /**
50           * An execution that originates from the direct invocation of a goal from the CLI.
51           */
52          CLI,
53  
54          /**
55           * An execution that originates from a goal bound to a lifecycle phase.
56           */
57          LIFECYCLE,
58      }
59  
60      private Source source = Source.LIFECYCLE;
61  
62      /**
63       * The phase may or may not have been bound to a phase but once the plan has been calculated we know what phase
64       * this mojo execution is going to run in.
65       */
66      private String lifecyclePhase;
67  
68      /**
69       * The executions to fork before this execution, indexed by the groupId:artifactId:version of the project on which
70       * the forked execution are to be run and in reactor build order.
71       */
72      private Map<String, List<MojoExecution>> forkedExecutions = new LinkedHashMap<>();
73  
74      public MojoExecution( Plugin plugin, String goal, String executionId )
75      {
76          this.plugin = plugin;
77          this.goal = goal;
78          this.executionId = executionId;
79      }
80  
81      public MojoExecution( MojoDescriptor mojoDescriptor )
82      {
83          this.mojoDescriptor = mojoDescriptor;
84          this.executionId = null;
85          this.configuration = null;
86      }
87  
88      public MojoExecution( MojoDescriptor mojoDescriptor, String executionId, Source source )
89      {
90          this.mojoDescriptor = mojoDescriptor;
91          this.executionId = executionId;
92          this.configuration = null;
93          this.source = source;
94      }
95  
96      public MojoExecution( MojoDescriptor mojoDescriptor, String executionId )
97      {
98          this.mojoDescriptor = mojoDescriptor;
99          this.executionId = executionId;
100         this.configuration = null;
101     }
102 
103     public MojoExecution( MojoDescriptor mojoDescriptor, Xpp3Dom configuration )
104     {
105         this.mojoDescriptor = mojoDescriptor;
106         this.configuration = 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     {
117         return source;
118     }
119 
120     public String getExecutionId()
121     {
122         return executionId;
123     }
124 
125     public Plugin getPlugin()
126     {
127         if ( mojoDescriptor != null )
128         {
129             return mojoDescriptor.getPluginDescriptor().getPlugin();
130         }
131 
132         return plugin;
133     }
134 
135     public MojoDescriptor getMojoDescriptor()
136     {
137         return mojoDescriptor;
138     }
139 
140     public Xpp3Dom getConfiguration()
141     {
142         return configuration;
143     }
144 
145     public void setConfiguration( Xpp3Dom configuration )
146     {
147         this.configuration = configuration;
148     }
149 
150     public String identify()
151     {
152         StringBuilder sb = new StringBuilder( 256 );
153 
154         sb.append( executionId );
155         sb.append( configuration.toString() );
156 
157         return sb.toString();
158     }
159 
160     public String getLifecyclePhase()
161     {
162         return lifecyclePhase;
163     }
164 
165     public void setLifecyclePhase( String lifecyclePhase )
166     {
167         this.lifecyclePhase = lifecyclePhase;
168     }
169 
170     @Override
171     public String toString()
172     {
173         StringBuilder buffer = new StringBuilder( 128 );
174         if ( mojoDescriptor != null )
175         {
176             buffer.append( mojoDescriptor.getId() );
177         }
178         buffer.append( " {execution: " ).append( executionId ).append( '}' );
179         return buffer.toString();
180     }
181 
182     public String getGroupId()
183     {
184         if ( mojoDescriptor != null )
185         {
186             return mojoDescriptor.getPluginDescriptor().getGroupId();
187         }
188 
189         return plugin.getGroupId();
190     }
191 
192     public String getArtifactId()
193     {
194         if ( mojoDescriptor != null )
195         {
196             return mojoDescriptor.getPluginDescriptor().getArtifactId();
197         }
198 
199         return plugin.getArtifactId();
200     }
201 
202     public String getVersion()
203     {
204         if ( mojoDescriptor != null )
205         {
206             return mojoDescriptor.getPluginDescriptor().getVersion();
207         }
208 
209         return plugin.getVersion();
210     }
211 
212     public String getGoal()
213     {
214         if ( mojoDescriptor != null )
215         {
216             return mojoDescriptor.getGoal();
217         }
218 
219         return goal;
220     }
221 
222     public void setMojoDescriptor( MojoDescriptor mojoDescriptor )
223     {
224         this.mojoDescriptor = mojoDescriptor;
225     }
226 
227     public Map<String, List<MojoExecution>> getForkedExecutions()
228     {
229         return forkedExecutions;
230     }
231 
232     public void setForkedExecutions( String projectKey, List<MojoExecution> forkedExecutions )
233     {
234         this.forkedExecutions.put( projectKey, forkedExecutions );
235     }
236 
237 }