1 package org.apache.maven;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.io.File;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.Set;
25
26 import org.apache.maven.jelly.MavenJellyContext;
27 import org.apache.maven.plugin.PluginManager;
28 import org.apache.maven.project.Project;
29
30 /**
31 * This class should represent the single place to access everything that
32 * MavenSession provides and control any options provided by MavenSession. Currently
33 * this is not entirely the case: there are bits of logic that need to
34 * be moved out of App.java and some code that needs to be shuffled
35 * between the PluginManager and MavenSession proper.
36 *
37 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
38 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
39 *
40 * @version $Id: MavenSession.java 517014 2007-03-11 21:15:50Z ltheussl $
41 *
42 * @todo Filter the MavenSession version number into a Java constant that will change
43 * during compilation.
44 */
45
46
47 public class MavenSession
48 extends AbstractMavenComponent
49 {
50
51
52
53
54 /** Tag for build start goal. */
55 public static final String BUILD_START_GOAL = "build:start";
56
57 /** Tag for build end goal. */
58 public static final String BUILD_END_GOAL = "build:end";
59
60 /** Initialization jellyscript name. */
61 public static final String DRIVER_SCRIPT_NAME = "driver.jelly";
62
63
64
65
66
67 /** globals across projects */
68 private MavenJellyContext rootContext;
69
70 /** top level project file */
71 private static File rootDescriptorFile;
72
73 /** top level project */
74 private Project rootProject;
75
76 /** plugin manager across projects */
77 private PluginManager pluginManager;
78
79 /** Default Constructor. */
80 public MavenSession()
81 {
82 pluginManager = new PluginManager( this );
83
84 }
85
86
87
88
89
90 /**
91 * Set the context for the session
92 * @param context the context to use
93 */
94 public void setRootContext( MavenJellyContext context )
95 {
96 rootContext = context;
97 }
98
99 /**
100 * @return the root context for the session
101 */
102 public MavenJellyContext getRootContext()
103 {
104 return rootContext;
105 }
106
107 /**
108 * Set the top level project for the session
109 * @param descriptorFile the POM for the top level project
110 */
111 public static void setRootDescriptorFile( File descriptorFile )
112 {
113 rootDescriptorFile = descriptorFile;
114 }
115
116 /**
117 * @return the top level project for the session
118 */
119 public static File getRootDescriptorFile()
120 {
121 return rootDescriptorFile;
122 }
123
124 /**
125 * Set the top level project object for the session
126 * @param project the top level project
127 */
128 public void setRootProject( Project project )
129 {
130 rootProject = project;
131 }
132
133 /**
134 * @return the top level project object for the session
135 */
136 public Project getRootProject()
137 {
138 return rootProject;
139 }
140
141 /**
142 * Set the plugin manager to handle plugin loading for the session
143 * @param pluginManager a plugin manager
144 */
145 public void setPluginManager( PluginManager pluginManager )
146 {
147 this.pluginManager = pluginManager;
148 }
149
150 /**
151 * @return the plugin manager for the session
152 */
153 public PluginManager getPluginManager()
154 {
155 return pluginManager;
156 }
157
158
159
160
161
162 /** Perform pre-build initialization.
163 *
164 * @throws Exception If an error occurs while performing
165 * runtime initialization.
166 */
167 public void initialize()
168 throws Exception
169 {
170
171 getRootContext().setMavenSession( this );
172
173 initializePluginManager();
174 initializeRootProject();
175 }
176
177 /**
178 * Return all goals requested to be attained.
179 *
180 * @return All goal names.
181 */
182 public Set getAllGoalNames()
183 {
184 return getPluginManager().getGoalNames();
185 }
186
187 /**
188 * Get a given goal's description.
189 *
190 * @param goalName Goal name to get the description for.
191 * @return Goal description.
192 */
193 public String getGoalDescription( String goalName )
194 {
195 return getPluginManager().getGoalDescription( goalName );
196 }
197
198 /** Initialize all plugins.
199 *
200 * @throws Exception If an error occurs while initializing
201 * any plugin.
202 */
203 private void initializePluginManager()
204 throws Exception
205 {
206 getPluginManager().initialize();
207 }
208
209 /**
210 * Load the mavenSession project descriptor.
211 *
212 * @throws MavenException If there is an error while
213 * reading the project descriptor.
214 */
215 private void initializeRootProject()
216 throws MavenException
217 {
218 File descriptorFile = getRootDescriptorFile();
219
220 if ( descriptorFile.exists() )
221 {
222 if ( descriptorFile.length() == 0 )
223 {
224 throw new MavenException( MavenUtils.getMessage( "empty.descriptor.error", descriptorFile.getName() ) );
225 }
226
227
228
229
230
231
232
233 setRootProject( MavenUtils.getProject( descriptorFile, getRootContext() ) );
234 }
235 else
236 {
237
238
239
240 Project globalProject = new Project();
241 globalProject.setId( "Global Project" );
242
243 globalProject.setFile( descriptorFile );
244
245
246
247 getRootContext().setProject( globalProject );
248
249 globalProject.setContext( getRootContext() );
250 setRootProject( globalProject );
251 }
252 }
253
254 /**
255 * Attain a list of goals.
256 *
257 * @param project the project being processed
258 * @param goals a list of goal names to execute
259 * @throws Exception when anything goes wrong
260 * @todo don't throw exception
261 */
262 public void attainGoals( final Project project, final List goals )
263 throws Exception
264 {
265 pluginManager.attainGoals( project, goals );
266 }
267
268 /**
269 * Returns a plugin project for the given goal.
270 *
271 * @param goal a goal name
272 * @return Project
273 * @throws MavenException when anything goes wrong
274 */
275 public Project getPluginProjectFromGoal( String goal )
276 throws MavenException
277 {
278 return pluginManager.getPluginProjectFromGoal( goal );
279 }
280
281 /**
282 * Return the goals for the given project.
283 *
284 * @param project A project
285 * @return A set of goals
286 * @throws MavenException when anything goes wrong
287 */
288 public Set getProjectGoals( Project project )
289 throws MavenException
290 {
291 return pluginManager.getGoalNames( project );
292 }
293
294 /**
295 * Return the current list of plugins.
296 *
297 * @return Collection
298 */
299 public Collection getPluginList()
300 {
301 return pluginManager.getPluginList();
302 }
303 }