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  
25  import org.apache.maven.model.Plugin;
26  import org.apache.maven.plugin.descriptor.MojoDescriptor;
27  import org.codehaus.plexus.util.xml.Xpp3Dom;
28  
29  /**
30   * MojoExecution
31   */
32  public class MojoExecution {
33  
34      private Plugin plugin;
35  
36      private String goal;
37  
38      private String executionId;
39  
40      private MojoDescriptor mojoDescriptor;
41  
42      private Xpp3Dom configuration;
43  
44      /**
45       * Describes the source of an execution.
46       */
47      public enum Source {
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          this.plugin = plugin;
76          this.goal = goal;
77          this.executionId = executionId;
78      }
79  
80      public MojoExecution(MojoDescriptor mojoDescriptor) {
81          this.mojoDescriptor = mojoDescriptor;
82          this.executionId = null;
83          this.configuration = null;
84      }
85  
86      public MojoExecution(MojoDescriptor mojoDescriptor, String executionId, Source source) {
87          this.mojoDescriptor = mojoDescriptor;
88          this.executionId = executionId;
89          this.configuration = null;
90          this.source = source;
91      }
92  
93      public MojoExecution(MojoDescriptor mojoDescriptor, String executionId) {
94          this.mojoDescriptor = mojoDescriptor;
95          this.executionId = executionId;
96          this.configuration = null;
97      }
98  
99      public MojoExecution(MojoDescriptor mojoDescriptor, Xpp3Dom configuration) {
100         this.mojoDescriptor = mojoDescriptor;
101         this.configuration = configuration;
102         this.executionId = null;
103     }
104 
105     /**
106      * Gets the source of this execution.
107      *
108      * @return The source of this execution or {@code null} if unknown.
109      */
110     public Source getSource() {
111         return source;
112     }
113 
114     public String getExecutionId() {
115         return executionId;
116     }
117 
118     public Plugin getPlugin() {
119         if (mojoDescriptor != null) {
120             return mojoDescriptor.getPluginDescriptor().getPlugin();
121         }
122 
123         return plugin;
124     }
125 
126     public MojoDescriptor getMojoDescriptor() {
127         return mojoDescriptor;
128     }
129 
130     public Xpp3Dom getConfiguration() {
131         return configuration;
132     }
133 
134     public void setConfiguration(Xpp3Dom configuration) {
135         this.configuration = configuration;
136     }
137 
138     public String identify() {
139         StringBuilder sb = new StringBuilder(256);
140 
141         sb.append(executionId);
142         sb.append(configuration.toString());
143 
144         return sb.toString();
145     }
146 
147     public String getLifecyclePhase() {
148         return lifecyclePhase;
149     }
150 
151     public void setLifecyclePhase(String lifecyclePhase) {
152         this.lifecyclePhase = lifecyclePhase;
153     }
154 
155     @Override
156     public String toString() {
157         StringBuilder buffer = new StringBuilder(128);
158         if (mojoDescriptor != null) {
159             buffer.append(mojoDescriptor.getId());
160         }
161         buffer.append(" {execution: ").append(executionId).append('}');
162         return buffer.toString();
163     }
164 
165     public String getGroupId() {
166         if (mojoDescriptor != null) {
167             return mojoDescriptor.getPluginDescriptor().getGroupId();
168         }
169 
170         return plugin.getGroupId();
171     }
172 
173     public String getArtifactId() {
174         if (mojoDescriptor != null) {
175             return mojoDescriptor.getPluginDescriptor().getArtifactId();
176         }
177 
178         return plugin.getArtifactId();
179     }
180 
181     public String getVersion() {
182         if (mojoDescriptor != null) {
183             return mojoDescriptor.getPluginDescriptor().getVersion();
184         }
185 
186         return plugin.getVersion();
187     }
188 
189     public String getGoal() {
190         if (mojoDescriptor != null) {
191             return mojoDescriptor.getGoal();
192         }
193 
194         return goal;
195     }
196 
197     public void setMojoDescriptor(MojoDescriptor mojoDescriptor) {
198         this.mojoDescriptor = mojoDescriptor;
199     }
200 
201     public Map<String, List<MojoExecution>> getForkedExecutions() {
202         return forkedExecutions;
203     }
204 
205     public void setForkedExecutions(String projectKey, List<MojoExecution> forkedExecutions) {
206         this.forkedExecutions.put(projectKey, forkedExecutions);
207     }
208 }