o Cleaned up code

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@807327 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-08-24 18:17:13 +00:00
parent 9298fbb213
commit 6a7fdf038e
1 changed files with 34 additions and 122 deletions

View File

@ -17,6 +17,7 @@
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -163,6 +164,20 @@ public void execute( MavenSession session )
return; return;
} }
if ( logger.isDebugEnabled() )
{
logger.debug( "=== REACTOR BUILD PLAN ===" );
for ( ProjectBuild projectBuild : projectBuilds )
{
logger.debug( "------------------" );
logger.debug( "Project: " + projectBuild.project.getId() );
logger.debug( "Tasks: " + projectBuild.taskSegment.tasks );
}
logger.debug( "==========================" );
}
ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
RepositoryRequest repositoryRequest = getRepositoryRequest( session, null ); RepositoryRequest repositoryRequest = getRepositoryRequest( session, null );
@ -196,20 +211,11 @@ public void execute( MavenSession session )
} }
MavenExecutionPlan executionPlan = MavenExecutionPlan executionPlan =
calculateProjectExecutionPlan( session, currentProject, projectBuild.taskSegment ); calculateExecutionPlan( session, currentProject, projectBuild.taskSegment );
// TODO: once we have calculated the build plan then we should accurately be able to download
// the project dependencies. Having it happen in the plugin manager is a tangled mess. We can optimize
// this later by looking at the build plan. Would be better to just batch download everything required
// by the reactor.
repositoryRequest.setRemoteRepositories( currentProject.getRemoteArtifactRepositories() );
projectDependenciesResolver.resolve( currentProject, executionPlan.getRequiredResolutionScopes(),
repositoryRequest );
if ( logger.isDebugEnabled() ) if ( logger.isDebugEnabled() )
{ {
logger.debug( "=== BUILD PLAN ===" ); logger.debug( "=== PROJECT BUILD PLAN ===" );
logger.debug( "Project: " + currentProject ); logger.debug( "Project: " + currentProject );
for ( MojoExecution mojoExecution : executionPlan.getExecutions() ) for ( MojoExecution mojoExecution : executionPlan.getExecutions() )
@ -221,9 +227,18 @@ public void execute( MavenSession session )
logger.debug( "Configuration: " + String.valueOf( mojoExecution.getConfiguration() ) ); logger.debug( "Configuration: " + String.valueOf( mojoExecution.getConfiguration() ) );
} }
logger.debug( "==================" ); logger.debug( "==========================" );
} }
// TODO: once we have calculated the build plan then we should accurately be able to download
// the project dependencies. Having it happen in the plugin manager is a tangled mess. We can optimize
// this later by looking at the build plan. Would be better to just batch download everything required
// by the reactor.
repositoryRequest.setRemoteRepositories( currentProject.getRemoteArtifactRepositories() );
projectDependenciesResolver.resolve( currentProject, executionPlan.getRequiredResolutionScopes(),
repositoryRequest );
for ( MojoExecution mojoExecution : executionPlan.getExecutions() ) for ( MojoExecution mojoExecution : executionPlan.getExecutions() )
{ {
execute( currentProject, session, mojoExecution ); execute( currentProject, session, mojoExecution );
@ -428,8 +443,8 @@ private List<ProjectBuild> calculateProjectBuilds( MavenSession session )
return projectBuilds; return projectBuilds;
} }
private MavenExecutionPlan calculateProjectExecutionPlan( MavenSession session, MavenProject project, private MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project,
TaskSegment taskSegment ) TaskSegment taskSegment )
throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException, throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException, PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
@ -625,62 +640,22 @@ public String toString()
} }
// TODO: refactor this to reuse the same code as for the reactor build
public MavenExecutionPlan calculateExecutionPlan( MavenSession session, String... tasks ) public MavenExecutionPlan calculateExecutionPlan( MavenSession session, String... tasks )
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException, throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException, MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
PluginManagerException, LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginManagerException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
PluginVersionResolutionException PluginVersionResolutionException
{ {
MavenProject project = session.getCurrentProject(); List<TaskSegment> taskSegments = calculateTaskSegments( session, Arrays.asList( tasks ) );
List<MojoExecution> lifecyclePlan = new ArrayList<MojoExecution>(); TaskSegment mergedSegment = new TaskSegment( false );
Set<String> requiredDependencyResolutionScopes = new HashSet<String>(); for ( TaskSegment taskSegment : taskSegments )
for ( String task : tasks )
{ {
if ( task.indexOf( ":" ) > 0 ) mergedSegment.tasks.addAll( taskSegment.tasks );
{
calculateExecutionForIndividualGoal( session, lifecyclePlan, task );
}
else
{
calculateExecutionForLifecyclePhase( session, project, lifecyclePlan, task );
}
} }
// 7. Now we create the correct configuration for the mojo to execute. return calculateExecutionPlan( session, session.getCurrentProject(), mergedSegment );
//
for ( MojoExecution mojoExecution : lifecyclePlan )
{
// These are bits that look like this:
//
// org.apache.maven.plugins:maven-remote-resources-plugin:1.0:process
//
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
if ( mojoDescriptor == null )
{
mojoDescriptor =
pluginManager.getMojoDescriptor( mojoExecution.getPlugin(), mojoExecution.getGoal(),
getRepositoryRequest( session, project ) );
mojoExecution.setMojoDescriptor( mojoDescriptor );
}
populateMojoExecutionConfiguration( project, mojoExecution,
MojoExecution.Source.CLI.equals( mojoExecution.getSource() ) );
extractMojoConfiguration( mojoExecution );
calculateForkedExecutions( mojoExecution, session, project, new HashSet<MojoDescriptor>() );
collectDependencyResolutionScopes( requiredDependencyResolutionScopes, mojoExecution );
}
return new MavenExecutionPlan( lifecyclePlan, requiredDependencyResolutionScopes );
} }
private RepositoryRequest getRepositoryRequest( MavenSession session, MavenProject project ) private RepositoryRequest getRepositoryRequest( MavenSession session, MavenProject project )
@ -714,69 +689,6 @@ private void collectDependencyResolutionScopes( Collection<String> requiredDepen
} }
} }
private void calculateExecutionForIndividualGoal( MavenSession session, List<MojoExecution> lifecyclePlan,
String goal )
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
PluginVersionResolutionException
{
// If this is a goal like "mvn modello:java" and the POM looks like the following:
//
// <project>
// <modelVersion>4.0.0</modelVersion>
// <groupId>org.apache.maven.plugins</groupId>
// <artifactId>project-plugin-level-configuration-only</artifactId>
// <version>1.0.1</version>
// <build>
// <plugins>
// <plugin>
// <groupId>org.codehaus.modello</groupId>
// <artifactId>modello-maven-plugin</artifactId>
// <version>1.0.1</version>
// <configuration>
// <version>1.1.0</version>
// <models>
// <model>src/main/mdo/remote-resources.mdo</model>
// </models>
// </configuration>
// </plugin>
// </plugins>
// </build>
// </project>
//
// 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-<goal>.
MojoDescriptor mojoDescriptor = getMojoDescriptor( goal, session, session.getCurrentProject() );
MojoExecution mojoExecution = new MojoExecution( mojoDescriptor, "default-cli", MojoExecution.Source.CLI );
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, MavenProject project,
List<MojoExecution> lifecyclePlan, String lifecyclePhase )
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
LifecyclePhaseNotFoundException
{
Map<String, List<MojoExecution>> phaseToMojoMapping =
calculateLifecycleMappings( session, project, lifecyclePhase );
for ( List<MojoExecution> mojoExecutions : phaseToMojoMapping.values() )
{
lifecyclePlan.addAll( mojoExecutions );
}
}
private Map<String, List<MojoExecution>> calculateLifecycleMappings( MavenSession session, MavenProject project, private Map<String, List<MojoExecution>> calculateLifecycleMappings( MavenSession session, MavenProject project,
String lifecyclePhase ) String lifecyclePhase )
throws LifecyclePhaseNotFoundException, PluginNotFoundException, PluginResolutionException, throws LifecyclePhaseNotFoundException, PluginNotFoundException, PluginResolutionException,