001    package org.apache.maven.plugin;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.LinkedHashMap;
023    import java.util.List;
024    import java.util.Map;
025    
026    import org.apache.maven.model.Plugin;
027    import org.apache.maven.plugin.descriptor.MojoDescriptor;
028    import org.codehaus.plexus.util.xml.Xpp3Dom;
029    
030    public class MojoExecution
031    {
032    
033        private Plugin plugin;
034        
035        private String goal;
036        
037        private String executionId;
038        
039        private MojoDescriptor mojoDescriptor;
040    
041        private Xpp3Dom configuration;
042    
043        /**
044         * Describes the source of an execution.
045         */
046        public enum Source
047        {
048    
049            /**
050             * An execution that originates from the direct invocation of a goal from the CLI.
051             */
052            CLI,
053    
054            /**
055             * An execution that originates from a goal bound to a lifecycle phase.
056             */
057            LIFECYCLE,
058        }
059    
060        private Source source;
061    
062        /**
063         * The phase may or may not have been bound to a phase but once the plan has been calculated we know what phase
064         * this mojo execution is going to run in.
065         */
066        private String lifecyclePhase;
067    
068        /**
069         * The executions to fork before this execution, indexed by the groupId:artifactId:version of the project on which
070         * the forked execution are to be run and in reactor build order.
071         */
072        private Map<String, List<MojoExecution>> forkedExecutions = new LinkedHashMap<String, List<MojoExecution>>();
073    
074        public MojoExecution( Plugin plugin, String goal, String executionId )
075        {
076            this.plugin = plugin;
077            this.goal = goal;
078            this.executionId = executionId;
079        }
080        
081        public MojoExecution( MojoDescriptor mojoDescriptor )
082        {
083            this.mojoDescriptor = mojoDescriptor;
084            this.executionId = null;
085            this.configuration = null;
086        }
087    
088        public MojoExecution( MojoDescriptor mojoDescriptor, String executionId, Source source )
089        {
090            this.mojoDescriptor = mojoDescriptor;
091            this.executionId = executionId;
092            this.configuration = null;
093            this.source = source;
094        }
095    
096        public MojoExecution( MojoDescriptor mojoDescriptor, String executionId )
097        {
098            this.mojoDescriptor = mojoDescriptor;
099            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    }