o working creating a clean path of plugin resolution logic, and separting report processing from plugin processing

git-svn-id: https://svn.apache.org/repos/asf/maven/components/branches/MNG-2766@757003 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2009-03-21 20:18:36 +00:00
parent 0bf25e64d8
commit 0f70fd4a0f
6 changed files with 137 additions and 104 deletions

View File

@ -428,13 +428,7 @@ public ArtifactResolutionResult resolve( ArtifactResolutionRequest request )
// After the collection we will have the artifact object in the result but they will not be resolved yet.
result = artifactCollector.collect( artifacts, rootArtifact, managedVersions, localRepository, remoteRepositories, source, filter, listeners );
if ( !isDummy( request ) )
{
// Add the root artifact
result.addArtifact( rootArtifact );
}
// We have metadata retrieval problems, or there are cycles that have been detected
// so we give this back to the calling code and let them deal with this information
// appropriately.
@ -469,6 +463,14 @@ public ArtifactResolutionResult resolve( ArtifactResolutionRequest request )
}
}
// We want to send the root artifact back in the result but we need to do this after the other dependencies
// have been resolved.
if ( !isDummy( request ) )
{
// Add the root artifact
result.addArtifact( rootArtifact );
}
return result;
}

View File

@ -59,6 +59,7 @@ public class DefaultArtifactFilterManager
artifacts.add( "maven-plugin-api" );
artifacts.add( "maven-plugin-descriptor" );
artifacts.add( "maven-plugin-parameter-documenter" );
artifacts.add( "maven-plugin-registry" );
artifacts.add( "maven-profile" );
artifacts.add( "maven-project" );
artifacts.add( "maven-reporting-api" );
@ -70,6 +71,7 @@ public class DefaultArtifactFilterManager
artifacts.add( "plexus-component-api" );
artifacts.add( "plexus-container-default" );
artifacts.add( "plexus-interactivity-api" );
artifacts.add( "plexus-interpolation" );
artifacts.add( "wagon-provider-api" );
artifacts.add( "wagon-file" );
artifacts.add( "wagon-http-lightweight" );

View File

@ -54,11 +54,9 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.MavenReport;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@ -1302,7 +1300,6 @@ else if ( numTokens == 3 || numTokens == 4 )
project.addPlugin( plugin );
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( goal );
return mojoDescriptor;
}

View File

@ -46,6 +46,7 @@
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
@ -169,7 +170,9 @@ public PluginDescriptor verifyPlugin( Plugin plugin, MavenProject project, Maven
if ( ( pluginVersion == null ) || Artifact.LATEST_VERSION.equals( pluginVersion ) || Artifact.RELEASE_VERSION.equals( pluginVersion ) )
{
logger.debug( "Resolving version for plugin: " + plugin.getKey() );
pluginVersion = resolvePluginVersion( plugin.getGroupId(), plugin.getArtifactId(), project, session );
plugin.setVersion( pluginVersion );
logger.debug( "Resolved to version: " + pluginVersion );
@ -188,10 +191,8 @@ private PluginDescriptor verifyVersionedPlugin( Plugin plugin, MavenProject proj
// and no ChildContainer exists. The check for that below fixes
// the 'Can't find plexus container for plugin: xxx' error.
try
{
Artifact pluginArtifact = resolvePluginArtifact( plugin, project, session );
addPlugin( plugin, pluginArtifact, project, session );
{
addPlugin( plugin, project, session );
project.addPlugin( plugin );
}
@ -230,9 +231,29 @@ private String pluginKey( Plugin plugin )
return plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion();
}
protected void addPlugin( Plugin plugin, Artifact pluginArtifact, MavenProject project, MavenSession session )
throws ArtifactNotFoundException, ArtifactResolutionException, PluginManagerException, InvalidPluginException
{
protected void addPlugin( Plugin plugin, MavenProject project, MavenSession session )
throws ArtifactNotFoundException, ArtifactResolutionException, PluginManagerException, InvalidPluginException, PluginVersionResolutionException
{
logger.debug( "Resolving plugin artifact " + plugin.getKey() + " from " + project.getRemoteArtifactRepositories() );
ArtifactRepository localRepository = session.getLocalRepository();
MavenProject pluginProject = buildPluginProject( plugin, localRepository, project.getRemoteArtifactRepositories() );
Artifact pluginArtifact = repositorySystem.createPluginArtifact( plugin );
checkRequiredMavenVersion( plugin, pluginProject, localRepository, project.getRemoteArtifactRepositories() );
checkPluginDependencySpec( plugin, pluginProject );
pluginArtifact = project.replaceWithActiveArtifact( pluginArtifact );
ArtifactResolutionRequest request = new ArtifactResolutionRequest( pluginArtifact, localRepository, project.getRemoteArtifactRepositories() );
ArtifactResolutionResult result = repositorySystem.resolve( request );
resolutionErrorHandler.throwErrors( request, result );
// ----------------------------------------------------------------------------
// Get the dependencies for the Plugin
// ----------------------------------------------------------------------------
@ -240,6 +261,8 @@ protected void addPlugin( Plugin plugin, Artifact pluginArtifact, MavenProject p
// the only Plugin instance which will have dependencies is the one specified in the project.
// We need to look for a Plugin instance there, in case the instance we're using didn't come from
// the project.
// Trying to cache the version of the plugin for a project?
Plugin projectPlugin = project.getPlugin( plugin.getKey() );
if ( projectPlugin == null )
@ -272,7 +295,7 @@ else if ( projectPlugin.getVersion() == null || Artifact.RELEASE_VERSION.equals(
// Not going to happen
}
}
try
{
logger.debug( "Discovering components in realm: " + pluginRealm );
@ -319,11 +342,18 @@ else if ( projectPlugin.getVersion() == null || Artifact.RELEASE_VERSION.equals(
}
}
// plugin artifact
// its dependencies while filtering out what's in the core
// layering on the project level plugin dependencies
private Set<Artifact> getPluginArtifacts( Artifact pluginArtifact, Plugin plugin, MavenProject project, ArtifactRepository localRepository )
throws InvalidPluginException, ArtifactNotFoundException, ArtifactResolutionException
{
ArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME_PLUS_SYSTEM );
AndArtifactFilter filter = new AndArtifactFilter();
filter.add( coreArtifactFilterManager.getCoreArtifactFilter() );
filter.add( new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME_PLUS_SYSTEM ) );
Set<Artifact> projectPluginDependencies;
// The case where we have a plugin that can host multiple versions of a particular tool. Say the
@ -333,7 +363,7 @@ private Set<Artifact> getPluginArtifacts( Artifact pluginArtifact, Plugin plugin
try
{
projectPluginDependencies = repositorySystem.createArtifacts( plugin.getDependencies(), null, coreArtifactFilterManager.getCoreArtifactFilter(), project );
projectPluginDependencies = repositorySystem.createArtifacts( plugin.getDependencies(), null, filter, project );
}
catch ( VersionNotFoundException e )
{
@ -1305,6 +1335,7 @@ protected void resolveTransitiveDependencies( MavenSession context, RepositorySy
result.setUnresolvedArtifacts( null );
}
}
resolutionErrorHandler.throwErrors( request, result );
project.setArtifacts( result.getArtifacts() );
@ -1365,9 +1396,9 @@ private void downloadDependencies( MavenProject project, MavenSession context, R
List<ArtifactRepository> remoteArtifactRepositories = project.getRemoteArtifactRepositories();
for ( Iterator<Artifact> it = project.getArtifacts().iterator(); it.hasNext(); )
{
{
Artifact artifact = (Artifact) it.next();
repositorySystem.resolve( new ArtifactResolutionRequest( artifact, localRepository, remoteArtifactRepositories ) );
}
}
@ -1446,27 +1477,27 @@ private static String interpolateXmlString( String xml, List<InterpolatorPropert
public String resolvePluginVersion( String groupId, String artifactId, MavenProject project, MavenSession session )
throws PluginVersionResolutionException, InvalidPluginException, PluginVersionNotFoundException
{
return resolvePluginVersion( groupId, artifactId, project, session.getLocalRepository(), false );
}
public String resolveReportPluginVersion( String groupId, String artifactId, MavenProject project, MavenSession session )
throws PluginVersionResolutionException, InvalidPluginException, PluginVersionNotFoundException
{
return resolvePluginVersion( groupId, artifactId, project, session.getLocalRepository(), true );
}
private String resolvePluginVersion( String groupId, String artifactId, MavenProject project, ArtifactRepository localRepository, boolean resolveAsReportPlugin )
throws PluginVersionResolutionException, InvalidPluginException, PluginVersionNotFoundException
{
// first pass...if the plugin is specified in the pom, try to retrieve the version from there.
String version = getVersionFromPluginConfig( groupId, artifactId, project, resolveAsReportPlugin );
String version = null;
if ( project.getBuildPlugins() != null )
{
for ( Iterator it = project.getBuildPlugins().iterator(); it.hasNext() && ( version == null ); )
{
Plugin plugin = (Plugin) it.next();
if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) )
{
version = plugin.getVersion();
}
}
}
// final pass...retrieve the version for RELEASE and also set that resolved version as the <useVersion/>
// in settings.xml.
if ( StringUtils.isEmpty( version ) || Artifact.RELEASE_VERSION.equals( version ) )
{
// 1. resolve the version to be used
version = resolveMetaVersion( groupId, artifactId, project, localRepository, Artifact.RELEASE_VERSION );
version = resolveMetaVersion( groupId, artifactId, project, session.getLocalRepository(), Artifact.RELEASE_VERSION );
logger.debug( "Version from RELEASE metadata: " + version );
}
@ -1476,42 +1507,40 @@ private String resolvePluginVersion( String groupId, String artifactId, MavenPro
throw new PluginVersionNotFoundException( groupId, artifactId );
}
return version;
return version;
}
private String getVersionFromPluginConfig( String groupId, String artifactId, MavenProject project, boolean resolveAsReportPlugin )
public String resolveReportPluginVersion( String groupId, String artifactId, MavenProject project, MavenSession session )
throws PluginVersionResolutionException, InvalidPluginException, PluginVersionNotFoundException
{
String version = null;
if ( resolveAsReportPlugin )
if ( project.getReportPlugins() != null )
{
if ( project.getReportPlugins() != null )
for ( Iterator it = project.getReportPlugins().iterator(); it.hasNext() && ( version == null ); )
{
for ( Iterator it = project.getReportPlugins().iterator(); it.hasNext() && ( version == null ); )
{
ReportPlugin plugin = (ReportPlugin) it.next();
ReportPlugin plugin = (ReportPlugin) it.next();
if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) )
{
version = plugin.getVersion();
}
if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) )
{
version = plugin.getVersion();
}
}
}
else
// final pass...retrieve the version for RELEASE and also set that resolved version as the <useVersion/>
// in settings.xml.
if ( StringUtils.isEmpty( version ) || Artifact.RELEASE_VERSION.equals( version ) )
{
if ( project.getBuildPlugins() != null )
{
for ( Iterator it = project.getBuildPlugins().iterator(); it.hasNext() && ( version == null ); )
{
Plugin plugin = (Plugin) it.next();
// 1. resolve the version to be used
version = resolveMetaVersion( groupId, artifactId, project, session.getLocalRepository(), Artifact.RELEASE_VERSION );
logger.debug( "Version from RELEASE metadata: " + version );
}
if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) )
{
version = plugin.getVersion();
}
}
}
// if we still haven't found a version, then fail early before we get into the update goop.
if ( StringUtils.isEmpty( version ) )
{
throw new PluginVersionNotFoundException( groupId, artifactId );
}
return version;

View File

@ -1,21 +1,13 @@
<!--
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.
-->
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
@ -44,4 +36,13 @@ under the License.
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
</plugin>
</plugins>
</build>
</project>

View File

@ -55,7 +55,7 @@ public class MavenEmbedderTest
{
protected String basedir;
protected MavenEmbedder maven;
protected MavenEmbedder mavenEmbedder;
protected void setUp()
@ -77,13 +77,13 @@ protected void setUp()
.setMavenEmbedderLogger( new MavenEmbedderConsoleLogger() );
configuration.setUserSettingsFile( MavenEmbedder.DEFAULT_USER_SETTINGS_FILE );
maven = new MavenEmbedder( configuration );
mavenEmbedder = new MavenEmbedder( configuration );
}
protected void tearDown()
throws Exception
{
maven.stop();
mavenEmbedder.stop();
}
protected void assertNoExceptions( MavenExecutionResult result )
@ -119,10 +119,12 @@ public void testExecutionUsingABaseDirectory()
FileUtils.copyDirectoryStructure( testDirectory, targetDirectory );
MavenExecutionRequest request = new DefaultMavenExecutionRequest().setBaseDirectory( targetDirectory )
.setShowErrors( true ).setGoals( Arrays.asList( new String[]{"package"} ) );
MavenExecutionRequest request = new DefaultMavenExecutionRequest()
.setBaseDirectory( targetDirectory )
.setShowErrors( true )
.setGoals( Arrays.asList( new String[]{"package"} ) );
MavenExecutionResult result = maven.execute( request );
MavenExecutionResult result = mavenEmbedder.execute( request );
assertNoExceptions( result );
@ -148,7 +150,7 @@ public void testWithInvalidGoal()
MavenExecutionRequest request = new DefaultMavenExecutionRequest().setBaseDirectory( targetDirectory )
.setShowErrors( true ).setGoals( Arrays.asList( new String[]{"validate"} ) );
MavenExecutionResult result = maven.execute( request );
MavenExecutionResult result = mavenEmbedder.execute( request );
List exceptions = result.getExceptions();
assertEquals("Incorrect number of exceptions", 1, exceptions.size());
@ -172,7 +174,7 @@ public void testExecutionUsingAPomFile()
.setPom( new File( targetDirectory, "pom.xml" ) ).setShowErrors( true )
.setGoals( Arrays.asList( new String[] { "package" } ) );
MavenExecutionResult result = maven.execute( request );
MavenExecutionResult result = mavenEmbedder.execute( request );
assertNoExceptions( result );
@ -200,7 +202,7 @@ public void testExecutionUsingAProfileWhichSetsAProperty()
.setPom( new File( targetDirectory, "pom.xml" ) ).setShowErrors( true )
.setGoals( Arrays.asList( new String[] { "validate" } ) );
MavenExecutionResult r0 = maven.execute( requestWithoutProfile );
MavenExecutionResult r0 = mavenEmbedder.execute( requestWithoutProfile );
assertNoExceptions( r0 );
@ -220,7 +222,7 @@ public void testExecutionUsingAProfileWhichSetsAProperty()
.setGoals( Arrays.asList( new String[] { "validate" } ) )
.addActiveProfile( "embedderProfile" );
MavenExecutionResult r1 = maven.execute( request );
MavenExecutionResult r1 = mavenEmbedder.execute( request );
MavenProject p1 = r1.getProject();
@ -248,7 +250,7 @@ public void testTwoExecutionsDoNotCacheChangedData()
File pom = new File( targetDirectory, "pom.xml" );
/* Add the surefire plugin 2.2 to the pom */
Model model = maven.readModel( pom );
Model model = mavenEmbedder.readModel( pom );
Plugin plugin = new Plugin();
plugin.setArtifactId( "maven-surefire-plugin" );
@ -257,14 +259,14 @@ public void testTwoExecutionsDoNotCacheChangedData()
model.getBuild().addPlugin( plugin );
Writer writer = WriterFactory.newXmlWriter( pom );
maven.writeModel( writer, model );
mavenEmbedder.writeModel( writer, model );
writer.close();
/* execute maven */
MavenExecutionRequest request = new DefaultMavenExecutionRequest().setPom( pom ).setShowErrors( true )
.setGoals( Arrays.asList( new String[] { "package" } ) );
MavenExecutionResult result = maven.execute( request );
MavenExecutionResult result = mavenEmbedder.execute( request );
assertNoExceptions( result );
@ -276,13 +278,13 @@ public void testTwoExecutionsDoNotCacheChangedData()
/* Add the surefire plugin 2.3 to the pom */
plugin.setVersion( "2.3" );
writer = WriterFactory.newXmlWriter( pom );
maven.writeModel( writer, model );
mavenEmbedder.writeModel( writer, model );
writer.close();
/* execute Maven */
request = new DefaultMavenExecutionRequest().setPom( pom ).setShowErrors( true )
.setGoals( Arrays.asList( new String[] { "package" } ) );
result = maven.execute( request );
result = mavenEmbedder.execute( request );
assertNoExceptions( result );
@ -299,7 +301,7 @@ public void testTwoExecutionsDoNotCacheChangedData()
public void testRetrievingLifecyclePhases()
throws Exception
{
List phases = maven.getLifecyclePhases();
List phases = mavenEmbedder.getLifecyclePhases();
assertEquals( "validate", (String) phases.get( 0 ) );
@ -315,7 +317,7 @@ public void testRetrievingLifecyclePhases()
public void testLocalRepositoryRetrieval()
throws Exception
{
assertNotNull( maven.getLocalRepository().getBasedir() );
assertNotNull( mavenEmbedder.getLocalRepository().getBasedir() );
}
// ----------------------------------------------------------------------
@ -329,7 +331,7 @@ public void testModelReading()
// Test model reading
// ----------------------------------------------------------------------
Model model = maven.readModel( getPomFile() );
Model model = mavenEmbedder.readModel( getPomFile() );
assertEquals( "org.apache.maven", model.getGroupId() );
}
@ -339,7 +341,7 @@ public void testProjectReading()
{
MavenExecutionRequest request = new DefaultMavenExecutionRequest().setShowErrors( true ).setPom( getPomFile() );
MavenExecutionResult result = maven.readProjectWithDependencies( request );
MavenExecutionResult result = mavenEmbedder.readProjectWithDependencies( request );
assertNoExceptions( result );
@ -347,7 +349,7 @@ public void testProjectReading()
Set artifacts = result.getProject().getArtifacts();
assertEquals( 1, artifacts.size() );
assertEquals( 2, artifacts.size() );
artifacts.iterator().next();
}
@ -357,7 +359,7 @@ public void testProjectReading_FromChildLevel_ScmInheritanceCalculations()
{
File pomFile = new File( basedir, "src/test/projects/readProject-withScmInheritance/modules/child1/pom.xml" );
MavenProject project = maven.readProject( pomFile );
MavenProject project = mavenEmbedder.readProject( pomFile );
assertEquals( "http://host/viewer?path=/trunk/parent/child1", project.getScm().getUrl() );
assertEquals( "scm:svn:http://host/trunk/parent/child1", project.getScm().getConnection() );
@ -370,7 +372,7 @@ public void testProjectReading_SkipMissingModuleSilently()
File pomFile = new File( basedir,
"src/test/projects/readProject-missingModuleIgnored/pom.xml" );
maven.readProject( pomFile );
mavenEmbedder.readProject( pomFile );
}
/*
@ -402,7 +404,7 @@ public void testProjectReadingWithDistributionStatus()
public void testModelWriting()
throws Exception
{
Model model = maven.readModel( getPomFile() );
Model model = mavenEmbedder.readModel( getPomFile() );
model.setGroupId( "org.apache.maven.new" );
@ -410,11 +412,11 @@ public void testModelWriting()
Writer writer = WriterFactory.newXmlWriter( file );
maven.writeModel( writer, model );
mavenEmbedder.writeModel( writer, model );
writer.close();
model = maven.readModel( file );
model = mavenEmbedder.readModel( file );
assertEquals( "org.apache.maven.new", model.getGroupId() );
}