jvanzyl 2003/01/12 18:34:32
Modified: src/java/org/apache/maven/plugin PluginManager.java
Log:
o Fixing the processing of user defined jelly and the overriding of goals.
Revision Changes Path
1.30 +257 -335
jakarta-turbine-maven/src/java/org/apache/maven/plugin/PluginManager.java
Index: PluginManager.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/plugin/PluginManager.java,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -r1.29 -r1.30
--- PluginManager.java 12 Jan 2003 07:41:06 -0000 1.29
+++ PluginManager.java 13 Jan 2003 02:34:31 -0000 1.30
@@ -56,16 +56,18 @@
* ====================================================================
*/
+import com.werken.forehead.Forehead;
import com.werken.werkz.Goal;
import com.werken.werkz.Session;
import com.werken.werkz.jelly.JellySession;
-import com.werken.forehead.Forehead;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.maven.AbstractMavenComponent;
import org.apache.maven.MavenException;
-import org.apache.maven.MavenUtils;
import org.apache.maven.MavenSession;
+import org.apache.maven.MavenUtils;
import org.apache.maven.UnknownGoalException;
import org.apache.maven.jelly.JellyUtils;
import org.apache.maven.jelly.MavenJellyContext;
@@ -76,7 +78,6 @@
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
@@ -88,8 +89,6 @@
import java.util.Set;
import java.util.StringTokenizer;
-import org.apache.commons.lang.StringUtils;
-
/*
NOTES:
@@ -137,12 +136,6 @@
/** Plug-in default properties name. */
public static final String PLUGIN_PROPERTIES_NAME = "plugin.properties";
- /** Plug-in descriptor. */
- public static final String PLUGIN_DESCRIPTOR = "plugin.xml";
-
- /** Plug-in directory property tag */
- public static final String PLUGIN_DIR = "maven.plugin.dir";
-
/** Logger */
private static final Log log = LogFactory.getLog( PluginManager.class );
@@ -150,12 +143,6 @@
private File pluginsDir;
/**
- * Goals defined by users in maven.xml files. These may override goals defined
in
- * plugins.
- */
- private Properties userDefinedGoalsCache;
-
- /**
* This contains a set of sets where each set contains a list of
* plugins that have been loaded for a particular project.
*/
@@ -168,28 +155,70 @@
private MavenSession mavenSession;
/** Plugin cache manager. */
- private PluginCacheManager cacheManager = new PluginCacheManager();
+ private PluginCacheManager cacheManager;
+
+ /** Cache manager for user jelly scripts. */
+ private PluginCacheManager transientCacheManager;
/**
- * Constructor.
+ * Default constructor.
+ *
+ * @param mavenSession The MavenSession this plugin manager will use
+ * until Maven shuts down.
*/
public PluginManager( MavenSession mavenSession )
{
this.mavenSession = mavenSession;
+
loadedPlugins = new HashSet();
- userDefinedGoalsCache = new Properties();
+ cacheManager = new PluginCacheManager();
+ transientCacheManager = new PluginCacheManager();
}
- public boolean isInitialized()
+ // ----------------------------------------------------------------------
+ // A C C E S S O R S
+ // ----------------------------------------------------------------------
+
+ /**
+ * Retrieve the set of all goal names.
+ *
+ * @return The set of <code>String</code> goal names.
+ */
+ public Set getGoalNames()
{
- return initialized;
+ return cacheManager.getGoalCache().keySet();
}
+ /**
+ * Retrieve a goal description by the goal name.
+ *
+ * @param name THe goal name.
+ *
+ * @return The description or <code>null</code> if no
+ * description has been set.
+ */
+ public String getGoalDescription( String name )
+ {
+ String value = cacheManager.getGoalCache().getProperty( name );
+
+ if ( value == null )
+ {
+ return null;
+ }
+
+ String description = value.substring( 0, value.indexOf( ">" ) );
+
+ return description;
+ }
+
+ // ----------------------------------------------------------------------
+ // P U B L I C A P I
+ // ----------------------------------------------------------------------
/**
- * Initialize all plugins.
+ * Initialize all plugins.
*
- * @throws Exception If an error occurs while initializing any plugin.
+ * @throws Exception If an error occurs while initializing any plugin.
*/
public void initialize()
throws Exception
@@ -231,14 +260,14 @@
processed.delete();
}
- try
+ try
{
Expand unzipper = new Expand();
unzipper.setSrc( files[i] );
unzipper.setDest( unzipDir );
unzipper.execute();
}
- catch (IOException e)
+ catch (IOException e)
{
throw new MavenException("Unable to extract plugin: " +
files[i], e);
}
@@ -282,50 +311,78 @@
log.info( "Finished initializing Plugins!" );
}
- /**
- * Retrieve the set of all goal names.
- *
- * @return The set of <code>String</code> goal names.
- */
- public Set getGoalNames()
- {
- return cacheManager.getGoalCache().keySet();
- }
/**
- * Retrieve a goal description by the goal name.
+ * Attain the goals.
*
- * @param name THe goal name.
- *
- * @return The description or <code>null</code> if no
- * description has been set.
+ * @throws org.apache.maven.UnknownGoalException If one of the specified
+ * goals refers to an non-existent goal.
+ * @throws Exception If an exception occurs while running a goal.
*/
- public String getGoalDescription( String name )
+ public void attainGoals( Project project )
+ throws UnknownGoalException, Exception
{
- String value = cacheManager.getGoalCache().getProperty( name );
+ // Before attempting to attain the goals verify the project
+ // if desired.
+ project.verifyDependencies();
- if ( value == null )
+ // Currently we will not attempt to load a maven.xml file when the
project.xml
+ // file is not present.
+ if ( project.getFile() != null )
{
- return null;
+ File mavenXmlFile = new File( project.getFile().getParentFile(),
"maven.xml" );
+ loadJellyScript( mavenXmlFile, project );
}
- String description = value.substring( 0, value.indexOf( ">" ) );
+ // There will always be at least one goal present which is the build:start
+ // goal. If this is the only goal present then we want to add either the
+ // default goal specified in the project.xml file, or the default goal
+ // specified in the driver.properties file.
- return description;
+ //if ( goalNames.size() == 1 )
+ if ( project.getGoalNames().size() == 0 )
+ {
+ String defaultGoalName =
project.getContext().getWerkzProject().getDefaultGoalName();
+
+ if ( defaultGoalName != null )
+ {
+ project.getGoalNames().add( defaultGoalName );
+ }
+ }
+
+ //project.getGoalNames().add( BUILD_END_GOAL );
+
+ for ( Iterator i = project.getGoalNames().iterator(); i.hasNext(); )
+ {
+ String goalName = (String) i.next();
+ prepForGoal( goalName, project );
+ Goal eachGoal = project.getContext().getWerkzProject().getGoal(
goalName );
+
+ if ( eachGoal == null )
+ {
+ throw new UnknownGoalException( goalName );
+ }
+ }
+
+ Session session = new JellySession( project.getContext().getXMLOutput() );
+ Thread.currentThread().setContextClassLoader( null );
+
+ for ( Iterator i = project.getGoalNames().iterator(); i.hasNext(); )
+ {
+ String eachGoalName = (String) i.next();
+ Goal eachGoal = project.getContext().getWerkzProject().getGoal(
eachGoalName );
+ eachGoal.attain( session );
+ }
}
/**
- * Load the project build file (maven.xml).
+ * Load a user jelly script. This may be a maven.xml file for a project or
+ * a shared jelly script used by the reactor.
*
- * @param projectBuildFile The project build file.
+ * @param jellyScript The jelly.
*
- * @throws Exception If an error occurs while attempting to load the file.
+ * @throws Exception If an error occurs while attempting to load the file.
*/
-
- // This method should not be here and is currently used from the Project class.
-
- PluginCacheManager transientCacheManager = new PluginCacheManager();
-
public void loadJellyScript( File jellyScript, Project project )
throws Exception
{
@@ -334,9 +391,6 @@
return;
}
- // We need to make sure that any goals specified in jelly scripts loaded
- // after the plugin override any goals specified in the plugins.
-
Set originalGoals = new HashSet(
project.getContext().getWerkzProject().getGoals() );
for ( Iterator i = originalGoals.iterator(); i.hasNext(); )
@@ -349,10 +403,9 @@
}
}
- // The goals seem to be picked up correctly, but dependent tag libs are not
- // being loaded.
transientCacheManager.setPluginScript( jellyScript );
transientCacheManager.parse();
+
for ( Iterator i = transientCacheManager.getDynaTagLibDecls().iterator();
i.hasNext();)
{
prepDynamicTagLib( (String) i.next(), project );
@@ -365,105 +418,66 @@
project.getFile().getParentFile().toURL(),
project.getContext(),
project.getContext().getXMLOutput() );
-
- // Now after running the user defined maven.xml file we might have some
- // new goals. These may be goals that override plugins goals or simply
- // new goals that the user defined.
-
- Set currentGoals = new HashSet(
project.getContext().getWerkzProject().getGoals() );
-
- currentGoals.removeAll( originalGoals );
-
- for ( Iterator i = currentGoals.iterator(); i.hasNext(); )
- {
- Goal eachGoal = (Goal) i.next();
-
- if ( eachGoal.getAction() == null )
- {
- continue;
- }
-
- if ( originalGoals.contains( eachGoal ) )
- {
- continue;
- }
-
- StringBuffer prereqSpec = new StringBuffer();
-
- for ( Iterator j = eachGoal.getPrecursors().iterator(); j.hasNext(); )
- {
- Goal eachPrereq = (Goal) j.next();
-
- prereqSpec.append( eachPrereq.getName() );
-
- if ( j.hasNext() )
- {
- prereqSpec.append( " " );
- }
- }
-
- String desc = eachGoal.getDescription();
-
- if ( desc == null )
- {
- userDefinedGoalsCache.setProperty( eachGoal.getName(), ">" +
prereqSpec.toString() );
- }
- else
- {
- userDefinedGoalsCache.setProperty( eachGoal.getName(),
eachGoal.getDescription() + ">" + prereqSpec.toString() );
- }
- }
}
/**
- * Perform any required initialization to enable a goal
- * to be met.
+ * Perform any required initialization to enable a goal
+ * to be met.
*
- * @param name The goal name.
+ * @param initialGoalToPrep The initial goal to prepare for. There may
+ * also be additional goals to prep for once prequisite goals
+ * are taken into consideration.
*
* @throws Exception If an error occurs while attempting to
* prepare for the goal.
*/
- public void prepForGoal( String name, Project project )
+ public void prepForGoal( String initialGoalToPrep, Project project )
throws Exception
{
- LinkedList goals = new LinkedList();
+ LinkedList goalsToPrep = new LinkedList();
Set seen = new HashSet();
+ String goalToPrep = null;
- String goal = null;
- String pluginName = null;
-
- goals.add( name );
+ goalsToPrep.add( initialGoalToPrep );
- while ( goals.isEmpty() == false )
+ while ( goalsToPrep.isEmpty() == false )
{
- goal = (String) goals.removeFirst();
+ goalToPrep = (String) goalsToPrep.removeFirst();
- if ( seen.contains( goal ) )
+ if ( seen.contains( goalToPrep ) )
{
continue;
}
- seen.add( goal );
+ seen.add( goalToPrep );
- String spec = userDefinedGoalsCache.getProperty( goal );
+ // We check to see if the goalToPrep has been defined in any user
+ // jelly script. This allows any goal specified in a plugin to be
+ // overriden if a user desires.
+ String spec = transientCacheManager.getPluginCache().getProperty(
goalToPrep );
if ( spec == null )
{
- pluginName = cacheManager.getPluginCache().getProperty( goal );
+ spec = cacheManager.getPluginCache().getProperty( goalToPrep );
- if ( pluginName != null )
+ if ( spec != null )
{
- prepForCallbacks( goal, project );
- loadPlugin( pluginName, project );
+ prepForCallbacks( goalToPrep, project );
+ loadPlugin( spec, project );
}
}
- List prereqs = getPrereqs( goal );
- goals.addAll( prereqs );
+ // Find any prerequisite goals and add them to the list of
+ // goals to prepare.
+ List prereqs = getPrereqs( goalToPrep );
+ goalsToPrep.addAll( prereqs );
}
}
+ // ----------------------------------------------------------------------
+ // P R E P A R A T I O N M E T H O D S
+ // ----------------------------------------------------------------------
+
/**
* Prepare and load plugins based upon callback dependencies.
*
@@ -486,75 +500,13 @@
}
}
- /** Attain the goals.
- *
- * @throws org.apache.maven.UnknownGoalException If one of the specified
- * goals refers to an non-existent goal.
- * @throws Exception If an exception occurs while
- * running a goal.
- */
- public void attainGoals( Project project )
- throws UnknownGoalException, Exception
- {
- // Before attempting to attain the goals verify the project
- // if desired.
- project.verifyDependencies();
-
- // Currently we will not attempt to load a maven.xml file when the
project.xml
- // file is not present.
- if ( project.getFile() != null )
- {
- File mavenXmlFile = new File( project.getFile().getParentFile(),
"maven.xml" );
- loadJellyScript( mavenXmlFile, project );
- }
-
- // There will always be at least one goal present which is the build:start
- // goal. If this is the only goal present then we want to add either the
- // default goal specified in the project.xml file, or the default goal
- // specified in the driver.properties file.
-
- //if ( goalNames.size() == 1 )
- if ( project.getGoalNames().size() == 0 )
- {
- String defaultGoalName =
project.getContext().getWerkzProject().getDefaultGoalName();
-
- if ( defaultGoalName != null )
- {
- project.getGoalNames().add( defaultGoalName );
- }
- }
-
- //project.getGoalNames().add( BUILD_END_GOAL );
-
- for ( Iterator i = project.getGoalNames().iterator(); i.hasNext(); )
- {
- String goalName = (String) i.next();
- prepForGoal( goalName, project );
- Goal eachGoal = project.getContext().getWerkzProject().getGoal(
goalName );
-
- if ( eachGoal == null )
- {
- throw new UnknownGoalException( goalName );
- }
- }
-
- Session session = new JellySession( project.getContext().getXMLOutput() );
- Thread.currentThread().setContextClassLoader( null );
-
- for ( Iterator i = project.getGoalNames().iterator(); i.hasNext(); )
- {
- String eachGoalName = (String) i.next();
-
- Goal eachGoal = project.getContext().getWerkzProject().getGoal(
eachGoalName );
- eachGoal.attain( session );
- }
- }
-
/**
* Prepare the tag libary for use.
*
- * @param pluginName URI of the tag library to prepare.
- * @param project Project to load the dyna tag libs into.
+ * @param pluginName URI of the tag library to prepare.
+ * @param project Project to load the dyna tag libs into.
+ *
+ * @throws Exception if an error occurs while preparing dyna tag libs.
*/
void prepDynaTagLibs( String pluginName, Project project )
throws Exception
@@ -576,11 +528,12 @@
}
/**
- * Prepare an individual dyna tag lib for use.
+ * Prepare an individual dyna tag lib for use.
+ *
+ * @param uri URI of the dyna tag.
+ * @param project Project to load the dyna tag lib into.
*
- * @param uri URI of the dyna tag.
- * @param project Project to load the dyna tag lib into.
- * @throws Exception If an error occurs preparing the dynamic tag library.
+ * @throws Exception If an error occurs preparing the dynamic tag library.
*/
void prepDynamicTagLib( String uri, Project project )
throws Exception
@@ -599,10 +552,59 @@
}
/**
- * Load the specified plugin.
+ * Retrieve the prerequisites for a goal.
+ *
+ * @param name The goal name.
*
- * @param name The name of the plugin to load.
- * @throws Exception If an error occurs while initializing the plugin.
+ * @return A list of <code>String</code> prerequisite goal names.
+ */
+ List getPrereqs( String name )
+ {
+ String spec = transientCacheManager.getGoalCache().getProperty( name );
+
+ if ( spec == null )
+ {
+ spec = cacheManager.getGoalCache().getProperty( name );
+ }
+
+ if ( spec == null )
+ {
+ return Collections.EMPTY_LIST;
+ }
+
+ int splitLoc = spec.indexOf( ">" );
+
+ if ( ( splitLoc < 0 )
+ ||
+ ( splitLoc + 1 ) == spec.length() )
+ {
+ return Collections.EMPTY_LIST;
+ }
+
+ String prereqSpec = spec.substring( splitLoc + 1 );
+
+ StringTokenizer tokens = new StringTokenizer( prereqSpec, "," );
+
+ List prereqs = new ArrayList();
+
+ while ( tokens.hasMoreTokens() )
+ {
+ prereqs.add( tokens.nextToken() );
+ }
+
+ return prereqs;
+ }
+
+ // ----------------------------------------------------------------------
+ // P L U G I N L O A D I N G
+ // ----------------------------------------------------------------------
+
+ /**
+ * Load the specified plugin.
+ *
+ * @param name The name of the plugin to load.
+ *
+ * @throws Exception If an error occurs while initializing the plugin.
*/
void loadPlugin( String name, Project project )
throws Exception
@@ -635,7 +637,7 @@
pluginProject.verifyDependencies();
// Mark the plugin as processed.
- createNewFile( getPluginProcessedMarker( name ) );
+ FileUtils.fileWrite( getPluginProcessedMarker( name ).getPath(),
"plugin has been processed.");
}
for ( Iterator i = pluginProject.getArtifacts().iterator(); i.hasNext(); )
@@ -692,11 +694,31 @@
loadedPlugins.add( project.hashCode() + name );
}
+ /**
+ * Load plugins specified in a whitespace delimited string.
+ *
+ * @param names The whitespace delimited string of plugin names.
+ *
+ * @throws Exception If an error occurs while attempting to load
+ * the plugins.
+ */
+ void loadPlugins( String names, Project project )
+ throws Exception
+ {
+ StringTokenizer tokens = new StringTokenizer( names );
+
+ while ( tokens.hasMoreTokens() )
+ {
+ loadPlugin( tokens.nextToken(), project );
+ }
+ }
+
// ----------------------------------------------------------------------
// C A C H I N G
// ----------------------------------------------------------------------
- /** Cache a plugin.
+ /**
+ * Cache a plugin.
*
* @param name The plugin name.
*
@@ -718,11 +740,10 @@
}
}
-
/**
* Save cache information to disk.
*
- * @throws Exception If an error occurs while saving the cache.
+ * @throws Exception If an error occurs while saving the cache.
*/
void saveCache()
throws Exception
@@ -731,9 +752,9 @@
}
/**
- * Invalidate cache information for a single plugin.
+ * Invalidate cache information for a single plugin.
*
- * @param pluginName The name of the plugin to invalid cache entries.
+ * @param pluginName The name of the plugin to invalid cache entries.
*/
void invalidateCache( String pluginName )
{
@@ -782,75 +803,12 @@
return cacheManager.getDynaTagLibCache().keySet();
}
-
/**
- * Load plugins specified in a whitespace delimited string.
- *
- * @param names The whitespace delimited string of plugin names.
+ * Retrieve the plugin processed marker.
*
- * @throws Exception If an error occurs while attempting to load
- * the plugins.
- */
- void loadPlugins( String names, Project project )
- throws Exception
- {
- StringTokenizer tokens = new StringTokenizer( names );
-
- while ( tokens.hasMoreTokens() )
- {
- loadPlugin( tokens.nextToken(), project );
- }
- }
-
- /**
- * Retrieve the prerequisites for a goal.
- *
- * @param name The goal name.
- *
- * @return A list of <code>String</code> prerequisite goal names.
- */
- List getPrereqs( String name )
- {
- String spec = userDefinedGoalsCache.getProperty( name );
-
- if ( spec == null )
- {
- spec = cacheManager.getGoalCache().getProperty( name );
- }
-
- if ( spec == null )
- {
- return Collections.EMPTY_LIST;
- }
-
- int splitLoc = spec.indexOf( ">" );
-
- if ( ( splitLoc < 0 )
- ||
- ( splitLoc + 1 ) == spec.length() )
- {
- return Collections.EMPTY_LIST;
- }
-
- String prereqSpec = spec.substring( splitLoc + 1 );
-
- StringTokenizer tokens = new StringTokenizer( prereqSpec, "," );
-
- List prereqs = new ArrayList();
-
- while ( tokens.hasMoreTokens() )
- {
- prereqs.add( tokens.nextToken() );
- }
-
- return prereqs;
- }
-
- /**
- * Retrieve the plugin processed marker.
+ * @param pluginName Name of the plugin for which to return the processed
marker.
*
- * @param pluginName Name of the plugin for which to return the processed
marker.
- * @return File The file which indicates the plugin has been processed.
+ * @return File The file which indicates the plugin has been processed.
*/
File getPluginProcessedMarker( String pluginName )
{
@@ -858,10 +816,11 @@
}
/**
- * Plugin processed state.
+ * Plugin processed state.
*
- * @param pluginName Name of the plugin for which to check for processing.
- * @return boolean Processed state of plugin.
+ * @param pluginName Name of the plugin for which to check for processing.
+ *
+ * @return boolean Processed state of plugin.
*/
boolean isPluginProcessed( String pluginName )
{
@@ -869,47 +828,6 @@
}
/**
- * Emulation of File.createNewFile for JDK 1.1. <p>
- *
- * This method does <strong>not</strong> guarantee that the operation is
- * atomic.</p>
- *
- * @since 1.21, Ant 1.5
- * @param f New file to create.
- * @return booelan Indicate whether file creation was successful.
- * @throws IOException If an error occurs while creating the new file.
- */
- boolean createNewFile( File f )
- throws IOException
- {
- if ( f != null )
- {
- if ( f.exists() )
- {
- return false;
- }
-
- FileOutputStream fos = null;
- try
- {
- fos = new FileOutputStream( f );
- fos.write( new byte[0] );
- }
- finally
- {
- if ( fos != null )
- {
- fos.close();
- }
- }
-
- return true;
- }
- return false;
- }
-
-
- /**
* Determine if a plugin has been loaded.
*
* @param name The plugin name.
@@ -923,13 +841,15 @@
}
/**
- * Retrieve the plugin's default properties.
+ * Retrieve the plugin's default properties.
+ *
+ * @param pluginDir The name of the plugin.
+ *
+ * @return The default properties file for the plugin, or <code>null</code>
+ * if no such plugin.
*
- * @param pluginDir The name of the plugin.
- * @return The default properties file for the plugin, or <code>null</code>
- * if no such plugin.
- * @throws IOException If an IO error occurs while attempting to read the
- * plugin's properties.
+ * @throws IOException If an IO error occurs while attempting to read the
+ * plugin's properties.
*/
Properties getPluginProperties( File pluginDir )
throws IOException
@@ -954,11 +874,12 @@
/**
- * Retrieve the directory for the specified plugin.
+ * Retrieve the directory for the specified plugin.
*
- * @param pluginName The name of the plugin.
- * @return The directory for the plugin, or <code>null</code> if no such
- * plugin.
+ * @param pluginName The name of the plugin.
+ *
+ * @return The directory for the plugin, or <code>null</code> if no such
+ * plugin.
*/
File getPluginDir( String pluginName )
{
@@ -966,11 +887,12 @@
}
/**
- * Retrieve the plugin's entry-point jelly script.
+ * Retrieve the plugin's entry-point jelly script.
+ *
+ * @param pluginName The name of the plugin.
*
- * @param pluginName The name of the plugin.
- * @return The entry-point script for the plugin, or <code>null</code> if no
- * such plugin.
+ * @return The entry-point script for the plugin, or <code>null</code> if no
+ * such plugin.
*/
File getPluginScript( String pluginName )
{
@@ -978,9 +900,9 @@
}
/**
- * Sets the pluginsDir attribute of the PluginManager object
+ * Sets the pluginsDir attribute of the PluginManager object
*
- * @param pluginsDir The maven plugin directory.
+ * @param pluginsDir The maven plugin directory.
*/
void setPluginsDir( File pluginsDir )
{
@@ -988,9 +910,9 @@
}
/**
- * Retrieve the directory containing all plugins.
+ * Retrieve the directory containing all plugins.
*
- * @return The directory containing all plugins.
+ * @return The directory containing all plugins.
*/
File getPluginsDir()
{
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>