package org.apache.maven;
/* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.apache.maven.jelly.MavenJellyContext;
import org.apache.maven.plugin.PluginManager;
import org.apache.maven.project.Project;
/**
* This class should represent the single place to access everything that
* MavenSession provides and control any options provided by MavenSession. Currently
* this is not entirely the case: there are bits of logic that need to
* be moved out of App.java and some code that needs to be shuffled
* between the PluginManager and MavenSession proper.
*
* @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
* @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
*
* @version $Id: MavenSession.java 517014 2007-03-11 21:15:50Z ltheussl $
*
* @todo Filter the MavenSession version number into a Java constant that will change
* during compilation.
*/
// This is the mediator and main point of entry for the guts of maven.
public class MavenSession
extends AbstractMavenComponent
{
// ------------------------------------------------------------
// C O N S T A N T S
// ------------------------------------------------------------
/** Tag for build start goal. */
public static final String BUILD_START_GOAL = "build:start";
/** Tag for build end goal. */
public static final String BUILD_END_GOAL = "build:end";
/** Initialization jellyscript name. */
public static final String DRIVER_SCRIPT_NAME = "driver.jelly";
// ------------------------------------------------------------
// C L A S S M E M B E R S
// ------------------------------------------------------------
/** globals across projects */
private MavenJellyContext rootContext;
/** top level project file */
private static File rootDescriptorFile;
/** top level project */
private Project rootProject;
/** plugin manager across projects */
private PluginManager pluginManager;
/** Default Constructor. */
public MavenSession()
{
pluginManager = new PluginManager( this );
//goalNames.add( BUILD_START_GOAL );
}
// ------------------------------------------------------------
// A C C E S S O R S
// ------------------------------------------------------------
/**
* Set the context for the session
* @param context the context to use
*/
public void setRootContext( MavenJellyContext context )
{
rootContext = context;
}
/**
* @return the root context for the session
*/
public MavenJellyContext getRootContext()
{
return rootContext;
}
/**
* Set the top level project for the session
* @param descriptorFile the POM for the top level project
*/
public static void setRootDescriptorFile( File descriptorFile )
{
rootDescriptorFile = descriptorFile;
}
/**
* @return the top level project for the session
*/
public static File getRootDescriptorFile()
{
return rootDescriptorFile;
}
/**
* Set the top level project object for the session
* @param project the top level project
*/
public void setRootProject( Project project )
{
rootProject = project;
}
/**
* @return the top level project object for the session
*/
public Project getRootProject()
{
return rootProject;
}
/**
* Set the plugin manager to handle plugin loading for the session
* @param pluginManager a plugin manager
*/
public void setPluginManager( PluginManager pluginManager )
{
this.pluginManager = pluginManager;
}
/**
* @return the plugin manager for the session
*/
public PluginManager getPluginManager()
{
return pluginManager;
}
// ------------------------------------------------------------
// I M P L E M E N T A T I O N
// ------------------------------------------------------------
/** Perform pre-build initialization.
*
* @throws Exception If an error occurs while performing
* runtime initialization.
*/
public void initialize()
throws Exception
{
// THIS NEEDS TO BE SET
getRootContext().setMavenSession( this );
initializePluginManager();
initializeRootProject();
}
/**
* Return all goals requested to be attained.
*
* @return All goal names.
*/
public Set getAllGoalNames()
{
return getPluginManager().getGoalNames();
}
/**
* Get a given goal's description.
*
* @param goalName Goal name to get the description for.
* @return Goal description.
*/
public String getGoalDescription( String goalName )
{
return getPluginManager().getGoalDescription( goalName );
}
/** Initialize all plugins.
*
* @throws Exception If an error occurs while initializing
* any plugin.
*/
private void initializePluginManager()
throws Exception
{
getPluginManager().initialize();
}
/**
* Load the mavenSession project descriptor.
*
* @throws MavenException If there is an error while
* reading the project descriptor.
*/
private void initializeRootProject()
throws MavenException
{
File descriptorFile = getRootDescriptorFile();
if ( descriptorFile.exists() )
{
if ( descriptorFile.length() == 0 )
{
throw new MavenException( MavenUtils.getMessage( "empty.descriptor.error", descriptorFile.getName() ) );
}
// Now we want to use the root context as the parent context because
// it has all the global values needed by all projects. The root context
// created within MavenUtils.getProject(f) is almost identical to the root context
// because in both cases they are created with MavenUtils.createContext(d)
// but the root context has addition values set from from values based
// on options present on the command line.
setRootProject( MavenUtils.getProject( descriptorFile, getRootContext() ) );
}
else
{
// A type of null pattern. We don't really have a root project per se
// but want the rest of the system to believe there is for consistency
// in the rest of the code.
Project globalProject = new Project();
globalProject.setId( "Global Project" );
// even though it doesn't exist, we need something to
globalProject.setFile( descriptorFile );
// We need to set the project of the root context so the plugin manager
// has a project to run jelly scripts against.
getRootContext().setProject( globalProject );
globalProject.setContext( getRootContext() );
setRootProject( globalProject );
}
}
/**
* Attain a list of goals.
*
* @param project the project being processed
* @param goals a list of goal names to execute
* @throws Exception when anything goes wrong
* @todo don't throw exception
*/
public void attainGoals( final Project project, final List goals )
throws Exception
{
pluginManager.attainGoals( project, goals );
}
/**
* Returns a plugin project for the given goal.
*
* @param goal a goal name
* @return Project
* @throws MavenException when anything goes wrong
*/
public Project getPluginProjectFromGoal( String goal )
throws MavenException
{
return pluginManager.getPluginProjectFromGoal( goal );
}
/**
* Return the goals for the given project.
*
* @param project A project
* @return A set of goals
* @throws MavenException when anything goes wrong
*/
public Set getProjectGoals( Project project )
throws MavenException
{
return pluginManager.getGoalNames( project );
}
/**
* Return the current list of plugins.
*
* @return Collection
*/
public Collection getPluginList()
{
return pluginManager.getPluginList();
}
}