diff --git a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/Pom.java b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/Pom.java index 1b12f37e69..3ba77bbf38 100755 --- a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/Pom.java +++ b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/Pom.java @@ -74,7 +74,6 @@ public class Pom } public void setFile( File file ) - throws Exception { this.file = file; } diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 9258fb6f99..509998e47a 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -23,6 +23,7 @@ import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionResponse; import org.apache.maven.execution.MavenSession; +import org.apache.maven.lifecycle.LifecycleExecutionException; import org.apache.maven.lifecycle.LifecycleExecutor; import org.apache.maven.monitor.event.EventDispatcher; import org.apache.maven.monitor.event.MavenEvents; @@ -32,8 +33,8 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.reactor.ReactorException; -import org.apache.maven.settings.Settings; import org.apache.maven.settings.Proxy; +import org.apache.maven.settings.Settings; import org.codehaus.plexus.PlexusConstants; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; @@ -76,21 +77,22 @@ public class DefaultMaven protected PlexusContainer container; protected ArtifactRepositoryFactory artifactRepositoryFactory; - + protected WagonManager wagonManager; // ---------------------------------------------------------------------- // Project execution // ---------------------------------------------------------------------- - public MavenExecutionResponse execute( MavenExecutionRequest request ) throws ReactorException + public MavenExecutionResponse execute( MavenExecutionRequest request ) + throws ReactorException { if ( request.getGoals().isEmpty() ) { throw new ReactorException( "You must specify at least one goal. Try 'install'." ); } - - if( request.getSettings().getActiveProfile().isOffline() ) + + if ( request.getSettings().getActiveProfile().isOffline() ) { getLogger().info( "Maven is running in offline mode." ); } @@ -100,7 +102,7 @@ public class DefaultMaven // TODO: goals are outer loop dispatcher.dispatchStart( event, request.getBaseDirectory() ); - + try { List projects; @@ -201,11 +203,19 @@ public class DefaultMaven } private MavenExecutionResponse processProject( MavenExecutionRequest request, MavenProject project, - EventDispatcher dispatcher, List goals ) throws Exception + EventDispatcher dispatcher, List goals ) + throws LifecycleExecutionException { MavenSession session = createSession( request, project ); - resolveParameters( request ); + try + { + resolveParameters( request ); + } + catch ( ComponentLookupException e ) + { + throw new LifecycleExecutionException( "Unable to configure Maven for execution", e ); + } // !! This is ripe for refactoring to an aspect. // Event monitoring. @@ -221,7 +231,7 @@ public class DefaultMaven dispatcher.dispatchEnd( event, project.getId() ); } - catch ( Exception e ) + catch ( LifecycleExecutionException e ) { dispatcher.dispatchError( event, project.getId(), e ); throw e; @@ -261,7 +271,8 @@ public class DefaultMaven return response; } - public MavenProject getProject( File pom, ArtifactRepository localRepository ) throws ProjectBuildingException + public MavenProject getProject( File pom, ArtifactRepository localRepository ) + throws ProjectBuildingException { if ( pom.exists() ) { @@ -293,11 +304,11 @@ public class DefaultMaven /** * @todo [BP] this might not be required if there is a better way to pass * them in. It doesn't feel quite right. - * * @todo [JC] we should at least provide a mapping of protocol-to-proxy for * the wagons, shouldn't we? */ - private void resolveParameters( MavenExecutionRequest request ) throws ComponentLookupException + private void resolveParameters( MavenExecutionRequest request ) + throws ComponentLookupException { WagonManager wagonManager = (WagonManager) container.lookup( WagonManager.ROLE ); @@ -317,7 +328,8 @@ public class DefaultMaven // Lifecylce Management // ---------------------------------------------------------------------- - public void contextualize( Context context ) throws ContextException + public void contextualize( Context context ) + throws ContextException { container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); } @@ -394,9 +406,8 @@ public class DefaultMaven Runtime r = Runtime.getRuntime(); - getLogger().info( - "Final Memory: " + ( ( r.totalMemory() - r.freeMemory() ) / mb ) + "M/" - + ( r.totalMemory() / mb ) + "M" ); + getLogger().info( "Final Memory: " + ( ( r.totalMemory() - r.freeMemory() ) / mb ) + "M/" + + ( r.totalMemory() / mb ) + "M" ); } protected void line() diff --git a/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java index 32ae802f02..81a112f981 100644 --- a/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java @@ -40,16 +40,21 @@ import org.apache.maven.monitor.event.DefaultEventMonitor; import org.apache.maven.monitor.event.EventDispatcher; import org.apache.maven.monitor.logging.DefaultLog; import org.apache.maven.plugin.Plugin; -import org.apache.maven.settings.Settings; +import org.apache.maven.reactor.ReactorException; import org.apache.maven.settings.MavenSettingsBuilder; import org.apache.maven.settings.Profile; +import org.apache.maven.settings.Settings; import org.codehaus.classworlds.ClassWorld; +import org.codehaus.plexus.PlexusContainerException; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.embed.ArtifactEnabledEmbedder; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.LoggerManager; import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; +import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.Collections; @@ -67,7 +72,6 @@ public class MavenCli public static File userDir = new File( System.getProperty( "user.dir" ) ); public static int main( String[] args, ClassWorld classWorld ) - throws Exception { // ---------------------------------------------------------------------- // Setup the command line parser @@ -75,7 +79,17 @@ public class MavenCli CLIManager cliManager = new CLIManager(); - CommandLine commandLine = cliManager.parse( args ); + CommandLine commandLine; + try + { + commandLine = cliManager.parse( args ); + } + catch ( ParseException e ) + { + System.err.println( "Unable to parse command line options: " + e.getMessage() ); + cliManager.displayHelp(); + return 1; + } // ---------------------------------------------------------------------- // @@ -95,6 +109,8 @@ public class MavenCli initializeSystemProperties( commandLine ); + boolean debug = commandLine.hasOption( CLIManager.DEBUG ); + // ---------------------------------------------------------------------- // Process particular command line options // ---------------------------------------------------------------------- @@ -106,10 +122,218 @@ public class MavenCli } if ( commandLine.hasOption( CLIManager.VERSION ) ) { - // TODO: is there a beter way? Maybe read the manifest? + showVersion(); - String version = "unknown"; + return 0; + } + EventDispatcher eventDispatcher = new DefaultEventDispatcher(); + + // ---------------------------------------------------------------------- + // Now that we have everything that we need we will fire up plexus and + // bring the maven component to life for use. + // ---------------------------------------------------------------------- + + ArtifactEnabledEmbedder embedder = new ArtifactEnabledEmbedder(); + + try + { + embedder.start( classWorld ); + } + catch ( PlexusContainerException e ) + { + showFatalError( "Unable to start the embedded plexus container", e, debug ); + return 1; + } + + Settings settings; + try + { + MavenSettingsBuilder settingsBuilder = (MavenSettingsBuilder) embedder.lookup( MavenSettingsBuilder.ROLE ); + + settings = settingsBuilder.buildSettings(); + } + catch ( IOException e ) + { + showFatalError( "Unable to read settings.xml", e, debug ); + return 1; + } + catch ( XmlPullParserException e ) + { + showFatalError( "Unable to read settings.xml", e, debug ); + return 1; + } + catch ( ComponentLookupException e ) + { + showFatalError( "Unable to read settings.xml", e, debug ); + return 1; + } + + List projectFiles = null; + try + { + projectFiles = getProjectFiles( commandLine ); + } + catch ( IOException e ) + { + showFatalError( "Error locating project files for reactor execution", e, debug ); + return 1; + } + + Maven maven = null; + MavenExecutionRequest request = null; + try + { + maven = createMavenInstance( embedder ); + + request = createRequest( projectFiles, embedder, commandLine, settings, eventDispatcher, debug ); + } + catch ( ComponentLookupException e ) + { + showFatalError( "Unable to configure the Maven application", e, debug ); + return 1; + } + + MavenExecutionResponse response = null; + try + { + response = maven.execute( request ); + } + catch ( ReactorException e ) + { + showFatalError( "Error executing Maven for a project", e, debug ); + return 1; + } + + if ( response != null && response.isExecutionFailure() ) + { + return 1; + } + else + { + return 0; + } + } + + private static void showFatalError( String message, Exception e, boolean debug ) + { + System.err.println( "FATAL ERROR: " + message ); + if ( debug ) + { + e.printStackTrace(); + } + else + { + System.err.println( "For more information, run with the -X flag" ); + } + } + + private static MavenExecutionRequest createRequest( List files, ArtifactEnabledEmbedder embedder, + CommandLine commandLine, Settings settings, + EventDispatcher eventDispatcher, boolean debug ) + throws ComponentLookupException + { + MavenExecutionRequest request = null; + + ArtifactRepository localRepository = createLocalRepository( embedder, settings, commandLine ); + + request = new DefaultMavenExecutionRequest( localRepository, settings, eventDispatcher, + commandLine.getArgList(), files, userDir.getPath() ); + + LoggerManager manager = (LoggerManager) embedder.lookup( LoggerManager.ROLE ); + if ( debug ) + { + manager.setThreshold( Logger.LEVEL_DEBUG ); + } + + // TODO [BP]: do we set one per mojo? where to do it? + Logger logger = manager.getLoggerForComponent( Plugin.ROLE ); + if ( logger != null ) + { + request.setLog( new DefaultLog( logger ) ); + + request.addEventMonitor( new DefaultEventMonitor( logger ) ); + } + + if ( commandLine.hasOption( CLIManager.NON_RECURSIVE ) ) + { + request.setRecursive( false ); + } + return request; + } + + private static List getProjectFiles( CommandLine commandLine ) + throws IOException + { + List files = Collections.EMPTY_LIST; + if ( commandLine.hasOption( CLIManager.REACTOR ) ) + { + // TODO: should we now include the pom.xml in the current directory? + String includes = System.getProperty( "maven.reactor.includes", "**/" + POMv4 ); + + String excludes = System.getProperty( "maven.reactor.excludes", POMv4 ); + + files = FileUtils.getFiles( userDir, includes, excludes ); + } + else + { + File projectFile = new File( userDir, POMv4 ); + + if ( projectFile.exists() ) + { + files = Collections.singletonList( projectFile ); + } + } + return files; + } + + private static Maven createMavenInstance( ArtifactEnabledEmbedder embedder ) + throws ComponentLookupException + { + // TODO [BP]: doing this here as it is CLI specific, though it doesn't feel like the right place (likewise logger). + WagonManager wagonManager = (WagonManager) embedder.lookup( WagonManager.ROLE ); + wagonManager.setDownloadMonitor( new ConsoleDownloadMonitor() ); + + return (Maven) embedder.lookup( Maven.ROLE ); + } + + private static ArtifactRepository createLocalRepository( ArtifactEnabledEmbedder embedder, Settings settings, + CommandLine commandLine ) + throws ComponentLookupException + { + ArtifactRepositoryLayout repositoryLayout = (ArtifactRepositoryLayout) embedder.lookup( + ArtifactRepositoryLayout.ROLE, "default" ); + + ArtifactRepositoryFactory artifactRepositoryFactory = (ArtifactRepositoryFactory) embedder.lookup( + ArtifactRepositoryFactory.ROLE ); + + ArtifactRepository localRepository = getLocalRepository( settings, artifactRepositoryFactory, repositoryLayout ); + + boolean snapshotPolicySet = false; + if ( commandLine.hasOption( CLIManager.OFFLINE ) ) + { + settings.getActiveProfile().setOffline( true ); + + // TODO: this will still check to download if the artifact does not exist locally, instead of failing as it should in offline mode + artifactRepositoryFactory.setGlobalSnapshotPolicy( ArtifactRepository.SNAPSHOT_POLICY_NEVER ); + snapshotPolicySet = true; + } + + if ( !snapshotPolicySet && commandLine.hasOption( CLIManager.UPDATE_SNAPSHOTS ) ) + { + artifactRepositoryFactory.setGlobalSnapshotPolicy( ArtifactRepository.SNAPSHOT_POLICY_ALWAYS ); + } + return localRepository; + } + + private static void showVersion() + { + // TODO: is there a beter way? Maybe read the manifest? + + String version = "unknown"; + + try + { for ( Enumeration e = MavenCli.class.getClassLoader().getResources( "/META-INF/maven/pom.xml" ); e.hasMoreElements(); ) { @@ -124,121 +348,14 @@ public class MavenCli } System.out.println( "Maven version: " + version ); - return 0; } - - // ---------------------------------------------------------------------- - // We will ultimately not require a flag to indicate the reactor as - // we should take this from the execution context i.e. what the type - // is stated as in the POM. - // ---------------------------------------------------------------------- - - MavenExecutionRequest request = null; - - File projectFile = new File( userDir, POMv4 ); - - EventDispatcher eventDispatcher = new DefaultEventDispatcher(); - - // ---------------------------------------------------------------------- - // Now that we have everything that we need we will fire up plexus and - // bring the maven component to life for use. - // ---------------------------------------------------------------------- - - ArtifactEnabledEmbedder embedder = new ArtifactEnabledEmbedder(); - - embedder.start( classWorld ); - - MavenSettingsBuilder settingsBuilder = (MavenSettingsBuilder) embedder.lookup( MavenSettingsBuilder.ROLE ); - - Settings settings = settingsBuilder.buildSettings(); - - ArtifactRepositoryFactory artifactRepositoryFactory = (ArtifactRepositoryFactory) embedder.lookup( - ArtifactRepositoryFactory.ROLE ); - - - boolean snapshotPolicySet = false; - if ( commandLine.hasOption( CLIManager.OFFLINE ) ) + catch ( IOException e ) { - settings.getActiveProfile().setOffline(true); - - // TODO: this will still check to download if the artifact does not exist locally, instead of failing as it should in offline mode - artifactRepositoryFactory.setGlobalSnapshotPolicy( ArtifactRepository.SNAPSHOT_POLICY_NEVER ); - snapshotPolicySet = true; + System.err.println( "Unable determine version from JAR file: " + e.getMessage() ); } - - if ( !snapshotPolicySet && commandLine.hasOption( CLIManager.UPDATE_SNAPSHOTS ) ) + catch ( XmlPullParserException e ) { - artifactRepositoryFactory.setGlobalSnapshotPolicy( ArtifactRepository.SNAPSHOT_POLICY_ALWAYS ); - } - - ArtifactRepositoryLayout repositoryLayout = (ArtifactRepositoryLayout) embedder.lookup( - ArtifactRepositoryLayout.ROLE, "default" ); - - ArtifactRepository localRepository = getLocalRepository( settings, artifactRepositoryFactory, repositoryLayout ); - - if ( commandLine.hasOption( CLIManager.REACTOR ) ) - { - // TODO: should we now include the pom.xml in the current directory? - String includes = System.getProperty( "maven.reactor.includes", "**/" + POMv4 ); - - String excludes = System.getProperty( "maven.reactor.excludes", POMv4 ); - - request = new DefaultMavenExecutionRequest( localRepository, settings, eventDispatcher, - commandLine.getArgList(), FileUtils.getFiles( userDir, - includes, - excludes ), - userDir.getPath() ); - } - else - { - List files = Collections.EMPTY_LIST; - if ( projectFile.exists() ) - { - files = Collections.singletonList( projectFile ); - } - request = new DefaultMavenExecutionRequest( localRepository, settings, eventDispatcher, - commandLine.getArgList(), files, userDir.getPath() ); - - if ( commandLine.hasOption( CLIManager.NON_RECURSIVE ) ) - { - request.setRecursive( false ); - } - } - - LoggerManager manager = (LoggerManager) embedder.lookup( LoggerManager.ROLE ); - if ( commandLine.hasOption( CLIManager.DEBUG ) ) - { - manager.setThreshold( Logger.LEVEL_DEBUG ); - } - - // TODO [BP]: do we set one per mojo? where to do it? - Logger logger = manager.getLoggerForComponent( Plugin.ROLE ); - if ( logger != null ) - { - request.setLog( new DefaultLog( logger ) ); - - request.addEventMonitor( new DefaultEventMonitor( logger ) ); - } - - // TODO [BP]: doing this here as it is CLI specific, though it doesn't feel like the right place (likewise logger). - WagonManager wagonManager = (WagonManager) embedder.lookup( WagonManager.ROLE ); - wagonManager.setDownloadMonitor( new ConsoleDownloadMonitor() ); - - Maven maven = (Maven) embedder.lookup( Maven.ROLE ); - - // ---------------------------------------------------------------------- - // - // ---------------------------------------------------------------------- - - MavenExecutionResponse response = maven.execute( request ); - - if ( response != null && response.isExecutionFailure() ) - { - return 1; - } - else - { - return 0; + System.err.println( "Unable to parse POM in JAR file: " + e.getMessage() ); } } @@ -355,10 +472,8 @@ public class MavenCli // // ---------------------------------------------------------------------- - protected static ArtifactRepository getLocalRepository( Settings settings, - ArtifactRepositoryFactory repoFactory, + protected static ArtifactRepository getLocalRepository( Settings settings, ArtifactRepositoryFactory repoFactory, ArtifactRepositoryLayout repositoryLayout ) - throws Exception { Profile profile = settings.getActiveProfile(); diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java index 2e6514b68e..11c310808a 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java @@ -18,6 +18,7 @@ package org.apache.maven.lifecycle; import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; +import org.apache.maven.artifact.handler.manager.ArtifactHandlerNotFoundException; import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.execution.MavenExecutionResponse; import org.apache.maven.execution.MavenSession; @@ -28,6 +29,8 @@ import org.apache.maven.monitor.event.EventDispatcher; import org.apache.maven.monitor.event.MavenEvents; import org.apache.maven.plugin.PluginExecutionException; import org.apache.maven.plugin.PluginManager; +import org.apache.maven.plugin.PluginManagerException; +import org.apache.maven.plugin.PluginNotFoundException; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.project.MavenProject; @@ -139,9 +142,13 @@ public class DefaultLifecycleExecutor { response.setException( e ); } - catch ( Exception e ) + catch ( PluginNotFoundException e ) { - throw new LifecycleExecutionException( "Error during lifecycle execution", e ); + response.setException( e ); + } + catch ( ArtifactHandlerNotFoundException e ) + { + response.setException( e ); } finally { @@ -206,9 +213,8 @@ public class DefaultLifecycleExecutor return plugin; } - // TODO: don't throw Exception private void processPluginConfiguration( MavenProject project, MavenSession mavenSession, Map phaseMap ) - throws Exception + throws LifecycleExecutionException, PluginNotFoundException { for ( Iterator i = project.getPlugins().iterator(); i.hasNext(); ) { @@ -224,16 +230,22 @@ public class DefaultLifecycleExecutor * to execute for that given phase. * * @param session - * @throws Exception */ private void processPluginPhases( Plugin plugin, MavenSession session, Map phaseMap ) - throws Exception + throws LifecycleExecutionException, PluginNotFoundException { String groupId = plugin.getGroupId(); String artifactId = plugin.getArtifactId(); - pluginManager.verifyPlugin( groupId, artifactId, session ); + try + { + pluginManager.verifyPlugin( groupId, artifactId, session ); + } + catch ( PluginManagerException e ) + { + throw new LifecycleExecutionException( "Internal error in the plugin manager", e ); + } PluginDescriptor pluginDescriptor = pluginManager.getPluginDescriptor( groupId, artifactId ); @@ -288,7 +300,7 @@ public class DefaultLifecycleExecutor */ private void configureMojo( MojoDescriptor mojoDescriptor, Map phaseMap, Settings settings ) { - if( settings.getActiveProfile().isOffline() && mojoDescriptor.requiresOnline() ) + if ( settings.getActiveProfile().isOffline() && mojoDescriptor.requiresOnline() ) { String goal = mojoDescriptor.getGoal(); getLogger().warn( goal + " requires online mode, but maven is currently offline. Disabling " + goal + "." ); @@ -305,7 +317,7 @@ public class DefaultLifecycleExecutor } private void processGoalChain( String task, MavenSession session, Map phaseMap ) - throws Exception + throws LifecycleExecutionException, PluginNotFoundException { if ( phaseMap.containsKey( task ) ) { @@ -337,7 +349,7 @@ public class DefaultLifecycleExecutor } private void verifyMojoPhase( String task, MavenSession session, Map phaseMap ) - throws Exception + throws LifecycleExecutionException, PluginNotFoundException { MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( task ); @@ -356,7 +368,14 @@ public class DefaultLifecycleExecutor injectHandlerPluginConfiguration( session.getProject(), groupId, artifactId ); - pluginManager.verifyPluginForGoal( task, session ); + try + { + pluginManager.verifyPluginForGoal( task, session ); + } + catch ( PluginManagerException e ) + { + throw new LifecycleExecutionException( "Internal error in the plugin manager", e ); + } mojoDescriptor = pluginManager.getMojoDescriptor( task ); @@ -370,7 +389,7 @@ public class DefaultLifecycleExecutor } private void executePhase( String phase, MavenSession session, Map phaseMap ) - throws PluginExecutionException + throws PluginExecutionException, PluginNotFoundException { // only execute up to the given phase int index = phases.indexOf( phaseMap.get( phase ) ); @@ -411,7 +430,7 @@ public class DefaultLifecycleExecutor } protected void executeMojo( String id, MavenSession session ) - throws PluginExecutionException + throws PluginExecutionException, PluginNotFoundException { // ---------------------------------------------------------------------- // We have something of the form :, so this might be diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java index 6699e9fed4..43c579d9ca 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java @@ -38,6 +38,7 @@ import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.path.PathTranslator; import org.apache.maven.settings.MavenSettingsBuilder; import org.codehaus.plexus.ArtifactEnabledContainer; +import org.codehaus.plexus.ArtifactEnabledContainerException; import org.codehaus.plexus.PlexusConstants; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.component.configurator.ComponentConfigurationException; @@ -47,6 +48,7 @@ import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator import org.codehaus.plexus.component.discovery.ComponentDiscoveryEvent; import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener; import org.codehaus.plexus.component.repository.ComponentSetDescriptor; +import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; @@ -93,6 +95,8 @@ public class DefaultPluginManager protected ComponentConfigurator configurator; + private ArtifactFactory artifactFactory; + public DefaultPluginManager() { mojoDescriptors = new HashMap(); @@ -194,80 +198,91 @@ public class DefaultPluginManager return pluginDescriptors.containsKey( PluginDescriptor.constructPluginKey( groupId, artifactId ) ); } - // TODO: don't throw Exception public void verifyPluginForGoal( String goalName, MavenSession session ) - throws Exception + throws PluginNotFoundException, PluginManagerException { String pluginId = PluginDescriptor.getPluginIdFromGoal( goalName ); verifyPlugin( PluginDescriptor.getDefaultPluginGroupId(), pluginId, session ); } - // TODO: don't throw Exception public void verifyPlugin( String groupId, String artifactId, MavenSession session ) - throws Exception + throws PluginNotFoundException, PluginManagerException { if ( !isPluginInstalled( groupId, artifactId ) ) { - ArtifactFactory artifactFactory = null; + MavenProject project = session.getProject(); + + org.apache.maven.model.Plugin pluginConfig = null; + + for ( Iterator it = project.getPlugins().iterator(); it.hasNext(); ) + { + org.apache.maven.model.Plugin plugin = (org.apache.maven.model.Plugin) it.next(); + + if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) ) + { + pluginConfig = plugin; + + break; + } + } + + String version = null; + + if ( pluginConfig != null ) + { + if ( StringUtils.isEmpty( pluginConfig.getVersion() ) ) + { + // The model/project builder should have validated this already + String message = "The maven plugin with groupId: '" + groupId + "' and artifactId: '" + artifactId + + "' which was configured for use in this project does not have a version associated with it."; + throw new IllegalStateException( message ); + } + else + { + version = pluginConfig.getVersion(); + } + } + try { - MavenProject project = session.getProject(); - - org.apache.maven.model.Plugin pluginConfig = null; - - for ( Iterator it = project.getPlugins().iterator(); it.hasNext(); ) - { - org.apache.maven.model.Plugin plugin = (org.apache.maven.model.Plugin) it.next(); - - if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) ) - { - pluginConfig = plugin; - - break; - } - } - - String version = null; - - if ( pluginConfig != null ) - { - if ( StringUtils.isEmpty( pluginConfig.getVersion() ) ) - { - throw new PluginVersionNotConfiguredException( groupId, artifactId ); - } - else - { - version = pluginConfig.getVersion(); - } - } - - // TODO: Default over to a sensible value (is 1.0-SNAPSHOT right?) Hardcoging of group ID also - if ( StringUtils.isEmpty( version ) ) - { - version = "1.0-SNAPSHOT"; - } - - artifactFactory = (ArtifactFactory) container.lookup( ArtifactFactory.ROLE ); - Artifact pluginArtifact = artifactFactory.createArtifact( groupId, artifactId, version, null, MAVEN_PLUGIN, null ); addPlugin( pluginArtifact, session ); } + catch ( ArtifactEnabledContainerException e ) + { + throw new PluginManagerException( "Error occurred in the artifact container attempting to download plugin " + + groupId + ":" + artifactId, e ); + } + catch ( ArtifactResolutionException e ) + { + throw new PluginNotFoundException( groupId, artifactId, version, e ); + } + catch ( ComponentLookupException e ) + { + throw new PluginManagerException( "Internal configuration error while retrieving " + groupId + ":" + artifactId, e ); + } finally { if ( artifactFactory != null ) { - container.release( artifactFactory ); + try + { + container.release( artifactFactory ); + } + catch ( ComponentLifecycleException e ) + { + getLogger().error( "Error releasing component - ignoring", e ); + } } } } } - // TODO: don't throw Exception protected void addPlugin( Artifact pluginArtifact, MavenSession session ) - throws Exception + throws ArtifactEnabledContainerException, ArtifactResolutionException, ComponentLookupException { ArtifactResolver artifactResolver = null; MavenProjectBuilder mavenProjectBuilder = null; @@ -287,14 +302,27 @@ public class DefaultPluginManager } finally { - // TODO: watch out for the exceptions being thrown if ( artifactResolver != null ) { - container.release( artifactResolver ); + try + { + container.release( artifactResolver ); + } + catch ( ComponentLifecycleException e ) + { + getLogger().error( "Error releasing component - ignoring", e ); + } } if ( mavenProjectBuilder != null ) { - container.release( mavenProjectBuilder ); + try + { + container.release( mavenProjectBuilder ); + } + catch ( ComponentLifecycleException e ) + { + getLogger().error( "Error releasing component - ignoring", e ); + } } } } @@ -304,7 +332,7 @@ public class DefaultPluginManager // ---------------------------------------------------------------------- public void executeMojo( MavenSession session, String goalName ) - throws PluginExecutionException + throws PluginExecutionException, PluginNotFoundException { try { @@ -400,7 +428,7 @@ public class DefaultPluginManager { if ( newMojoTechnique ) { - Map map = getPluginConfigurationFromExpressions( mojoDescriptor, configuration, session, + Map map = getPluginConfigurationFromExpressions( mojoDescriptor, configuration, expressionEvaluator ); populatePluginFields( plugin, configuration, map, expressionEvaluator ); @@ -409,7 +437,7 @@ public class DefaultPluginManager { getLogger().warn( "WARNING: The mojo " + mojoDescriptor.getId() + " is using the OLD API" ); - Map map = getPluginConfigurationFromExpressions( mojoDescriptor, configuration, session, + Map map = getPluginConfigurationFromExpressions( mojoDescriptor, configuration, expressionEvaluator ); request = createPluginRequest( configuration, map ); @@ -641,7 +669,7 @@ public class DefaultPluginManager * @deprecated */ private Map getPluginConfigurationFromExpressions( MojoDescriptor goal, PlexusConfiguration configuration, - MavenSession session, ExpressionEvaluator expressionEvaluator ) + ExpressionEvaluator expressionEvaluator ) throws ExpressionEvaluationException, PluginConfigurationException { List parameters = goal.getParameters(); @@ -742,7 +770,7 @@ public class DefaultPluginManager ArtifactFilter filter = new ScopeArtifactFilter( scope ); boolean systemOnline = !context.getSettings().getActiveProfile().isOffline(); - + ArtifactResolutionResult result = artifactResolver.resolveTransitively( project.getArtifacts(), context.getRemoteRepositories(), context.getLocalRepository(), diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginManager.java index ce2c05d482..d71f991ac6 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/PluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginManager.java @@ -30,17 +30,15 @@ public interface PluginManager String ROLE = PluginManager.class.getName(); void executeMojo( MavenSession session, String goalName ) - throws PluginExecutionException; + throws PluginExecutionException, PluginNotFoundException; MojoDescriptor getMojoDescriptor( String goalId ); - // TODO: don't throw Exception void verifyPluginForGoal( String goalName, MavenSession session ) - throws Exception; + throws PluginNotFoundException, PluginManagerException; - // TODO: don't throw Exception void verifyPlugin( String groupId, String artifactId, MavenSession session ) - throws Exception; + throws PluginNotFoundException, PluginManagerException; PluginDescriptor getPluginDescriptor( String groupId, String artifactId ); } \ No newline at end of file diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginManagerException.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginManagerException.java new file mode 100644 index 0000000000..334b617406 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginManagerException.java @@ -0,0 +1,31 @@ +package org.apache.maven.plugin; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +/** + * Exception in the plugin manager. + * + * @author Brett Porter + * @version $Id$ + */ +public class PluginManagerException + extends Exception +{ + public PluginManagerException( String message, Exception e ) + { + } +} diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginNotFoundException.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginNotFoundException.java new file mode 100644 index 0000000000..7d367f8673 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginNotFoundException.java @@ -0,0 +1,36 @@ +package org.apache.maven.plugin; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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 org.apache.maven.artifact.resolver.ArtifactResolutionException; + +/** + * Exception occurring trying to resolve a plugin. + * + * @author Brett Porter + * @version $Id$ + */ +public class PluginNotFoundException + extends Throwable +{ + public PluginNotFoundException( String groupId, String artifactId, String version, ArtifactResolutionException e ) + { + super( + "Plugin could not found in any remote repositories: [" + groupId + ":" + artifactId + ":" + version + "]", + e ); + } +} diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginVersionNotConfiguredException.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginVersionNotConfiguredException.java deleted file mode 100644 index 33b1ac0d50..0000000000 --- a/maven-core/src/main/java/org/apache/maven/plugin/PluginVersionNotConfiguredException.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.apache.maven.plugin; - -/* ==================================================================== - * Copyright 2001-2004 The Apache Software Foundation. - * - * Licensed 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. - * ==================================================================== - */ - -/** - * @author jdcasey - */ -public class PluginVersionNotConfiguredException - extends Exception -{ - - private final String groupId; - - private final String artifactId; - - public PluginVersionNotConfiguredException( String groupId, String artifactId ) - { - super( "The maven plugin with groupId: \'" + groupId + "\' and artifactId: \'" + artifactId - + "\' which was configured for use in this project does not have a version associated with it." ); - - this.groupId = groupId; - - this.artifactId = artifactId; - } - - public String getGroupId() - { - return groupId; - } - - public String getArtifactId() - { - return artifactId; - } - -} \ No newline at end of file diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index d9683360c1..472ee6f30a 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -127,16 +127,7 @@ public class DefaultMavenProjectBuilder // Always cache files in the source tree over those in the repository modelCache.put( createCacheKey( model.getGroupId(), model.getArtifactId(), model.getVersion() ), model ); - Settings settings = null; - - try - { - settings = mavenSettingsBuilder.buildSettings(); - } - catch ( Exception e ) - { - throw new ProjectBuildingException( "Cannot read settings.", e ); - } + Settings settings = readSettings(); boolean systemOnline = !settings.getActiveProfile().isOffline(); @@ -156,8 +147,13 @@ public class DefaultMavenProjectBuilder return project; } - public MavenProject buildFromRepository( Artifact artifact, List remoteArtifactRepositories, - ArtifactRepository localRepository ) + /** + * @return + * @throws ProjectBuildingException + * @todo shouldn't be re-reading all the time - perhaps cache, but check a timestamp so you can detect and reload on + * changes in a long running process + */ + private Settings readSettings() throws ProjectBuildingException { Settings settings = null; @@ -170,6 +166,14 @@ public class DefaultMavenProjectBuilder { throw new ProjectBuildingException( "Cannot read settings.", e ); } + return settings; + } + + public MavenProject buildFromRepository( Artifact artifact, List remoteArtifactRepositories, + ArtifactRepository localRepository ) + throws ProjectBuildingException + { + Settings settings = readSettings(); Model model = findModelFromRepository( artifact, remoteArtifactRepositories, localRepository ); @@ -304,16 +308,6 @@ public class DefaultMavenProjectBuilder if ( resolveDependencies ) { - Settings settings; - try - { - settings = mavenSettingsBuilder.buildSettings(); - } - catch ( Exception e ) - { - throw new ProjectBuildingException( "Cannot read settings.", e ); - } - MavenMetadataSource sourceReader = new MavenMetadataSource( artifactResolver, this ); ArtifactResolutionResult result = artifactResolver.resolveTransitively( project.getArtifacts(), @@ -385,16 +379,7 @@ public class DefaultMavenProjectBuilder private List buildArtifactRepositories( List repositories ) throws ProjectBuildingException { - Settings settings = null; - - try - { - settings = mavenSettingsBuilder.buildSettings(); - } - catch ( Exception e ) - { - throw new ProjectBuildingException( "Cannot read settings.", e ); - } + Settings settings = readSettings(); List repos = new ArrayList(); @@ -402,20 +387,7 @@ public class DefaultMavenProjectBuilder { Repository mavenRepo = (Repository) i.next(); - String layout = mavenRepo.getLayout(); - - ArtifactRepositoryLayout remoteRepoLayout = null; - try - { - remoteRepoLayout = - (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE, layout ); - } - catch ( ComponentLookupException e ) - { - throw new ProjectBuildingException( "Cannot find layout implementation corresponding to: \'" + layout + - "\' for remote repository with id: \'" + mavenRepo.getId() + "\'.", - e ); - } + ArtifactRepositoryLayout remoteRepoLayout = getRepositoryLayout( mavenRepo ); ArtifactRepository artifactRepo = artifactRepositoryFactory.createArtifactRepository( mavenRepo, settings, remoteRepoLayout ); @@ -429,30 +401,17 @@ public class DefaultMavenProjectBuilder } private List buildPluginRepositories( List pluginRepositories ) - throws Exception + throws ProjectBuildingException { List remotePluginRepositories = new ArrayList(); - Settings settings = mavenSettingsBuilder.buildSettings(); + Settings settings = readSettings(); for ( Iterator it = pluginRepositories.iterator(); it.hasNext(); ) { Repository mavenRepo = (Repository) it.next(); - String layout = mavenRepo.getLayout(); - - ArtifactRepositoryLayout repositoryLayout = null; - try - { - repositoryLayout = - (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE, layout ); - } - catch ( ComponentLookupException e ) - { - throw new ProjectBuildingException( "Cannot find layout implementation corresponding to: \'" + layout + - "\' for remote repository with id: \'" + mavenRepo.getId() + "\'.", - e ); - } + ArtifactRepositoryLayout repositoryLayout = getRepositoryLayout( mavenRepo ); ArtifactRepository pluginRepository = artifactRepositoryFactory.createArtifactRepository( mavenRepo, settings, @@ -465,20 +424,35 @@ public class DefaultMavenProjectBuilder return remotePluginRepositories; } + private ArtifactRepositoryLayout getRepositoryLayout( Repository mavenRepo ) + throws ProjectBuildingException + { + String layout = mavenRepo.getLayout(); + + ArtifactRepositoryLayout repositoryLayout = null; + try + { + repositoryLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE, layout ); + } + catch ( ComponentLookupException e ) + { + throw new ProjectBuildingException( "Cannot find layout implementation corresponding to: \'" + layout + + "\' for remote repository with id: \'" + mavenRepo.getId() + "\'.", e ); + } + return repositoryLayout; + } + private ArtifactRepository buildDistributionManagementRepository( Repository dmRepo ) - throws Exception + throws ProjectBuildingException { if ( dmRepo == null ) { return null; } - Settings settings = mavenSettingsBuilder.buildSettings(); + Settings settings = readSettings(); - String repoLayoutId = dmRepo.getLayout(); - - ArtifactRepositoryLayout repositoryLayout = (ArtifactRepositoryLayout) container.lookup( - ArtifactRepositoryLayout.ROLE, repoLayoutId ); + ArtifactRepositoryLayout repositoryLayout = getRepositoryLayout( dmRepo ); ArtifactRepository dmArtifactRepository = artifactRepositoryFactory.createArtifactRepository( dmRepo, settings, repositoryLayout ); diff --git a/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java b/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java index e67d332833..33b5f37179 100644 --- a/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java @@ -21,9 +21,12 @@ import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileReader; +import java.io.IOException; /** * @author jdcasey @@ -57,9 +60,8 @@ public class DefaultMavenSettingsBuilder // MavenSettingsBuilder Implementation // ---------------------------------------------------------------------- - // TODO: don't throw Exception. public Settings buildSettings() - throws Exception + throws IOException, XmlPullParserException { Settings settings = null; @@ -74,6 +76,11 @@ public class DefaultMavenSettingsBuilder settings = modelReader.read( reader ); } + catch ( FileNotFoundException e ) + { + // Not possible - just ignore + getLogger().warn( "Settings file disappeared - ignoring", e ); + } finally { IOUtil.close( reader ); diff --git a/maven-core/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java b/maven-core/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java index a5cf99f981..1098680302 100644 --- a/maven-core/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java @@ -1,5 +1,9 @@ package org.apache.maven.settings; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.IOException; + /* * Copyright 2001-2005 The Apache Software Foundation. * @@ -24,7 +28,6 @@ public interface MavenSettingsBuilder { String ROLE = MavenSettingsBuilder.class.getName(); - // TODO: Don't throw Exception. Settings buildSettings() - throws Exception; + throws IOException, XmlPullParserException; } diff --git a/maven-core/src/main/resources/META-INF/plexus/components.xml b/maven-core/src/main/resources/META-INF/plexus/components.xml index 7513104aff..c3ffd68d55 100644 --- a/maven-core/src/main/resources/META-INF/plexus/components.xml +++ b/maven-core/src/main/resources/META-INF/plexus/components.xml @@ -117,10 +117,6 @@ | | --> - - org.apache.maven.artifact.factory.ArtifactFactory - org.apache.maven.artifact.factory.DefaultArtifactFactory - + + org.apache.maven.artifact.factory.ArtifactFactory + org.apache.maven.artifact.factory.DefaultArtifactFactory + diff --git a/maven-core/src/main/resources/META-INF/plexus/plexus.xml b/maven-core/src/main/resources/META-INF/plexus/plexus.xml index 8ca9d3adbf..17f3419729 100644 --- a/maven-core/src/main/resources/META-INF/plexus/plexus.xml +++ b/maven-core/src/main/resources/META-INF/plexus/plexus.xml @@ -33,6 +33,9 @@ org.codehaus.plexus.component.configurator.ComponentConfigurator + + org.apache.maven.artifact.factory.ArtifactFactory + @@ -50,5 +53,9 @@ ${user.home}/.m2/settings.xml + + org.apache.maven.artifact.factory.ArtifactFactory + org.apache.maven.artifact.factory.DefaultArtifactFactory + diff --git a/maven-mboot2/pom.xml b/maven-mboot2/pom.xml index ccde5d8745..bb158b6e10 100644 --- a/maven-mboot2/pom.xml +++ b/maven-mboot2/pom.xml @@ -12,7 +12,7 @@ org.codehaus.modello modello-plugin-xpp3 - 1.0-alpha-1 + 1.0-alpha-2-SNAPSHOT runtime diff --git a/maven-plugins/maven-clean-plugin/src/main/java/org/apache/maven/plugin/clean/CleanPlugin.java b/maven-plugins/maven-clean-plugin/src/main/java/org/apache/maven/plugin/clean/CleanPlugin.java index 458c6e0e05..b37cdd0459 100644 --- a/maven-plugins/maven-clean-plugin/src/main/java/org/apache/maven/plugin/clean/CleanPlugin.java +++ b/maven-plugins/maven-clean-plugin/src/main/java/org/apache/maven/plugin/clean/CleanPlugin.java @@ -97,7 +97,6 @@ public class CleanPlugin * @param d the directory to delete */ protected void removeDir( File d ) - throws Exception { String[] list = d.list(); if ( list == null ) @@ -124,7 +123,7 @@ public class CleanPlugin // } // else // { - getLog().info( message ); + getLog().info( message ); // } } } @@ -140,7 +139,7 @@ public class CleanPlugin // } // else // { - getLog().info( message ); + getLog().info( message ); // } } }