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.profile.AlwaysOnActivation;
import org.apache.maven.profiles.MavenProfilesBuilder; import org.apache.maven.profiles.MavenProfilesBuilder;
import org.apache.maven.profiles.ProfilesRoot; import org.apache.maven.profiles.ProfilesRoot;
import org.apache.maven.project.ExternalProfileInjector;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.ProjectBuildingException;
@ -84,8 +83,6 @@ public class DefaultMaven
protected MavenProfilesBuilder profilesBuilder; protected MavenProfilesBuilder profilesBuilder;
protected ExternalProfileInjector externalProfileInjector;
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// Project execution // Project execution
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@ -122,7 +119,9 @@ public class DefaultMaven
if ( projects.isEmpty() ) 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 ) 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. // TODO: apply profiles.xml and settings.xml Profiles here.
List externalProfiles = new ArrayList();
List settingsProfiles = settings.getProfiles(); List settingsProfiles = settings.getProfiles();
if(settingsProfiles != null && !settingsProfiles.isEmpty()) if(settingsProfiles != null && !settingsProfiles.isEmpty())
{ {
List profiles = new ArrayList();
List settingsActiveProfileIds = settings.getActiveProfiles(); List settingsActiveProfileIds = settings.getActiveProfiles();
for ( Iterator it = settings.getProfiles().iterator(); it.hasNext(); ) for ( Iterator it = settings.getProfiles().iterator(); it.hasNext(); )
@ -336,40 +342,37 @@ public class DefaultMaven
profile.setActivation( new AlwaysOnActivation() ); profile.setActivation( new AlwaysOnActivation() );
} }
profiles.add( profile ); externalProfiles.add( profile );
} }
externalProfileInjector.injectExternalProfiles( project, profiles );
} }
try if( pom != null )
{ {
ProfilesRoot root = profilesBuilder.buildProfiles( pom.getParentFile() ); try
if( root != null )
{ {
List profiles = new ArrayList(); ProfilesRoot root = profilesBuilder.buildProfiles( pom.getParentFile() );
for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); ) if( root != null )
{ {
org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next(); for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); )
{
profiles.add( ModelNormalizationUtils.convertFromProfileXmlProfile( rawProfile ) ); org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next();
externalProfiles.add( ModelNormalizationUtils.convertFromProfileXmlProfile( rawProfile ) );
}
} }
}
externalProfileInjector.injectExternalProfiles( project, profiles ); catch ( IOException e )
{
throw new ProjectBuildingException( "Cannot read profiles.xml resource for pom: " + pom, e );
}
catch ( XmlPullParserException e )
{
throw new ProjectBuildingException( "Cannot parse profiles.xml resource for pom: " + pom, e );
} }
} }
catch ( IOException e )
{
throw new ProjectBuildingException( "Cannot read profiles.xml resource for pom: " + pom, e );
}
catch ( XmlPullParserException e )
{
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(); List repos = settingsProfile.getRepositories();
if ( repos != null ) if ( repos != null )
@ -117,7 +117,7 @@ public final class ModelNormalizationUtils
profile.setActivation( new AlwaysOnActivation() ); profile.setActivation( new AlwaysOnActivation() );
} }
profile.setProperties( profileXmlProfile.getProperties() ); profile.addEvalProperties( profileXmlProfile.getProperties() );
List repos = profileXmlProfile.getRepositories(); List repos = profileXmlProfile.getRepositories();
if ( repos != null ) 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> <requirement>
<role>org.apache.maven.profiles.MavenProfilesBuilder</role> <role>org.apache.maven.profiles.MavenProfilesBuilder</role>
</requirement> </requirement>
<requirement>
<role>org.apache.maven.project.ExternalProfileInjector</role>
</requirement>
</requirements> </requirements>
</component> </component>
<!-- <!--
@ -36,23 +33,6 @@
<role>org.apache.maven.profiles.MavenProfilesBuilder</role> <role>org.apache.maven.profiles.MavenProfilesBuilder</role>
<implementation>org.apache.maven.profiles.DefaultMavenProfilesBuilder</implementation> <implementation>org.apache.maven.profiles.DefaultMavenProfilesBuilder</implementation>
</component> </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 |PluginConfigurationDiagnoser

View File

@ -664,6 +664,24 @@
</association> </association>
</field> </field>
</fields> </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> </class>
<!-- @todo: is any of this too CVS specific? Investigate other SCMs --> <!-- @todo: is any of this too CVS specific? Investigate other SCMs -->
<class> <class>
@ -2308,18 +2326,6 @@
<version>4.0.0</version> <version>4.0.0</version>
<code><![CDATA[ <code><![CDATA[
// We don't want this to be parseable...it's sort of 'hidden' // 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. // default source for this profile is in the pom itself.
private String source = "pom"; private String source = "pom";

View File

@ -20,8 +20,7 @@
<plugin> <plugin>
<groupId>org.codehaus.modello</groupId> <groupId>org.codehaus.modello</groupId>
<artifactId>modello-maven-plugin</artifactId> <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> <configuration>
<version>4.0.0</version> <version>4.0.0</version>
<model>maven.mdo</model> <model>maven.mdo</model>

View File

@ -58,6 +58,8 @@ import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -112,10 +114,10 @@ public class DefaultMavenProjectBuilder
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
public MavenProject buildWithDependencies( File projectDescriptor, ArtifactRepository localRepository, public MavenProject buildWithDependencies( File projectDescriptor, ArtifactRepository localRepository,
ArtifactMetadataSource artifactMetadataSource ) ArtifactMetadataSource artifactMetadataSource, List externalProfiles )
throws ProjectBuildingException, ArtifactResolutionException 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 // Typically when the project builder is being used from maven proper
@ -138,13 +140,13 @@ public class DefaultMavenProjectBuilder
return project; return project;
} }
public MavenProject build( File projectDescriptor, ArtifactRepository localRepository ) public MavenProject build( File projectDescriptor, ArtifactRepository localRepository, List externalProfiles )
throws ProjectBuildingException, ArtifactResolutionException 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 throws ProjectBuildingException, ArtifactResolutionException
{ {
Model model = readModel( projectDescriptor ); Model model = readModel( projectDescriptor );
@ -152,7 +154,7 @@ public class DefaultMavenProjectBuilder
// Always cache files in the source tree over those in the repository // Always cache files in the source tree over those in the repository
modelCache.put( createCacheKey( model.getGroupId(), model.getArtifactId(), model.getVersion() ), model ); 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 // Only translate the base directory for files in the source tree
pathTranslator.alignToBaseDirectory( project.getModel(), projectDescriptor ); pathTranslator.alignToBaseDirectory( project.getModel(), projectDescriptor );
@ -174,7 +176,7 @@ public class DefaultMavenProjectBuilder
{ {
Model model = findModelFromRepository( artifact, remoteArtifactRepositories, localRepository ); 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, private Model findModelFromRepository( Artifact artifact, List remoteArtifactRepositories,
@ -212,7 +214,7 @@ public class DefaultMavenProjectBuilder
return model; 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 throws ProjectBuildingException, ArtifactResolutionException
{ {
Model superModel = getSuperModel(); Model superModel = getSuperModel();
@ -236,7 +238,7 @@ public class DefaultMavenProjectBuilder
try try
{ {
project = processProjectLogic( pomLocation, project, aggregatedRemoteWagonRepositories ); project = processProjectLogic( pomLocation, project, aggregatedRemoteWagonRepositories, externalProfiles );
} }
catch ( ModelInterpolationException e ) 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 * 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 * 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 throws ProjectBuildingException, ModelInterpolationException
{ {
Model model = project.getModel(); Model model = project.getModel();
@ -264,8 +266,13 @@ public class DefaultMavenProjectBuilder
} }
// TODO: Add profiles support here? // TODO: Add profiles support here?
List activeProfiles = new ArrayList( externalProfiles );
List activePomProfiles = profileActivationCalculator.calculateActiveProfiles( model.getProfiles() ); 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(); Profile profile = (Profile) it.next();
@ -466,7 +473,7 @@ public class DefaultMavenProjectBuilder
return pluginArtifacts; return pluginArtifacts;
} }
public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository ) public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository, List externalProfiles )
throws ProjectBuildingException throws ProjectBuildingException
{ {
Model superModel = getSuperModel(); Model superModel = getSuperModel();
@ -485,7 +492,7 @@ public class DefaultMavenProjectBuilder
List remoteRepositories = ProjectUtils.buildArtifactRepositories( superModel.getRepositories(), artifactRepositoryFactory, container ); List remoteRepositories = ProjectUtils.buildArtifactRepositories( superModel.getRepositories(), artifactRepositoryFactory, container );
project = processProjectLogic( "<Super-POM>", project, remoteRepositories ); project = processProjectLogic( "<Super-POM>", project, remoteRepositories, externalProfiles );
return project; return project;
} }

View File

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

View File

@ -152,11 +152,7 @@ public class DefaultModelInheritanceAssembler
{ {
assembleModelBaseInheritance( model, profile ); assembleModelBaseInheritance( model, profile );
Build modelBuild = model.getBuild(); assembleBuildBaseInheritance( model.getBuild(), profile.getBuild() );
BuildBase profileBuild = profile.getBuild();
assembleBuildBaseInheritance( modelBuild, profileBuild );
} }
private void assembleModelBaseInheritance( ModelBase child, ModelBase parent ) private void assembleModelBaseInheritance( ModelBase child, ModelBase parent )
@ -225,6 +221,9 @@ public class DefaultModelInheritanceAssembler
} }
assembleDependencyManagementInheritance( child, parent ); assembleDependencyManagementInheritance( child, parent );
// Evaluation Properties :: aggregate
child.addEvalProperties( parent.getEvalProperties() );
} }
private void assembleDependencyManagementInheritance( ModelBase child, ModelBase parent ) 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.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.net.URL; import java.net.URL;
import java.util.Collections;
/** /**
* @author <a href="mailto:jason@maven.org">Jason van Zyl </a> * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
@ -93,13 +94,14 @@ public abstract class MavenProjectTestCase
throws Exception throws Exception
{ {
return projectBuilder.buildWithDependencies( pom, getLocalRepository(), return projectBuilder.buildWithDependencies( pom, getLocalRepository(),
new ProjectClasspathArtifactResolver.Source() ); new ProjectClasspathArtifactResolver.Source(),
Collections.EMPTY_LIST );
} }
protected MavenProject getProject( File pom ) protected MavenProject getProject( File pom )
throws Exception throws Exception
{ {
return projectBuilder.build( pom, getLocalRepository() ); return projectBuilder.build( pom, getLocalRepository(), Collections.EMPTY_LIST );
} }
} }