Resolving: MNG-421

o External profiles (from settings.xml, profiles.xml) are now available before the main MavenProject is constructed, which allows repositories defined in external profiles to be used to resolve project parents and dependencies.

NOTE: I need to double-check whether the profile-defined repos are actually used to resolve the parent project(s)...there may be another commit following on the heels of this one.



git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@189667 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-06-08 23:34:35 +00:00
parent 0db2b646d2
commit 30b1434723
10 changed files with 85 additions and 228 deletions

View File

@ -32,7 +32,6 @@ import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.profile.AlwaysOnActivation;
import org.apache.maven.profiles.MavenProfilesBuilder;
import org.apache.maven.profiles.ProfilesRoot;
import org.apache.maven.project.ExternalProfileInjector;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
@ -84,8 +83,6 @@ public class DefaultMaven
protected MavenProfilesBuilder profilesBuilder;
protected ExternalProfileInjector externalProfileInjector;
// ----------------------------------------------------------------------
// Project execution
// ----------------------------------------------------------------------
@ -122,7 +119,9 @@ public class DefaultMaven
if ( projects.isEmpty() )
{
projects.add( projectBuilder.buildStandaloneSuperProject( request.getLocalRepository() ) );
List externalProfiles = getActiveExternalProfiles( null, request.getSettings() );
projects.add( projectBuilder.buildStandaloneSuperProject( request.getLocalRepository(), externalProfiles ) );
}
}
catch ( IOException e )
@ -314,15 +313,22 @@ public class DefaultMaven
}
}
MavenProject project = projectBuilder.build( pom, localRepository );
List externalProfiles = getActiveExternalProfiles( pom, settings );
MavenProject project = projectBuilder.build( pom, localRepository, externalProfiles );
return project;
}
private List getActiveExternalProfiles( File pom, Settings settings ) throws ProjectBuildingException
{
// TODO: apply profiles.xml and settings.xml Profiles here.
List externalProfiles = new ArrayList();
List settingsProfiles = settings.getProfiles();
if(settingsProfiles != null && !settingsProfiles.isEmpty())
{
List profiles = new ArrayList();
List settingsActiveProfileIds = settings.getActiveProfiles();
for ( Iterator it = settings.getProfiles().iterator(); it.hasNext(); )
@ -336,28 +342,24 @@ public class DefaultMaven
profile.setActivation( new AlwaysOnActivation() );
}
profiles.add( profile );
}
externalProfileInjector.injectExternalProfiles( project, profiles );
externalProfiles.add( profile );
}
}
if( pom != null )
{
try
{
ProfilesRoot root = profilesBuilder.buildProfiles( pom.getParentFile() );
if( root != null )
{
List profiles = new ArrayList();
for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); )
{
org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next();
profiles.add( ModelNormalizationUtils.convertFromProfileXmlProfile( rawProfile ) );
externalProfiles.add( ModelNormalizationUtils.convertFromProfileXmlProfile( rawProfile ) );
}
externalProfileInjector.injectExternalProfiles( project, profiles );
}
}
catch ( IOException e )
@ -368,8 +370,9 @@ public class DefaultMaven
{
throw new ProjectBuildingException( "Cannot parse profiles.xml resource for pom: " + pom, e );
}
}
return project;
return externalProfiles;
}
// ----------------------------------------------------------------------

View File

@ -59,7 +59,7 @@ public final class ModelNormalizationUtils
}
}
profile.setProperties( settingsProfile.getProperties() );
profile.addEvalProperties( settingsProfile.getProperties() );
List repos = settingsProfile.getRepositories();
if ( repos != null )
@ -117,7 +117,7 @@ public final class ModelNormalizationUtils
profile.setActivation( new AlwaysOnActivation() );
}
profile.setProperties( profileXmlProfile.getProperties() );
profile.addEvalProperties( profileXmlProfile.getProperties() );
List repos = profileXmlProfile.getRepositories();
if ( repos != null )

View File

@ -1,139 +0,0 @@
package org.apache.maven.project;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
import org.apache.maven.profile.activation.ProfileActivationCalculator;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
/*
* 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.
*/
public class ExternalProfileInjector
extends AbstractLogEnabled
implements Contextualizable
{
public static final String ROLE = ExternalProfileInjector.class.getName();
private ArtifactRepositoryFactory artifactRepositoryFactory;
private ProfileActivationCalculator profileActivationCalculator;
private PlexusContainer container;
public void injectExternalProfiles( MavenProject project, List profiles )
throws ProjectBuildingException
{
List activeProfiles = profileActivationCalculator.calculateActiveProfiles( profiles );
Model model = project.getModel();
for ( Iterator it = activeProfiles.iterator(); it.hasNext(); )
{
Profile profile = (Profile) it.next();
mergeRepositories( project, model, profile );
mergePluginRepositories( project, model, profile );
Properties props = profile.getProperties();
if( props != null )
{
project.addProfileConfiguration( props );
}
}
project.addActiveProfiles( activeProfiles );
}
private void mergePluginRepositories( MavenProject project, Model model, Profile profile ) throws ProjectBuildingException
{
List repos = profile.getPluginRepositories();
if( repos != null && !repos.isEmpty() )
{
List modelRepos = model.getPluginRepositories();
if( modelRepos == null )
{
modelRepos = new ArrayList();
model.setPluginRepositories( modelRepos );
}
modelRepos.addAll( repos );
List artifactRepos = ProjectUtils.buildArtifactRepositories( repos, artifactRepositoryFactory, container );
List projectRepos = project.getPluginArtifactRepositories();
if( projectRepos == null )
{
projectRepos = new ArrayList();
project.setPluginArtifactRepositories( projectRepos );
}
projectRepos.addAll( artifactRepos );
}
}
private void mergeRepositories( MavenProject project, Model model, Profile profile )
throws ProjectBuildingException
{
List repos = profile.getRepositories();
if( repos != null && !repos.isEmpty() )
{
List modelRepos = model.getRepositories();
if( modelRepos == null )
{
modelRepos = new ArrayList();
model.setRepositories( modelRepos );
}
modelRepos.addAll( repos );
List artifactRepos = ProjectUtils.buildArtifactRepositories( repos, artifactRepositoryFactory, container );
List projectRepos = project.getRemoteArtifactRepositories();
if( projectRepos == null )
{
projectRepos = new ArrayList();
project.setRemoteArtifactRepositories( projectRepos );
}
projectRepos.addAll( artifactRepos );
}
}
public void contextualize( Context context )
throws ContextException
{
this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
}
}

View File

@ -22,9 +22,6 @@
<requirement>
<role>org.apache.maven.profiles.MavenProfilesBuilder</role>
</requirement>
<requirement>
<role>org.apache.maven.project.ExternalProfileInjector</role>
</requirement>
</requirements>
</component>
<!--
@ -36,23 +33,6 @@
<role>org.apache.maven.profiles.MavenProfilesBuilder</role>
<implementation>org.apache.maven.profiles.DefaultMavenProfilesBuilder</implementation>
</component>
<!--
|
|
|
-->
<component>
<role>org.apache.maven.project.ExternalProfileInjector</role>
<implementation>org.apache.maven.project.ExternalProfileInjector</implementation>
<requirements>
<requirement>
<role>org.apache.maven.artifact.repository.ArtifactRepositoryFactory</role>
</requirement>
<requirement>
<role>org.apache.maven.profile.activation.ProfileActivationCalculator</role>
</requirement>
</requirements>
</component>
<!--
|
|PluginConfigurationDiagnoser

View File

@ -664,6 +664,24 @@
</association>
</field>
</fields>
<codeSegments>
<codeSegment>
<version>4.0.0</version>
<code><![CDATA[
private Properties evalProperties = new Properties();
public void addEvalProperties( Properties properties )
{
this.evalProperties.putAll( properties );
}
public Properties getEvalProperties()
{
return evalProperties;
}
]]></code>
</codeSegment>
</codeSegments>
</class>
<!-- @todo: is any of this too CVS specific? Investigate other SCMs -->
<class>
@ -2308,18 +2326,6 @@
<version>4.0.0</version>
<code><![CDATA[
// We don't want this to be parseable...it's sort of 'hidden'
private Properties properties;
public void setProperties( Properties properties )
{
this.properties = properties;
}
public Properties getProperties()
{
return properties;
}
// default source for this profile is in the pom itself.
private String source = "pom";

View File

@ -20,8 +20,7 @@
<plugin>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<!-- version>1.0-alpha-3-SNAPSHOT</version -->
<version>1.0-alpha-3-SNAPSHOT</version>
<configuration>
<version>4.0.0</version>
<model>maven.mdo</model>

View File

@ -58,6 +58,8 @@ import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@ -112,10 +114,10 @@ public class DefaultMavenProjectBuilder
// ----------------------------------------------------------------------
public MavenProject buildWithDependencies( File projectDescriptor, ArtifactRepository localRepository,
ArtifactMetadataSource artifactMetadataSource )
ArtifactMetadataSource artifactMetadataSource, List externalProfiles )
throws ProjectBuildingException, ArtifactResolutionException
{
MavenProject project = buildFromSourceFile( projectDescriptor, localRepository );
MavenProject project = buildFromSourceFile( projectDescriptor, localRepository, externalProfiles );
// ----------------------------------------------------------------------
// Typically when the project builder is being used from maven proper
@ -138,13 +140,13 @@ public class DefaultMavenProjectBuilder
return project;
}
public MavenProject build( File projectDescriptor, ArtifactRepository localRepository )
public MavenProject build( File projectDescriptor, ArtifactRepository localRepository, List externalProfiles )
throws ProjectBuildingException, ArtifactResolutionException
{
return buildFromSourceFile( projectDescriptor, localRepository );
return buildFromSourceFile( projectDescriptor, localRepository, externalProfiles );
}
private MavenProject buildFromSourceFile( File projectDescriptor, ArtifactRepository localRepository )
private MavenProject buildFromSourceFile( File projectDescriptor, ArtifactRepository localRepository, List externalProfiles )
throws ProjectBuildingException, ArtifactResolutionException
{
Model model = readModel( projectDescriptor );
@ -152,7 +154,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 );
MavenProject project = build( projectDescriptor.getAbsolutePath(), model, localRepository );
MavenProject project = build( projectDescriptor.getAbsolutePath(), model, localRepository, externalProfiles );
// Only translate the base directory for files in the source tree
pathTranslator.alignToBaseDirectory( project.getModel(), projectDescriptor );
@ -174,7 +176,7 @@ public class DefaultMavenProjectBuilder
{
Model model = findModelFromRepository( artifact, remoteArtifactRepositories, localRepository );
return build( "Artifact [" + artifact.getId() + "]", model, localRepository );
return build( "Artifact [" + artifact.getId() + "]", model, localRepository, Collections.EMPTY_LIST );
}
private Model findModelFromRepository( Artifact artifact, List remoteArtifactRepositories,
@ -212,7 +214,7 @@ public class DefaultMavenProjectBuilder
return model;
}
private MavenProject build( String pomLocation, Model model, ArtifactRepository localRepository )
private MavenProject build( String pomLocation, Model model, ArtifactRepository localRepository, List externalProfiles )
throws ProjectBuildingException, ArtifactResolutionException
{
Model superModel = getSuperModel();
@ -236,7 +238,7 @@ public class DefaultMavenProjectBuilder
try
{
project = processProjectLogic( pomLocation, project, aggregatedRemoteWagonRepositories );
project = processProjectLogic( pomLocation, project, aggregatedRemoteWagonRepositories, externalProfiles );
}
catch ( ModelInterpolationException e )
{
@ -252,7 +254,7 @@ public class DefaultMavenProjectBuilder
* the resolved source roots, etc for the parent - that occurs for the parent when it is constructed independently
* and projects are not cached or reused
*/
private MavenProject processProjectLogic( String pomLocation, MavenProject project, List remoteRepositories )
private MavenProject processProjectLogic( String pomLocation, MavenProject project, List remoteRepositories, List externalProfiles )
throws ProjectBuildingException, ModelInterpolationException
{
Model model = project.getModel();
@ -264,8 +266,13 @@ public class DefaultMavenProjectBuilder
}
// TODO: Add profiles support here?
List activeProfiles = new ArrayList( externalProfiles );
List activePomProfiles = profileActivationCalculator.calculateActiveProfiles( model.getProfiles() );
for ( Iterator it = activePomProfiles.iterator(); it.hasNext(); )
activeProfiles.addAll( activePomProfiles );
for ( Iterator it = activeProfiles.iterator(); it.hasNext(); )
{
Profile profile = (Profile) it.next();
@ -466,7 +473,7 @@ public class DefaultMavenProjectBuilder
return pluginArtifacts;
}
public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository )
public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository, List externalProfiles )
throws ProjectBuildingException
{
Model superModel = getSuperModel();
@ -485,7 +492,7 @@ public class DefaultMavenProjectBuilder
List remoteRepositories = ProjectUtils.buildArtifactRepositories( superModel.getRepositories(), artifactRepositoryFactory, container );
project = processProjectLogic( "<Super-POM>", project, remoteRepositories );
project = processProjectLogic( "<Super-POM>", project, remoteRepositories, externalProfiles );
return project;
}

View File

@ -34,11 +34,11 @@ public interface MavenProjectBuilder
static final String STANDALONE_SUPERPOM_VERSION = "2.0";
MavenProject build( File project, ArtifactRepository localRepository )
MavenProject build( File project, ArtifactRepository localRepository, List profiles )
throws ProjectBuildingException, ArtifactResolutionException;
MavenProject buildWithDependencies( File project, ArtifactRepository localRepository,
ArtifactMetadataSource artifactMetadataSource )
ArtifactMetadataSource artifactMetadataSource, List externalProfiles )
throws ProjectBuildingException, ArtifactResolutionException;
/**
@ -54,6 +54,6 @@ public interface MavenProjectBuilder
ArtifactRepository localRepository )
throws ProjectBuildingException, ArtifactResolutionException;
MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository )
MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository, List externalProfiles )
throws ProjectBuildingException;
}

View File

@ -152,11 +152,7 @@ public class DefaultModelInheritanceAssembler
{
assembleModelBaseInheritance( model, profile );
Build modelBuild = model.getBuild();
BuildBase profileBuild = profile.getBuild();
assembleBuildBaseInheritance( modelBuild, profileBuild );
assembleBuildBaseInheritance( model.getBuild(), profile.getBuild() );
}
private void assembleModelBaseInheritance( ModelBase child, ModelBase parent )
@ -225,6 +221,9 @@ public class DefaultModelInheritanceAssembler
}
assembleDependencyManagementInheritance( child, parent );
// Evaluation Properties :: aggregate
child.addEvalProperties( parent.getEvalProperties() );
}
private void assembleDependencyManagementInheritance( ModelBase child, ModelBase parent )

View File

@ -23,6 +23,7 @@ import org.codehaus.plexus.PlexusTestCase;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Collections;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
@ -93,13 +94,14 @@ public abstract class MavenProjectTestCase
throws Exception
{
return projectBuilder.buildWithDependencies( pom, getLocalRepository(),
new ProjectClasspathArtifactResolver.Source() );
new ProjectClasspathArtifactResolver.Source(),
Collections.EMPTY_LIST );
}
protected MavenProject getProject( File pom )
throws Exception
{
return projectBuilder.build( pom, getLocalRepository() );
return projectBuilder.build( pom, getLocalRepository(), Collections.EMPTY_LIST );
}
}