From 08406e1c1374c08443c07ba9b59c359090e014c9 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Mon, 21 Sep 2009 12:57:43 +0000 Subject: [PATCH] [MNG-4363] [regression] Dependency artifacts injected by plugins aren't included in class paths git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@817233 13f79535-47bb-0310-9956-ffa450edef68 --- .../DefaultProjectDependenciesResolver.java | 1 + .../lifecycle/DefaultLifecycleExecutor.java | 131 ++++++++++++++---- .../maven/lifecycle/LifecycleExecutor.java | 5 +- 3 files changed, 109 insertions(+), 28 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/DefaultProjectDependenciesResolver.java b/maven-core/src/main/java/org/apache/maven/DefaultProjectDependenciesResolver.java index e34e9ed985..50b6d5e1eb 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultProjectDependenciesResolver.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultProjectDependenciesResolver.java @@ -142,6 +142,7 @@ public class DefaultProjectDependenciesResolver for ( MavenProject project : projects ) { request.setArtifact( new ProjectArtifact( project ) ); + request.setArtifactDependencies( project.getDependencyArtifacts() ); request.setManagedVersionMap( project.getManagedVersionMap() ); request.setRemoteRepositories( project.getRemoteArtifactRepositories() ); 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 2ad429edad..334491e7ea 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 @@ -309,12 +309,17 @@ public class DefaultLifecycleExecutor for ( MavenProject project : projectsToResolve ) { - resolveProjectDependencies( project, executionPlan, session, projectBuild.taskSegment.aggregating ); + resolveProjectDependencies( project, executionPlan.getRequiredCollectionScopes(), + executionPlan.getRequiredResolutionScopes(), session, + projectBuild.taskSegment.aggregating ); } + DependencyContext dependencyContext = + new DependencyContext( executionPlan, projectBuild.taskSegment.aggregating ); + for ( MojoExecution mojoExecution : executionPlan.getExecutions() ) { - execute( session, mojoExecution, projectIndex ); + execute( session, mojoExecution, projectIndex, dependencyContext ); } long buildEndTime = System.currentTimeMillis(); @@ -364,18 +369,15 @@ public class DefaultLifecycleExecutor fireEvent( session, null, LifecycleEventCatapult.SESSION_ENDED ); } - private void resolveProjectDependencies( MavenProject project, MavenExecutionPlan executionPlan, - MavenSession session, boolean aggregating ) + private void resolveProjectDependencies( MavenProject project, Collection scopesToCollect, + Collection scopesToResolve, MavenSession session, + boolean aggregating ) throws ArtifactResolutionException, ArtifactNotFoundException { Set artifacts; try { - Collection scopesToResolve = executionPlan.getRequiredResolutionScopes(); - - Collection scopesToCollect = executionPlan.getRequiredCollectionScopes(); - artifacts = projectDependenciesResolver.resolve( project, scopesToCollect, scopesToResolve, session ); } catch ( MultipleArtifactsNotFoundException e ) @@ -407,21 +409,24 @@ public class DefaultLifecycleExecutor project.setArtifacts( artifacts ); - Set directDependencies = new HashSet( project.getDependencies().size() * 2 ); - for ( Dependency dependency : project.getDependencies() ) + if ( project.getDependencyArtifacts() == null ) { - directDependencies.add( dependency.getManagementKey() ); - } - - Set dependencyArtifacts = new LinkedHashSet( project.getDependencies().size() * 2 ); - for ( Artifact artifact : artifacts ) - { - if ( directDependencies.contains( artifact.getDependencyConflictId() ) ) + Set directDependencies = new HashSet( project.getDependencies().size() * 2 ); + for ( Dependency dependency : project.getDependencies() ) { - dependencyArtifacts.add( artifact ); + directDependencies.add( dependency.getManagementKey() ); } + + Set dependencyArtifacts = new LinkedHashSet( project.getDependencies().size() * 2 ); + for ( Artifact artifact : artifacts ) + { + if ( directDependencies.contains( artifact.getDependencyConflictId() ) ) + { + dependencyArtifacts.add( artifact ); + } + } + project.setDependencyArtifacts( dependencyArtifacts ); } - project.setDependencyArtifacts( dependencyArtifacts ); } private boolean areAllArtifactsInReactor( Collection projects, Collection artifacts ) @@ -445,8 +450,72 @@ public class DefaultLifecycleExecutor return true; } - private void execute( MavenSession session, MojoExecution mojoExecution, ProjectIndex projectIndex ) - throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException + private class DependencyContext + { + + private final Collection scopesToCollect; + + private final Collection scopesToResolve; + + private final boolean aggregating; + + private MavenProject lastProject; + + private Collection lastDependencyArtifacts; + + private int lastDependencyArtifactCount; + + DependencyContext( Collection scopesToCollect, Collection scopesToResolve, boolean aggregating ) + { + this.scopesToCollect = scopesToCollect; + this.scopesToResolve = scopesToResolve; + this.aggregating = aggregating; + } + + DependencyContext( MavenExecutionPlan executionPlan, boolean aggregating ) + { + this.scopesToCollect = executionPlan.getRequiredCollectionScopes(); + this.scopesToResolve = executionPlan.getRequiredResolutionScopes(); + this.aggregating = aggregating; + } + + DependencyContext( MojoExecution mojoExecution ) + { + this.scopesToCollect = new TreeSet(); + this.scopesToResolve = new TreeSet(); + collectDependencyRequirements( scopesToResolve, scopesToCollect, mojoExecution ); + this.aggregating = mojoExecution.getMojoDescriptor().isAggregating(); + } + + public DependencyContext clone() + { + return new DependencyContext( scopesToCollect, scopesToResolve, aggregating ); + } + + void checkForUpdate( MavenSession session ) + throws ArtifactResolutionException, ArtifactNotFoundException + { + if ( lastProject == session.getCurrentProject() ) + { + if ( lastDependencyArtifacts != lastProject.getDependencyArtifacts() + || ( lastDependencyArtifacts != null && lastDependencyArtifactCount != lastDependencyArtifacts.size() ) ) + { + logger.debug( "Re-resolving dependencies for project " + lastProject.getId() + + " to account for updates by previous goal execution" ); + resolveProjectDependencies( lastProject, scopesToCollect, scopesToResolve, session, aggregating ); + } + } + + lastProject = session.getCurrentProject(); + lastDependencyArtifacts = lastProject.getDependencyArtifacts(); + lastDependencyArtifactCount = ( lastDependencyArtifacts != null ) ? lastDependencyArtifacts.size() : 0; + } + } + + private void execute( MavenSession session, MojoExecution mojoExecution, ProjectIndex projectIndex, + DependencyContext dependencyContext ) + throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException, + ArtifactResolutionException, ArtifactNotFoundException { MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor(); @@ -471,7 +540,10 @@ public class DefaultLifecycleExecutor } } - List forkedProjects = executeForkedExecutions( mojoExecution, session, projectIndex ); + dependencyContext.checkForUpdate( session ); + + List forkedProjects = + executeForkedExecutions( mojoExecution, session, projectIndex, dependencyContext ); fireEvent( session, mojoExecution, LifecycleEventCatapult.MOJO_STARTED ); @@ -515,14 +587,17 @@ public class DefaultLifecycleExecutor } public List executeForkedExecutions( MojoExecution mojoExecution, MavenSession session ) - throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException + throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException, + ArtifactResolutionException, ArtifactNotFoundException { - return executeForkedExecutions( mojoExecution, session, new ProjectIndex( session.getProjects() ) ); + return executeForkedExecutions( mojoExecution, session, new ProjectIndex( session.getProjects() ), + new DependencyContext( mojoExecution ) ); } private List executeForkedExecutions( MojoExecution mojoExecution, MavenSession session, - ProjectIndex projectIndex ) - throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException + ProjectIndex projectIndex, DependencyContext dependencyContext ) + throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException, + ArtifactResolutionException, ArtifactNotFoundException { List forkedProjects = Collections.emptyList(); @@ -536,6 +611,8 @@ public class DefaultLifecycleExecutor forkedProjects = new ArrayList( forkedExecutions.size() ); + dependencyContext = dependencyContext.clone(); + try { for ( Map.Entry> fork : forkedExecutions.entrySet() ) @@ -558,7 +635,7 @@ public class DefaultLifecycleExecutor for ( MojoExecution forkedExecution : fork.getValue() ) { - execute( session, forkedExecution, projectIndex ); + execute( session, forkedExecution, projectIndex, dependencyContext ); } } finally 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 40754bafa4..0710cbcd41 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 @@ -22,6 +22,8 @@ package org.apache.maven.lifecycle; import java.util.List; import java.util.Set; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Plugin; import org.apache.maven.plugin.InvalidPluginDescriptorException; @@ -107,6 +109,7 @@ public interface LifecycleExecutor * will never be {@code null}. */ List executeForkedExecutions( MojoExecution mojoExecution, MavenSession session ) - throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException; + throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException, + ArtifactResolutionException, ArtifactNotFoundException; }