From 864ea152167c241e2cb0593713b341c4c8e5aa7e Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Wed, 10 Jun 2009 13:40:14 +0000 Subject: [PATCH] o refactoring the way plugin prefix metadata is found. the lifecycle executor is not the right place for this and the plugin manager is now strictly for already fully configured executions. so it belongs somewhere specific to augmenting the base set of information in order to make it complete but this first pass will do here. i just want to check it in as well because i'm traveling. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@783361 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/maven/DefaultMaven.java | 9 +- .../src/main/java/org/apache/maven/Maven.java | 6 +- .../apache/maven/execution/BuildFailure.java | 57 -- .../apache/maven/execution/BuildSuccess.java | 51 -- .../lifecycle/DefaultLifecycleExecutor.java | 746 ++++++++++-------- .../maven/lifecycle/LifecycleExecutor.java | 2 - .../NoPluginFoundForPrefixException.java | 15 +- .../maven/plugin/PluginNotFoundException.java | 2 +- .../lifecycle/LifecycleExecutorTest.java | 9 - .../org/apache/maven/cli/CLIRequestUtils.java | 14 +- .../apache/maven/embedder/MavenEmbedder.java | 6 - ...DefaultMavenExecutionRequestPopulator.java | 8 - .../maven/embedder/MavenEmbedderTest.java | 16 - .../DelegatingLocalArtifactRepository.java | 3 +- 14 files changed, 456 insertions(+), 488 deletions(-) delete mode 100644 maven-core/src/main/java/org/apache/maven/execution/BuildFailure.java delete mode 100644 maven-core/src/main/java/org/apache/maven/execution/BuildSuccess.java 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 6ccab71526..f513aa46e7 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -64,11 +64,6 @@ public class DefaultMaven @Requirement protected PlexusContainer container; - public List getLifecyclePhases() - { - return lifecycleExecutor.getLifecyclePhases(); - } - public MavenExecutionResult execute( MavenExecutionRequest request ) { //TODO: Need a general way to inject standard properties @@ -93,7 +88,7 @@ public MavenExecutionResult execute( MavenExecutionRequest request ) try { - projects = getProjects( request ); + projects = getProjectsForMavenReactor( request ); //TODO: We really need to get rid of this requirement in here. If we know there is no project present if ( projects.isEmpty() ) @@ -171,7 +166,7 @@ private MavenExecutionResult processResult( MavenExecutionResult result, Excepti return result; } - public Map getProjects( MavenExecutionRequest request ) + protected Map getProjectsForMavenReactor( MavenExecutionRequest request ) throws MavenExecutionException, ProjectBuildingException { // We have no POM file. diff --git a/maven-core/src/main/java/org/apache/maven/Maven.java b/maven-core/src/main/java/org/apache/maven/Maven.java index ff196655c0..16fb2c1e88 100644 --- a/maven-core/src/main/java/org/apache/maven/Maven.java +++ b/maven-core/src/main/java/org/apache/maven/Maven.java @@ -19,8 +19,6 @@ * under the License. */ -import java.util.List; - import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionResult; @@ -31,7 +29,5 @@ public interface Maven { String POMv4 = "pom.xml"; - MavenExecutionResult execute( MavenExecutionRequest request ); - - List getLifecyclePhases(); + MavenExecutionResult execute( MavenExecutionRequest request ); } \ No newline at end of file diff --git a/maven-core/src/main/java/org/apache/maven/execution/BuildFailure.java b/maven-core/src/main/java/org/apache/maven/execution/BuildFailure.java deleted file mode 100644 index c8c9f95a24..0000000000 --- a/maven-core/src/main/java/org/apache/maven/execution/BuildFailure.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.apache.maven.execution; - -/* - * 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. - */ - -/** - * Describe a build failure in the reactor. - * - * @author Brett Porter - * @version $Id$ - */ -public class BuildFailure -{ - private final Exception cause; - - private final String task; - - private final long time; - - BuildFailure( Exception cause, String task, long time ) - { - this.cause = cause; - this.task = task; - this.time = time; - } - - public String getTask() - { - return task; - } - - public Exception getCause() - { - return cause; - } - - public long getTime() - { - return time; - } -} diff --git a/maven-core/src/main/java/org/apache/maven/execution/BuildSuccess.java b/maven-core/src/main/java/org/apache/maven/execution/BuildSuccess.java deleted file mode 100644 index 40accc9097..0000000000 --- a/maven-core/src/main/java/org/apache/maven/execution/BuildSuccess.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.apache.maven.execution; - -/* - * 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 org.apache.maven.project.MavenProject; - -/** - * Describe a build success in the reactor. - * - * @author Brett Porter - * @version $Id$ - */ -public class BuildSuccess -{ - private final MavenProject project; - - private final long time; - - public BuildSuccess( MavenProject project, long time ) - { - this.project = project; - this.time = time; - } - - public MavenProject getProject() - { - return project; - } - - public long getTime() - { - return time; - } -} 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 d50b73e20c..f9e96d7e29 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 @@ -68,14 +68,8 @@ import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; -//TODO: The configuration for the lifecycle needs to be externalized so that I can use the annotations -// properly for the wiring and reference and external source for the lifecycle configuration. +//TODO: The configuration for the lifecycle needs to be externalized so that I can use the annotations properly for the wiring and reference and external source for the lifecycle configuration. //TODO: check for online status in the build plan and die if necessary -//TODO if ( mojoDescriptor.isProjectRequired() && !session.isUsingPOMsFromFilesystem() ) -//{ -// throw new PluginExecutionException( mojoExecution, project, "Cannot execute mojo: " + mojoDescriptor.getGoal() -// + ". It requires a project with an existing pom.xml, but the build is not using one." ); -//} /** * @author Jason van Zyl @@ -92,6 +86,25 @@ public class DefaultLifecycleExecutor @Requirement protected RepositorySystem repositorySystem; + @Requirement + private ProjectDependenciesResolver projectDependenciesResolver; + + // @Configuration(source="org/apache/maven/lifecycle/lifecycles.xml") + private List lifecycles; + + /** + * We use this to display all the lifecycles available and their phases to users. Currently this is primarily + * used in the IDE integrations where a UI is presented to the user and they can select the lifecycle phase + * they would like to execute. + */ + private Map lifecycleMap; + + /** + * We use this to map all phases to the lifecycle that contains it. This is used so that a user can specify the + * phase they want to execute and we can easily determine what lifecycle we need to run. + */ + private Map phaseToLifecycleMap; + /** * These mappings correspond to packaging types, like WAR packaging, which configure a particular mojos * to run in a given phase. @@ -99,16 +112,6 @@ public class DefaultLifecycleExecutor @Requirement private Map lifecycleMappings; - @Requirement - private ProjectDependenciesResolver projectDependenciesResolver; - - // @Configuration(source="org/apache/maven/lifecycle/lifecycles.xml") - private List lifecycles; - - private Map lifecycleMap; - - private Map phaseToLifecycleMap; - public void execute( MavenSession session ) { // TODO: Use a listener here instead of loggers @@ -215,203 +218,217 @@ public void execute( MavenSession session ) } } } - - // 1. Find the lifecycle given the phase (default lifecycle when given install) - // 2. Find the lifecycle mapping that corresponds to the project packaging (jar lifecycle mapping given the jar packaging) - // 3. Find the mojos associated with the lifecycle given the project packaging (jar lifecycle mapping for the default lifecycle) - // 4. Bind those mojos found in the lifecycle mapping for the packaging to the lifecycle - // 5. Bind mojos specified in the project itself to the lifecycle + public MavenExecutionPlan calculateExecutionPlan( MavenSession session, String... tasks ) throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException, CycleDetectedInPluginGraphException, MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException, PluginManagerException - { + { MavenProject project = session.getCurrentProject(); - - List phasesWithMojosToExecute = new ArrayList(); - + List lifecyclePlan = new ArrayList(); - + String requiredDependencyResolutionScope = null; - + for ( String task : tasks ) { - if ( task.indexOf( ":" ) > 0 ) { - // If this is a goal like "mvn modello:java" and the POM looks like the following: - - // - // 4.0.0 - // org.apache.maven.plugins - // project-plugin-level-configuration-only - // 1.0.1 - // - // - // - // org.codehaus.modello - // modello-maven-plugin - // 1.0.1 - // - // 1.1.0 - // - // src/main/mdo/remote-resources.mdo - // - // - // - // - // - // - // - // We want to - // - // - take the plugin/configuration in the POM and merge it with the plugin's default configuration found in its plugin.xml - // - attach that to the MojoExecution for its configuration - // - give the MojoExecution an id of default-. - - MojoDescriptor mojoDescriptor = getMojoDescriptor( task, session ); - - requiredDependencyResolutionScope = mojoDescriptor.isDependencyResolutionRequired(); - - MojoExecution mojoExecution = new MojoExecution( mojoDescriptor, "default-" + mojoDescriptor.getGoal() ); - - populateMojoExecutionConfiguration( project, mojoExecution, true ); - - lifecyclePlan.add( mojoExecution ); + calculateExecutionForIndividualGoal( session, lifecyclePlan, task ); } else { - // 1. - // - // Based on the lifecycle phase we are given, let's find the corresponding lifecycle. - // - Lifecycle lifecycle = phaseToLifecycleMap.get( task ); - - // 2. - // - // If we are dealing with the "clean" or "site" lifecycle then there are currently no lifecycle mappings but there are default phases - // that need to be run instead. - // - // Now we need to take into account the packaging type of the project. For a project of type WAR, the lifecycle where mojos are mapped - // on to the given phases in the lifecycle are going to be a little different then, say, a project of type JAR. - // - - // 3. - // - // Once we have the lifecycle mapping for the given packaging, we need to know whats phases we need to worry about executing. - // - - // Create an ordered Map of the phases in the lifecycle to a list of mojos to execute. - Map> phaseToMojoMapping = new LinkedHashMap>(); - - // 4. - - //TODO: need to separate the lifecycles - - for ( String phase : lifecycle.getPhases() ) - { - List mojos = new ArrayList(); - - //TODO: remove hard coding - if ( phase.equals( "clean" ) ) - { - Plugin plugin = new Plugin(); - plugin.setGroupId( "org.apache.maven.plugins" ); - plugin.setArtifactId( "maven-clean-plugin" ); - plugin.setVersion( "2.3" ); - mojos.add( new MojoExecution( plugin, "clean", "default-clean" ) ); - } - - // This is just just laying out the initial structure of the mojos to run in each phase of the - // lifecycle. Everything is now done in the project builder correctly so this could likely - // go away shortly. We no longer need to pull out bits from the default lifecycle. The MavenProject - // comes to us intact as it should. - - phaseToMojoMapping.put( phase, mojos ); - } - - // 5. Just build up the list of mojos that will execute for every phase. - // - // This will be useful for having the complete build plan and then we can filter/optimize later. - // - for ( Plugin plugin : project.getBuild().getPlugins() ) - { - for ( PluginExecution execution : plugin.getExecutions() ) - { - // if the phase is specified then I don't have to go fetch the plugin yet and pull it down - // to examine the phase it is associated to. - if ( execution.getPhase() != null ) - { - for ( String goal : execution.getGoals() ) - { - if ( phaseToMojoMapping.get( execution.getPhase() ) == null ) - { - // This is happening because executions in the POM are getting mixed into the clean lifecycle - // So for the lifecycle mapping we need a map with the phases as keys so we can easily check - // if this phase belongs to the given lifecycle. this shows the system is messed up. this - // shouldn't happen. - phaseToMojoMapping.put( execution.getPhase(), new ArrayList() ); - } - - MojoExecution mojoExecution = new MojoExecution( plugin, goal, execution.getId() ); - phaseToMojoMapping.get( execution.getPhase() ).add( mojoExecution ); - } - } - // if not then i need to grab the mojo descriptor and look at the phase that is specified - else - { - for ( String goal : execution.getGoals() ) - { - MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getLocalRepository(), project.getRemoteArtifactRepositories() ); - - if ( mojoDescriptor.getPhase() != null && phaseToMojoMapping.get( mojoDescriptor.getPhase() ) != null ) - { - MojoExecution mojoExecution = new MojoExecution( plugin, goal, execution.getId() ); - phaseToMojoMapping.get( mojoDescriptor.getPhase() ).add( mojoExecution ); - } - } - } - } - } - - // 6. - // - // We are only interested in the phases that correspond to the lifecycle we are trying to run. If we are running the "clean" - // lifecycle we are not interested in goals -- like "generate-sources -- that belong to the default lifecycle. - // - for ( String phase : phaseToMojoMapping.keySet() ) - { - phasesWithMojosToExecute.addAll( phaseToMojoMapping.get( phase ) ); - - if ( phase.equals( task ) ) - { - break; - } - } + calculateExecutionForLifecyclePhase( session, lifecyclePlan, task ); } } - + // 7. Now we create the correct configuration for the mojo to execute. - //TODO: this needs to go to the model builder. - //TODO: just used a hollowed out MojoExecution - for ( MojoExecution mojoExecution : phasesWithMojosToExecute ) + // + for ( MojoExecution mojoExecution : lifecyclePlan ) { // These are bits that look like this: // // org.apache.maven.plugins:maven-remote-resources-plugin:1.0:process // - MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( - mojoExecution.getPlugin(), mojoExecution.getGoal(), session.getLocalRepository(), project.getPluginArtifactRepositories() ); + MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( mojoExecution.getGroupId(), mojoExecution.getArtifactId(), mojoExecution.getVersion(), mojoExecution.getGoal(), session + .getLocalRepository(), project.getRemoteArtifactRepositories() ); + + PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor(); + if ( pluginDescriptor.getPlugin().isExtensions() ) + { + pluginDescriptor.setClassRealm( pluginManager.getPluginRealm( session, pluginDescriptor ) ); + } + + requiredDependencyResolutionScope = calculateRequiredDependencyResolutionScope( requiredDependencyResolutionScope, mojoDescriptor.isDependencyResolutionRequired() ); - requiredDependencyResolutionScope = calculateRequiredDependencyResolutionScope( requiredDependencyResolutionScope, mojoDescriptor.isDependencyResolutionRequired() ); - mojoExecution.setMojoDescriptor( mojoDescriptor ); - - populateMojoExecutionConfiguration( project, mojoExecution, false ); - lifecyclePlan.add( mojoExecution ); - } + populateMojoExecutionConfiguration( project, mojoExecution, false ); + } + + return new MavenExecutionPlan( lifecyclePlan, requiredDependencyResolutionScope ); + } + + private void calculateExecutionForIndividualGoal( MavenSession session, List lifecyclePlan, String goal ) + throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException, CycleDetectedInPluginGraphException, MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException + { + MavenProject project = session.getCurrentProject(); - return new MavenExecutionPlan( lifecyclePlan, requiredDependencyResolutionScope ); - } + // If this is a goal like "mvn modello:java" and the POM looks like the following: + // + // + // 4.0.0 + // org.apache.maven.plugins + // project-plugin-level-configuration-only + // 1.0.1 + // + // + // + // org.codehaus.modello + // modello-maven-plugin + // 1.0.1 + // + // 1.1.0 + // + // src/main/mdo/remote-resources.mdo + // + // + // + // + // + // + // + // We want to + // + // - take the plugin/configuration in the POM and merge it with the plugin's default configuration found in its plugin.xml + // - attach that to the MojoExecution for its configuration + // - give the MojoExecution an id of default-. + + MojoDescriptor mojoDescriptor = getMojoDescriptor( goal, session ); + + MojoExecution mojoExecution = new MojoExecution( mojoDescriptor, "default-" + mojoDescriptor.getGoal() ); + + populateMojoExecutionConfiguration( project, mojoExecution, true ); + + lifecyclePlan.add( mojoExecution ); + } + + // 1. Find the lifecycle given the phase (default lifecycle when given install) + // 2. Find the lifecycle mapping that corresponds to the project packaging (jar lifecycle mapping given the jar packaging) + // 3. Find the mojos associated with the lifecycle given the project packaging (jar lifecycle mapping for the default lifecycle) + // 4. Bind those mojos found in the lifecycle mapping for the packaging to the lifecycle + // 5. Bind mojos specified in the project itself to the lifecycle + private void calculateExecutionForLifecyclePhase( MavenSession session, List lifecyclePlan, String lifecyclePhase ) + throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException, CycleDetectedInPluginGraphException, MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException + { + MavenProject project = session.getCurrentProject(); + + // 1. + // + // Based on the lifecycle phase we are given, let's find the corresponding lifecycle. + // + Lifecycle lifecycle = phaseToLifecycleMap.get( lifecyclePhase ); + + // 2. + // + // If we are dealing with the "clean" or "site" lifecycle then there are currently no lifecycle mappings but there are default phases + // that need to be run instead. + // + // Now we need to take into account the packaging type of the project. For a project of type WAR, the lifecycle where mojos are mapped + // on to the given phases in the lifecycle are going to be a little different then, say, a project of type JAR. + // + + // 3. + // + // Once we have the lifecycle mapping for the given packaging, we need to know whats phases we need to worry about executing. + // + + // Create an ordered Map of the phases in the lifecycle to a list of mojos to execute. + Map> phaseToMojoMapping = new LinkedHashMap>(); + + // 4. + + //TODO: need to separate the lifecycles + + for ( String phase : lifecycle.getPhases() ) + { + List mojos = new ArrayList(); + + //TODO: remove hard coding + if ( phase.equals( "clean" ) ) + { + Plugin plugin = new Plugin(); + plugin.setGroupId( "org.apache.maven.plugins" ); + plugin.setArtifactId( "maven-clean-plugin" ); + plugin.setVersion( "2.3" ); + mojos.add( new MojoExecution( plugin, "clean", "default-clean" ) ); + } + + // This is just just laying out the initial structure of the mojos to run in each phase of the + // lifecycle. Everything is now done in the project builder correctly so this could likely + // go away shortly. We no longer need to pull out bits from the default lifecycle. The MavenProject + // comes to us intact as it should. + + phaseToMojoMapping.put( phase, mojos ); + } + + // 5. Just build up the list of mojos that will execute for every phase. + // + // This will be useful for having the complete build plan and then we can filter/optimize later. + // + for ( Plugin plugin : project.getBuild().getPlugins() ) + { + for ( PluginExecution execution : plugin.getExecutions() ) + { + // if the phase is specified then I don't have to go fetch the plugin yet and pull it down + // to examine the phase it is associated to. + if ( execution.getPhase() != null ) + { + for ( String goal : execution.getGoals() ) + { + if ( phaseToMojoMapping.get( execution.getPhase() ) == null ) + { + // This is happening because executions in the POM are getting mixed into the clean lifecycle + // So for the lifecycle mapping we need a map with the phases as keys so we can easily check + // if this phase belongs to the given lifecycle. this shows the system is messed up. this + // shouldn't happen. + phaseToMojoMapping.put( execution.getPhase(), new ArrayList() ); + } + + MojoExecution mojoExecution = new MojoExecution( plugin, goal, execution.getId() ); + phaseToMojoMapping.get( execution.getPhase() ).add( mojoExecution ); + } + } + // if not then i need to grab the mojo descriptor and look at the phase that is specified + else + { + for ( String goal : execution.getGoals() ) + { + MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getLocalRepository(), project.getRemoteArtifactRepositories() ); + + if ( mojoDescriptor.getPhase() != null && phaseToMojoMapping.get( mojoDescriptor.getPhase() ) != null ) + { + MojoExecution mojoExecution = new MojoExecution( plugin, goal, execution.getId() ); + phaseToMojoMapping.get( mojoDescriptor.getPhase() ).add( mojoExecution ); + } + } + } + } + } + + // 6. + // + // We are only interested in the phases that correspond to the lifecycle we are trying to run. If we are running the "clean" + // lifecycle we are not interested in goals -- like "generate-sources -- that belong to the default lifecycle. + // + for ( String phase : phaseToMojoMapping.keySet() ) + { + lifecyclePlan.addAll( phaseToMojoMapping.get( phase ) ); + + if ( phase.equals( lifecyclePhase ) ) + { + break; + } + } + } // SCOPE_COMPILE // SCOPE_TEST @@ -567,10 +584,49 @@ MojoDescriptor getMojoDescriptor( String task, MavenSession session ) Plugin plugin = null; StringTokenizer tok = new StringTokenizer( task, ":" ); + int numTokens = tok.countTokens(); + if ( numTokens == 4 ) + { + // We have everything that we need + // + // org.apache.maven.plugins:maven-remote-resources-plugin:1.0:process + // + // groupId + // artifactId + // version + // goal + // + plugin = new Plugin(); + plugin.setGroupId( tok.nextToken() ); + plugin.setArtifactId( tok.nextToken() ); + plugin.setVersion( tok.nextToken() ); + goal = tok.nextToken(); + + } + else if ( numTokens == 3 ) + { + // We have everything that we need except the version + // + // org.apache.maven.plugins:maven-remote-resources-plugin:???:process + // + // groupId + // artifactId + // ??? + // goal + // + plugin = new Plugin(); + plugin.setGroupId( tok.nextToken() ); + plugin.setArtifactId( tok.nextToken() ); + goal = tok.nextToken(); + } if ( numTokens == 2 ) { + // We have a prefix and goal + // + // idea:idea + // String prefix = tok.nextToken(); goal = tok.nextToken(); @@ -585,78 +641,69 @@ MojoDescriptor getMojoDescriptor( String task, MavenSession session ) plugin = findPluginForPrefix( prefix, session ); } - else if ( numTokens == 3 || numTokens == 4 ) - { - plugin = new Plugin(); - plugin.setGroupId( tok.nextToken() ); - plugin.setArtifactId( tok.nextToken() ); - - if ( numTokens == 4 ) - { - plugin.setVersion( tok.nextToken() ); - } - - goal = tok.nextToken(); - } if ( plugin.getVersion() == null ) { // We need to get it from the POM first before anything else // - for ( Plugin pluginInPom : project.getBuildPlugins() ) - { - if ( pluginInPom.getArtifactId().equals( plugin.getArtifactId() ) ) - { - plugin.setVersion( pluginInPom.getVersion() ); - break; - } - } - - if ( plugin.getVersion() == null && project.getPluginManagement() != null ) - { - for ( Plugin pluginInPom : project.getPluginManagement().getPlugins() ) - { - if ( pluginInPom.getArtifactId().equals( plugin.getArtifactId() ) ) - { - plugin.setVersion( pluginInPom.getVersion() ); - break; - } - } - } + plugin.setVersion( attemptToGetPluginVersionFromProject( plugin, project ) ); // If there is no version to be found then we need to look in the repository metadata for // this plugin and see what's specified as the latest release. // if ( plugin.getVersion() == null ) { - for ( ArtifactRepository repository : session.getCurrentProject().getRemoteArtifactRepositories() ) - { - String localPath = plugin.getGroupId().replace( '.', '/' ) + "/" + plugin.getArtifactId() + "/maven-metadata-" + repository.getId() + ".xml"; + File artifactMetadataFile; + + String localPath; + + // Search in the local repositiory for a version + // + // maven-metadata-local.xml + // + localPath = plugin.getGroupId().replace( '.', '/' ) + "/" + plugin.getArtifactId() + "/maven-metadata-" + session.getLocalRepository().getId() + ".xml"; - File destination = new File( session.getLocalRepository().getBasedir(), localPath ); - - if ( !destination.exists() ) + artifactMetadataFile = new File( session.getLocalRepository().getBasedir(), localPath ); + + if ( !artifactMetadataFile.exists() /* || user requests snapshot updates */ ) + { + // Search in remote repositories for a version. + // + // maven-metadata-{central|nexus|...}.xml + // + //TODO: we should cycle through the repositories but take the repository which actually + // satisfied the prefix. + for ( ArtifactRepository repository : project.getRemoteArtifactRepositories() ) { - try - { - String remotePath = plugin.getGroupId().replace( '.', '/' ) + "/" + plugin.getArtifactId() + "/maven-metadata.xml"; + localPath = plugin.getGroupId().replace( '.', '/' ) + "/" + plugin.getArtifactId() + "/maven-metadata-" + repository.getId() + ".xml"; - repositorySystem.retrieve( repository, destination, remotePath, session.getRequest().getTransferListener() ); - } - catch ( TransferFailedException e ) + artifactMetadataFile = new File( session.getLocalRepository().getBasedir(), localPath ); + + if ( !artifactMetadataFile.exists() ) { - continue; - } - catch ( ResourceDoesNotExistException e ) - { - continue; + try + { + String remotePath = plugin.getGroupId().replace( '.', '/' ) + "/" + plugin.getArtifactId() + "/maven-metadata.xml"; + + repositorySystem.retrieve( repository, artifactMetadataFile, remotePath, session.getRequest().getTransferListener() ); + } + catch ( TransferFailedException e ) + { + continue; + } + catch ( ResourceDoesNotExistException e ) + { + continue; + } } } + } - // We have retrieved the metadata + if ( artifactMetadataFile.exists() ) + { try { - Metadata pluginMetadata = readMetadata( destination ); + Metadata pluginMetadata = readMetadata( artifactMetadataFile ); String release = pluginMetadata.getVersioning().getRelease(); @@ -664,18 +711,55 @@ else if ( numTokens == 3 || numTokens == 4 ) { plugin.setVersion( release ); } + else + { + String latest = pluginMetadata.getVersioning().getLatest(); + + if ( latest != null ) + { + plugin.setVersion( latest ); + } + } } catch ( RepositoryMetadataReadException e ) { logger.warn( "Error reading plugin metadata: ", e ); } } + else + { + throw new PluginNotFoundException( plugin, null ); + } } } return pluginManager.getMojoDescriptor( plugin, goal, session.getLocalRepository(), project.getRemoteArtifactRepositories() ); } + private String attemptToGetPluginVersionFromProject( Plugin plugin, MavenProject project ) + { + for ( Plugin pluginInPom : project.getBuildPlugins() ) + { + if ( pluginInPom.getArtifactId().equals( plugin.getArtifactId() ) ) + { + return pluginInPom.getVersion(); + } + } + + if ( plugin.getVersion() == null && project.getPluginManagement() != null ) + { + for ( Plugin pluginInPom : project.getPluginManagement().getPlugins() ) + { + if ( pluginInPom.getArtifactId().equals( plugin.getArtifactId() ) ) + { + return pluginInPom.getVersion(); + } + } + } + + return null; + } + public void initialize() throws InitializationException { @@ -700,20 +784,7 @@ public void initialize() lifecycleMap.put( lifecycle.getId(), lifecycle ); } } - - public List getLifecyclePhases() - { - for ( Lifecycle lifecycle : lifecycles ) - { - if ( lifecycle.getId().equals( "default" ) ) - { - return lifecycle.getPhases(); - } - } - - return null; - } - + // These methods deal with construction intact Plugin object that look like they come from a standard // block in a Maven POM. We have to do some wiggling to pull the sources of information // together and this really shows the problem of constructing a sensible default configuration but @@ -798,7 +869,7 @@ private void populateDefaultConfigurationForPlugin( Plugin plugin, ArtifactRepos { for( String goal : pluginExecution.getGoals() ) { - Xpp3Dom dom = getDefaultPluginConfiguration( plugin, goal, localRepository, remoteRepositories ); + Xpp3Dom dom = getDefaultPluginConfiguration( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(), goal, localRepository, remoteRepositories ); pluginExecution.setConfiguration( Xpp3Dom.mergeXpp3Dom( (Xpp3Dom) pluginExecution.getConfiguration(), dom, Boolean.TRUE ) ); } } @@ -813,14 +884,14 @@ public void populateDefaultConfigurationForPlugins( Collection plugins, } } - private Xpp3Dom getDefaultPluginConfiguration( Plugin plugin, String goal, ArtifactRepository localRepository, List remoteRepositories ) + private Xpp3Dom getDefaultPluginConfiguration( String groupId, String artifactId, String version, String goal, ArtifactRepository localRepository, List remoteRepositories ) throws LifecycleExecutionException { MojoDescriptor mojoDescriptor; try { - mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, localRepository, remoteRepositories ); + mojoDescriptor = pluginManager.getMojoDescriptor( groupId, artifactId, version, goal, localRepository, remoteRepositories ); } catch ( PluginNotFoundException e ) { @@ -892,7 +963,7 @@ Xpp3Dom convert( MojoDescriptor mojoDescriptor ) // or the user forces the issue public Plugin findPluginForPrefix( String prefix, MavenSession session ) throws NoPluginFoundForPrefixException - { + { // [prefix]:[goal] Plugin plugin = pluginPrefixes.get( prefix ); @@ -901,71 +972,110 @@ public Plugin findPluginForPrefix( String prefix, MavenSession session ) { return plugin; } - - for ( ArtifactRepository repository : session.getCurrentProject().getRemoteArtifactRepositories() ) + + // Process all plugin groups in the local repository first to see if we get a hit. A developer may have been + // developing a plugin locally and installing. + // + for ( String pluginGroup : session.getPluginGroups() ) { - for ( String pluginGroup : session.getPluginGroups() ) - { - // org.apache.maven.plugins - // org/apache/maven/plugins/maven-metadata.xml - - String localPath = pluginGroup.replace( '.', '/' ) + "/" + "maven-metadata-" + repository.getId() + ".xml"; - - File destination = new File( session.getLocalRepository().getBasedir(), localPath ); - - if ( !destination.exists() ) - { - try - { - String remotePath = pluginGroup.replace( '.', '/' ) + "/" + "maven-metadata.xml"; - - repositorySystem.retrieve( repository, destination, remotePath, session.getRequest().getTransferListener() ); - } - catch ( TransferFailedException e ) - { - continue; - } - catch ( ResourceDoesNotExistException e ) - { - continue; - } - } + String localPath = pluginGroup.replace( '.', '/' ) + "/" + "maven-metadata-" + session.getLocalRepository().getId() + ".xml"; - // We have retrieved the metadata - try - { - Metadata pluginGroupMetadata = readMetadata( destination ); - - List plugins = pluginGroupMetadata.getPlugins(); - - if ( plugins != null ) - { - for ( org.apache.maven.artifact.repository.metadata.Plugin metadataPlugin : plugins ) - { - Plugin p = new Plugin(); - p.setGroupId( pluginGroup ); - p.setArtifactId( metadataPlugin.getArtifactId() ); - pluginPrefixes.put( metadataPlugin.getPrefix(), p ); - } - } - } - catch ( RepositoryMetadataReadException e ) + File destination = new File( session.getLocalRepository().getBasedir(), localPath ); + + if ( destination.exists() ) + { + processPluginGroupMetadata( pluginGroup, destination, pluginPrefixes ); + + plugin = pluginPrefixes.get( prefix ); + + if ( plugin != null ) { - logger.warn( "Error reading plugin group metadata: ", e ); - } - } + return plugin; + } + } } + + // Process all the remote repositories. + // + for ( String pluginGroup : session.getPluginGroups() ) + { + for ( ArtifactRepository repository : session.getCurrentProject().getRemoteArtifactRepositories() ) + { + try + { + String localPath = pluginGroup.replace( '.', '/' ) + "/" + "maven-metadata-" + repository.getId() + ".xml"; - plugin = pluginPrefixes.get( prefix ); - - if ( plugin != null ) - { - return plugin; - } - - throw new NoPluginFoundForPrefixException( prefix ); + File destination = new File( session.getLocalRepository().getBasedir(), localPath ); + + String remotePath = pluginGroup.replace( '.', '/' ) + "/" + "maven-metadata.xml"; + + repositorySystem.retrieve( repository, destination, remotePath, session.getRequest().getTransferListener() ); + + processPluginGroupMetadata( pluginGroup, destination, pluginPrefixes ); + + plugin = pluginPrefixes.get( prefix ); + + if ( plugin != null ) + { + return plugin; + } + } + catch ( TransferFailedException e ) + { + continue; + } + catch ( ResourceDoesNotExistException e ) + { + continue; + } + } + + } + + throw new NoPluginFoundForPrefixException( prefix, session.getLocalRepository(), session.getCurrentProject().getRemoteArtifactRepositories() ); } + // Keep track of the repository that provided the prefix mapping + // + private class PluginPrefix + { + private Plugin plugin; + + private ArtifactRepository repository; + + public PluginPrefix( Plugin plugin, ArtifactRepository repository ) + { + this.plugin = plugin; + this.repository = repository; + } + } + + + private void processPluginGroupMetadata( String pluginGroup, File pluginGroupMetadataFile, Map pluginPrefixes ) + { + try + { + Metadata pluginGroupMetadata = readMetadata( pluginGroupMetadataFile ); + + List plugins = pluginGroupMetadata.getPlugins(); + + if ( plugins != null ) + { + for ( org.apache.maven.artifact.repository.metadata.Plugin metadataPlugin : plugins ) + { + Plugin p = new Plugin(); + p.setGroupId( pluginGroup ); + p.setArtifactId( metadataPlugin.getArtifactId() ); + pluginPrefixes.put( metadataPlugin.getPrefix(), p ); + } + } + } + catch ( RepositoryMetadataReadException e ) + { + logger.warn( "Error reading plugin group metadata: ", e ); + } + } + protected Metadata readMetadata( File mappingFile ) throws RepositoryMetadataReadException { diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutor.java b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutor.java index 7c00f4845b..4fcea84604 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutor.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutor.java @@ -39,8 +39,6 @@ */ public interface LifecycleExecutor { - List getLifecyclePhases(); - /** * Calculate the list of {@link org.apache.maven.plugin.descriptor.MojoDescriptor} objects to run for the selected lifecycle phase. * diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/NoPluginFoundForPrefixException.java b/maven-core/src/main/java/org/apache/maven/lifecycle/NoPluginFoundForPrefixException.java index 1ea31691e3..a6d02932cb 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/NoPluginFoundForPrefixException.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/NoPluginFoundForPrefixException.java @@ -1,12 +1,23 @@ package org.apache.maven.lifecycle; +import java.util.List; + +import org.apache.maven.artifact.repository.ArtifactRepository; + public class NoPluginFoundForPrefixException extends Exception { private String prefix; - public NoPluginFoundForPrefixException( String prefix ) + private ArtifactRepository localRepository; + + private List remoteRepositories; + + public NoPluginFoundForPrefixException( String prefix, ArtifactRepository localRepository, List remoteRepositories ) { - super( "No plugin found for prefix '" + prefix + "'" ); + super( "No plugin found for prefix '" + prefix + "'" ); + this.prefix = prefix; + this.localRepository = localRepository; + this.remoteRepositories = remoteRepositories; } } 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 index dd75e01816..1dec562276 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/PluginNotFoundException.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginNotFoundException.java @@ -32,7 +32,7 @@ public class PluginNotFoundException extends AbstractArtifactResolutionException { - private final Plugin plugin; + private Plugin plugin; public PluginNotFoundException( Plugin plugin, ArtifactNotFoundException e ) { diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java index 28a44d20a9..4e61a82850 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java @@ -41,15 +41,6 @@ protected String getProjectsDirectory() return "src/test/projects/lifecycle-executor"; } - // ----------------------------------------------------------------------------------------------- - // - // ----------------------------------------------------------------------------------------------- - - public void testLifecyclePhases() - { - assertNotNull( lifecycleExecutor.getLifecyclePhases() ); - } - // ----------------------------------------------------------------------------------------------- // Tests which exercise the lifecycle executor when it is dealing with default lifecycle phases. // ----------------------------------------------------------------------------------------------- diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/CLIRequestUtils.java b/maven-embedder/src/main/java/org/apache/maven/cli/CLIRequestUtils.java index 1531e03bb9..c9edfdbfab 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/CLIRequestUtils.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/CLIRequestUtils.java @@ -252,16 +252,22 @@ else if ( quiet ) .setGlobalChecksumPolicy( globalChecksumPolicy ) // default: warn .setUserToolchainsFile( userToolchainsFile ); - + File pom; + if ( alternatePomFile != null ) { - request.setPom( new File( alternatePomFile ) ); + pom = new File( alternatePomFile ); } else { - request.setPom( new File( baseDirectory, Maven.POMv4 ) ); + pom = new File( baseDirectory, Maven.POMv4 ); } - + + if ( pom.exists() ) + { + request.setPom( pom ); + } + return request; } diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java index 26edd75575..b74adec8e5 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.io.Reader; import java.io.Writer; -import java.util.List; import org.apache.maven.Maven; import org.apache.maven.MavenExecutionException; @@ -549,9 +548,4 @@ public PlexusContainer getPlexusContainer() { return container; } - - public List getLifecyclePhases() - { - return maven.getLifecyclePhases(); - } } diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java index 0218985595..348d8431ad 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java @@ -94,14 +94,6 @@ public MavenExecutionRequest populateDefaults( MavenExecutionRequest request, Co private void pom( MavenExecutionRequest request ) { - // ------------------------------------------------------------------------ - // POM - // - // If we are not given a specific POM file, but passed a base directory - // then we will use a release POM in the directory provided, and then - // look for the standard POM. - // ------------------------------------------------------------------------ - if ( request.getPom() != null && !request.getPom().isAbsolute() ) { request.setPom( request.getPom().getAbsoluteFile() ); diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java index 6a18fa62f2..945fc324fa 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java @@ -326,22 +326,6 @@ public void testTwoExecutionsDoNotCacheChangedData() assertEquals( "2.4.3", p.getVersion() ); } - // ---------------------------------------------------------------------- - // Lifecycle phases - // ---------------------------------------------------------------------- - - public void testRetrievingLifecyclePhases() - throws Exception - { - List phases = mavenEmbedder.getLifecyclePhases(); - - assertEquals( "validate", (String) phases.get( 0 ) ); - - assertEquals( "initialize", (String) phases.get( 1 ) ); - - assertEquals( "generate-sources", (String) phases.get( 2 ) ); - } - // ---------------------------------------------------------------------- // Repository // ---------------------------------------------------------------------- diff --git a/maven-repository/src/main/java/org/apache/maven/repository/DelegatingLocalArtifactRepository.java b/maven-repository/src/main/java/org/apache/maven/repository/DelegatingLocalArtifactRepository.java index 97bf22d3a9..782514b003 100644 --- a/maven-repository/src/main/java/org/apache/maven/repository/DelegatingLocalArtifactRepository.java +++ b/maven-repository/src/main/java/org/apache/maven/repository/DelegatingLocalArtifactRepository.java @@ -64,10 +64,9 @@ public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, Artifact return userLocalArtifactRepository.pathOfLocalRepositoryMetadata( metadata, userLocalArtifactRepository ); } - // This ID is necessary of the metadata lookup doesn't work correctly. public String getId() { - return "delegating"; + return "local"; } @Override