brett 2004/11/05 23:31:13 Modified: src/java/org/apache/maven/plugin Tag: MAVEN-1_0-BRANCH PluginManager.java Log: PR: MAVEN-1471
handle dependency verification at a point where the project is in a consistent state to continue (for multiproject) PR: MAVEN-1493 put back caching of plugins for plugin:install-now formatting Revision Changes Path No revision No revision 1.70.4.57 +119 -84 maven/src/java/org/apache/maven/plugin/PluginManager.java Index: PluginManager.java =================================================================== RCS file: /home/cvs/maven/src/java/org/apache/maven/plugin/PluginManager.java,v retrieving revision 1.70.4.56 retrieving revision 1.70.4.57 diff -u -r1.70.4.56 -r1.70.4.57 --- PluginManager.java 4 Nov 2004 15:17:57 -0000 1.70.4.56 +++ PluginManager.java 6 Nov 2004 07:31:13 -0000 1.70.4.57 @@ -98,7 +98,9 @@ */ public class PluginManager extends AbstractMavenComponent { - /** Logger */ + /** + * Logger + */ private static final Log log = LogFactory.getLog( PluginManager.class ); /** @@ -114,37 +116,59 @@ /** */ public static final String BASE_CONTEXT = "maven.goalAttainmentContext"; - /** The directory where plugin jars reside under Maven's home. */ + /** + * The directory where plugin jars reside under Maven's home. + */ private File pluginsDir; - /** The directory where the plugin jars are unpacked to. */ + /** + * The directory where the plugin jars are unpacked to. + */ private File unpackedPluginsDir; - /** The directory where the user's plugin jars are installed. */ + /** + * The directory where the user's plugin jars are installed. + */ private File userPluginsDir; - /** This contains a map of plugins, keyed by id. */ + /** + * This contains a map of plugins, keyed by id. + */ private final Map pluginHousings = new HashMap(); - /** This contains a map of plugins, keyed by artifact id. */ + /** + * This contains a map of plugins, keyed by artifact id. + */ private final Map artifactIdToHousingMap = new HashMap(); - /** Maven session reference. */ + /** + * Maven session reference. + */ private MavenSession mavenSession; - /** Plugin cache manager. */ + /** + * Plugin cache manager. + */ private final PluginCacheManager cacheManager = new PluginCacheManager(); - /** Goal to Plugins mapper. */ + /** + * Goal to Plugins mapper. + */ private GoalToJellyScriptHousingMapper mapper = new GoalToJellyScriptHousingMapper(); - /** Current plugins mapper (transient - includes maven.xml, etc). **/ + /** + * Current plugins mapper (transient - includes maven.xml, etc). * + */ private GoalToJellyScriptHousingMapper transientMapper = mapper; - /** Plugins to be popped afterwards. */ + /** + * Plugins to be popped afterwards. + */ private Set delayedPops = new HashSet(); - /** The current goal attainment base context. */ + /** + * The current goal attainment base context. + */ private MavenJellyContext baseContext; private static final String PLUGIN_TEMP_MAP = "PluginManager.PLUGIN_TEMP_MAP"; @@ -162,7 +186,7 @@ /** * Get the list of plugin files. */ - private Map getPluginFiles( File directory, boolean acceptDirectories ) throws MavenException + private Map getPluginFiles( File directory, boolean acceptDirectories ) { File[] files = directory.listFiles(); if ( files == null ) @@ -193,7 +217,7 @@ /** * Load plugins. - * + * * @throws MavenException when the plugin jars can't be expanded */ private void loadUncachedPlugins( Map pluginFiles ) throws IOException, MavenException @@ -202,8 +226,8 @@ for ( Iterator i = pluginFiles.keySet().iterator(); i.hasNext(); ) { - String name = ( String ) i.next(); - File pluginDir = ( File ) pluginFiles.get( name ); + String name = (String) i.next(); + File pluginDir = (File) pluginFiles.get( name ); if ( !isLoaded( name ) ) { @@ -220,8 +244,6 @@ /** * Initialize all plugins. - * - * @throws Exception If an error occurs while initializing any plugin. */ public void initialize() throws IOException, MavenException { @@ -233,10 +255,10 @@ setPluginsDir( new File( mavenSession.getRootContext().getPluginsDir() ) ); setUnpackedPluginsDir( new File( mavenSession.getRootContext().getUnpackedPluginsDir() ) ); setUserPluginsDir( new File( mavenSession.getRootContext().getUserPluginsDir() ) ); - + if ( !getPluginsDir().isDirectory() || - ( getPluginsDir().listFiles() != null && - getPluginsDir().listFiles().length == 0 ) ) + ( getPluginsDir().listFiles() != null && + getPluginsDir().listFiles().length == 0 ) ) { throw new MavenException( "Maven was badly installed. Please reinstall it." ); } @@ -254,12 +276,12 @@ Map pluginFiles = getPluginFiles( pluginsDir, true ); Map userPluginFiles = getPluginFiles( userPluginsDir, false ); - + if ( !userPluginFiles.isEmpty() ) { pluginFiles.putAll( userPluginFiles ); } - + Map pluginDirs = expandPluginFiles( pluginFiles ); cacheManager.loadCache( unpackedPluginsDir ); @@ -271,7 +293,7 @@ // The following housings are considered loaded - so go through and cache them manually for ( Iterator i = pluginHousings.values().iterator(); i.hasNext(); ) { - JellyScriptHousing housing = ( JellyScriptHousing ) i.next(); + JellyScriptHousing housing = (JellyScriptHousing) i.next(); cacheManager.registerPlugin( housing.getName(), housing ); housing.parse( cacheManager ); housing.parse( mapper ); @@ -290,8 +312,8 @@ Map pluginDirs = new HashMap(); for ( Iterator i = pluginFiles.keySet().iterator(); i.hasNext(); ) { - String name = ( String ) i.next(); - File jarFile = ( File ) pluginFiles.get( name ); + String name = (String) i.next(); + File jarFile = (File) pluginFiles.get( name ); File dir = jarFile.isDirectory() ? jarFile : unpackPlugin( name, jarFile ); pluginDirs.put( name, dir ); } @@ -300,7 +322,7 @@ JellyScriptHousing loadPluginHousing( String name, File pluginDir ) throws IOException { - JellyScriptHousing housing = ( JellyScriptHousing ) pluginHousings.get( name ); + JellyScriptHousing housing = (JellyScriptHousing) pluginHousings.get( name ); return ( housing == null ? createLazyPluginHousing( pluginDir ) : housing ); } @@ -320,7 +342,7 @@ if ( artifactIdToHousingMap.containsKey( artifactId ) ) { JellyScriptHousing h = (JellyScriptHousing) artifactIdToHousingMap.get( artifactId ); - log.warn("WARNING: Plugin '" + artifactId + "' is already loaded from " + h.getName() + "; attempting to load " + housing.getName()); + log.warn( "WARNING: Plugin '" + artifactId + "' is already loaded from " + h.getName() + "; attempting to load " + housing.getName() ); } artifactIdToHousingMap.put( artifactId, housing ); } @@ -338,9 +360,9 @@ log.debug( "Loading plugin '" + pluginName + "'" ); JellyScriptHousing jellyScriptHousing = new JellyScriptHousing( pluginDir, mavenSession.getRootContext() ); - + pluginHousings.put( pluginName, jellyScriptHousing ); - + return jellyScriptHousing; } @@ -350,14 +372,9 @@ } /** - * @param project - * @param unpackedPluginDirectory - * @param jelly - * @return - * @throws Exception - * @todo [1.0] refactor into housing - * @deprecated get rid of this - it duplicates functionality in the housing + * @todo refactor into housing * @todo don't throw Exception + * @todo get rid of this - it duplicates functionality in the housing */ private JellyScriptHousing createJellyScriptHousing( Project project, InputStream jelly ) throws Exception { @@ -382,7 +399,6 @@ /** * @param project - * @param classesDirectory * @param jelly * @return * @todo [1.0] into the housing? @@ -405,7 +421,7 @@ * @throws Exception for any other issue. FIXME */ public void processDependencies( Project project ) - throws MalformedURLException, Exception + throws MalformedURLException, Exception { if ( project.getArtifacts() == null ) { @@ -413,13 +429,13 @@ return; } - ForeheadClassLoader projectClassLoader = ( ForeheadClassLoader ) project.getContext().getClassLoader(); + ForeheadClassLoader projectClassLoader = (ForeheadClassLoader) project.getContext().getClassLoader(); log.debug( "Processing dependencies for project " + project.getName() + "; classloader " + projectClassLoader ); // add the dependencies to the classpath - for ( Iterator i = project.getArtifacts().iterator(); i.hasNext();) + for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); ) { - Artifact artifact = ( Artifact ) i.next(); + Artifact artifact = (Artifact) i.next(); Dependency dependency = artifact.getDependency(); if ( dependency.isPlugin() ) { @@ -501,8 +517,7 @@ /** * Attain the goals. * - * @throws Exception - * If one of the specified + * @throws Exception If one of the specified * goals refers to an non-existent goal. * @throws Exception If an exception occurs while running a goal. * @todo stop throwing Exception @@ -510,6 +525,7 @@ public void attainGoals( Project project, List goals ) throws Exception { MavenJellyContext prevBaseContext = baseContext; + baseContext = new MavenJellyContext( mavenSession.getRootContext() ); baseContext.setInherit( true ); JellyUtils.populateVariables( baseContext, project.getContext() ); @@ -517,12 +533,6 @@ baseContext.setProject( project ); // Set up the ant project. - // Before attempting to attain the goals verify the project - // if desired. - // FIXME: From attainGoals angle, how does it know the project needs to - // to be verified, or that the project object hasn't been used before - project.verifyDependencies(); - AntProjectBuilder.build( project, baseContext ); // TODO: shouldn't this be a stack too? Then session attribute not needed @@ -567,8 +577,8 @@ if ( goals != null ) { for ( Iterator i = goals.iterator(); i.hasNext(); ) - { - String goal = ( String ) i.next(); + { + String goal = (String) i.next(); if ( goal.trim().length() == 0 ) { i.remove(); @@ -618,30 +628,33 @@ try { runScript( driverHousing, baseContext ); - transientMapper.addResolvedPlugins( Collections.singletonList( driverHousing )); + transientMapper.addResolvedPlugins( Collections.singletonList( driverHousing ) ); // Dependencies must be processed after the driver is run for compatibility + // FIXME: From attainGoals angle, how does it know the project needs to + // to be verified, or that the project object hasn't been used before + project.verifyDependencies(); processDependencies( project ); - for ( Iterator j = projectHousings.iterator(); j.hasNext();) + for ( Iterator j = projectHousings.iterator(); j.hasNext(); ) { - JellyScriptHousing housing = ( JellyScriptHousing ) j.next(); + JellyScriptHousing housing = (JellyScriptHousing) j.next(); runScript( housing, baseContext ); } transientMapper.addResolvedPlugins( projectHousings ); // Plugin Jelly scripts - for ( Iterator i = goals.iterator(); i.hasNext();) + for ( Iterator i = goals.iterator(); i.hasNext(); ) { - String goalName = ( String ) i.next(); + String goalName = (String) i.next(); - pluginSet.addAll( prepAttainGoal( goalName, baseContext, transientMapper )); + pluginSet.addAll( prepAttainGoal( goalName, baseContext, transientMapper ) ); } // Plugin Jelly scripts - for ( Iterator i = goals.iterator(); i.hasNext();) + for ( Iterator i = goals.iterator(); i.hasNext(); ) { - String goalName = ( String ) i.next(); + String goalName = (String) i.next(); log.debug( "attaining goal " + goalName ); try { @@ -675,9 +688,9 @@ { delayedPops.addAll( pluginSet ); - for ( Iterator j = delayedPops.iterator(); j.hasNext();) + for ( Iterator j = delayedPops.iterator(); j.hasNext(); ) { - JellyScriptHousing housing = ( JellyScriptHousing ) j.next(); + JellyScriptHousing housing = (JellyScriptHousing) j.next(); reinstallPlugins( housing.getProject().getContext() ); @@ -693,7 +706,7 @@ * @param goalName the goal * @param baseContext the base context to attain in * @return a set of plugins required to attain the goal - * @throws Exception + * @throws Exception * @todo don't throw Exception */ public Set prepAttainGoal( String goalName, MavenJellyContext baseContext, @@ -701,9 +714,9 @@ { Set pluginSet = goalMapper.resolveJellyScriptHousings( goalName ); - for ( Iterator j = pluginSet.iterator(); j.hasNext();) + for ( Iterator j = pluginSet.iterator(); j.hasNext(); ) { - JellyScriptHousing housing = ( JellyScriptHousing ) j.next(); + JellyScriptHousing housing = (JellyScriptHousing) j.next(); initialiseHousingPluginContext( housing, baseContext ); } return pluginSet; @@ -713,10 +726,13 @@ { Project project = housing.getProject(); + // TODO: I think this should merge into pluginContext, but that seems to break existing jelly as it depends on + // that kind of warped scoping MavenUtils.integrateMapInContext( housing.getPluginProperties(), baseContext ); // TODO necessary to create a new one every time? MavenJellyContext pluginContext = new MavenJellyContext( baseContext ); + //MavenUtils.integrateMapInContext( project.getContext().getVariables(), pluginContext ); project.pushContext( pluginContext ); pluginContext.setInherit( true ); pluginContext.setVariable( "context", pluginContext ); @@ -801,8 +817,9 @@ /** * Warning - this completely scrogs the default mapper. Only use this before System.exit! * (currently used by maven -u). - * @todo refactor to return mapper instead and use that, or perhaps instantiate a new plugin manager + * * @return + * @todo refactor to return mapper instead and use that, or perhaps instantiate a new plugin manager */ public Set getGoalNames( Project project ) throws MavenException { @@ -812,13 +829,23 @@ } /** + */ + public void installPlugin( File file, Project parentProject ) throws MavenException + { + // By default, don't copy to the unpacked plugins directory - only use this dependency for this project + installPlugin( file, parentProject, false ); + // TODO: we should unload the plugin after the project is done in this case - we really need + // to define the lifecycle of plugins + } + + /** * Load and install a plugin. * * @param file the file to install. Must be a plugin jar * @param parentProject the project to load the installed plugin into * @todo remove any old one */ - public void installPlugin( File file, Project parentProject ) + public void installPlugin( File file, Project parentProject, boolean cache ) throws MavenException { log.debug( "Using plugin file: " + file ); @@ -826,7 +853,7 @@ { String pluginName = file.getCanonicalFile().getName(); pluginName = pluginName.substring( 0, pluginName.indexOf( ".jar" ) ); - + if ( isLoaded( pluginName ) ) { // already installed this version @@ -865,6 +892,12 @@ housing.parse( transientMapper ); // Should only be putting in the transientMapper - but it is not consistent with isLoaded housing.parse( mapper ); + if ( cache ) + { + cacheManager.registerPlugin( pluginName, housing ); + housing.parse( cacheManager ); + cacheManager.saveCache( unpackedPluginsDir ); + } } else { @@ -910,7 +943,7 @@ log.debug( "Reinstalling: " + housing ); housing.parse( transientMapper ); housing.parse( mapper ); - } + } } } @@ -918,7 +951,7 @@ { log.debug( "Uninstalling plugin: " + artifactId ); - JellyScriptHousing housing = ( JellyScriptHousing ) artifactIdToHousingMap.get( artifactId ); + JellyScriptHousing housing = (JellyScriptHousing) artifactIdToHousingMap.get( artifactId ); if ( housing == null ) { log.warn( "Plugin not found when attempting to uninstall '" + artifactId + "'" ); @@ -935,31 +968,33 @@ } /** - * @todo [1.0] refactor out, or make more appropriate structure * @param id * @return * @throws UnknownPluginException + * @todo [1.0] refactor out, or make more appropriate structure * @todo remove throws Exception */ public MavenJellyContext getPluginContext( String id ) throws MavenException, UnknownPluginException { - JellyScriptHousing housing = ( JellyScriptHousing ) artifactIdToHousingMap.get( id ); + JellyScriptHousing housing = (JellyScriptHousing) artifactIdToHousingMap.get( id ); if ( housing != null ) { Project project = housing.getProject(); if ( baseContext != project.getContext().getParent() ) { - log.debug("Plugin context for " + id + " not initialised for this base context: initialising inside getPluginContext"); - try { + log.debug( "Plugin context for " + id + " not initialised for this base context: initialising inside getPluginContext" ); + try + { return initialiseHousingPluginContext( housing, baseContext ); } - catch (Exception e) { - throw new MavenException("Error initialising plugin context", e); + catch ( Exception e ) + { + throw new MavenException( "Error initialising plugin context", e ); } } else { - log.debug("Plugin context for " + id + " already initialised for this base context"); + log.debug( "Plugin context for " + id + " already initialised for this base context" ); return project.getContext(); } } @@ -1015,8 +1050,8 @@ } /** - * @todo get rid of throws Exception * @return + * @todo get rid of throws Exception */ private Script loadScript( JellyScriptHousing jellyScriptHousing ) throws Exception { @@ -1029,7 +1064,7 @@ // TODO: should differentiate between plugins and script housings better jellyScriptHousing.getProject().verifyDependencies(); processDependencies( jellyScriptHousing.getProject() ); - ForeheadClassLoader pluginClassLoader = ( ForeheadClassLoader ) jellyScriptHousing.getProject().getContext().getClassLoader(); + ForeheadClassLoader pluginClassLoader = (ForeheadClassLoader) jellyScriptHousing.getProject().getContext().getClassLoader(); pluginClassLoader.addURL( jellyScriptHousing.getPluginDirectory().toURL() ); } @@ -1057,8 +1092,8 @@ } /** - * @param context - * @throws Exception + * @param context + * @throws Exception * @todo get rid of throws Exception */ void runScript( JellyScriptHousing jellyScriptHousing, MavenJellyContext context ) throws Exception @@ -1091,10 +1126,10 @@ ArrayList list = new ArrayList(); for ( Iterator i = pluginHousings.values().iterator(); i.hasNext(); ) { - JellyScriptHousing housing = ( JellyScriptHousing ) i.next(); + JellyScriptHousing housing = (JellyScriptHousing) i.next(); list.add( housing.getName() ); } - Collections.sort(list); + Collections.sort( list ); return list; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]