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