From fe5a91b0d626193d59db343dd9d5e582d0ef289b Mon Sep 17 00:00:00 2001 From: John Dennis Casey Date: Tue, 16 Aug 2005 16:31:02 +0000 Subject: [PATCH] Working on: MNG-483 Fixing profile application to separate profiles discovered in and around POM from those in settings.xml, and apply them separately in the order: for-each-project-in-inheritance:{POM, profiles.xml}, settings.xml Added common interface for accumulating, explicitly activating and deactivating, and retrieving profiles to be applied to a given project. This manager interface (ProfileManager) is general enough to be applicable to both the project-level and settings-level profiles. Added 'performRelease'-keyed profile to super-POM which will be used by the release plugin and anyone using a parallel process, and which will enable '-DupdateReleaseInfo=true' for the deploy mojo, along with enabling the source attachment for the project. Added 'attach' parameter to JarSourceMojo to allow local POM to turn off source attachments, overriding release profile in super-pom. Updated the release:perform mojo to use '-DperformRelease=true' for switching on the new release profile, rather than just using '-DupdateReleaseInfo=true'... git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@233013 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/maven/DefaultMaven.java | 90 +++----- .../java/org/apache/maven/cli/MavenCli.java | 56 +++-- .../DefaultMavenExecutionRequest.java | 13 +- .../execution/MavenExecutionRequest.java | 3 + .../resources/META-INF/plexus/components.xml | 3 - maven-mboot2/src/main/java/MBoot.java | 2 +- .../plugin/eclipse/EclipsePluginTest.java | 3 +- .../plugins/release/PerformReleaseMojo.java | 2 +- .../maven/plugin/source/JarSourceMojo.java | 14 +- maven-profile/pom.xml | 4 +- .../maven/profiles/AlwaysOnActivation.java | 0 maven-project/pom.xml | 5 + .../maven/profiles/DefaultProfileManager.java | 214 ++++++++++++++++++ .../apache/maven/profiles/ProfileManager.java | 34 +++ .../activation/DetectedProfileActivator.java | 2 +- .../ExplicitListingProfileActivator.java | 34 --- .../ProfileActivationCalculator.java | 105 --------- .../ProfileActivationException.java | 17 ++ .../activation/ProfileActivationUtils.java | 67 ------ .../project/DefaultMavenProjectBuilder.java | 198 ++++++++++++---- .../apache/maven/project/MavenProject.java | 28 +++ .../maven/project/MavenProjectBuilder.java | 7 +- .../resources/META-INF/plexus/components.xml | 25 +- .../org/apache/maven/project/pom-4.0.0.xml | 32 +++ .../maven/project/MavenProjectTestCase.java | 6 +- .../maven/project/ProjectClasspathTest.xml | 6 +- 26 files changed, 595 insertions(+), 375 deletions(-) rename {maven-project => maven-profile}/src/main/java/org/apache/maven/profiles/AlwaysOnActivation.java (100%) create mode 100644 maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java create mode 100644 maven-project/src/main/java/org/apache/maven/profiles/ProfileManager.java delete mode 100644 maven-project/src/main/java/org/apache/maven/profiles/activation/ExplicitListingProfileActivator.java delete mode 100644 maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationCalculator.java create mode 100644 maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationException.java delete mode 100644 maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationUtils.java diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 5e8333c94b..beafa195f3 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -31,10 +31,8 @@ import org.apache.maven.model.Profile; import org.apache.maven.monitor.event.EventDispatcher; import org.apache.maven.monitor.event.MavenEvents; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.profiles.AlwaysOnActivation; -import org.apache.maven.profiles.MavenProfilesBuilder; -import org.apache.maven.profiles.ProfilesConversionUtils; -import org.apache.maven.profiles.ProfilesRoot; +import org.apache.maven.profiles.ProfileManager; +import org.apache.maven.profiles.activation.ProfileActivationException; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.ProjectBuildingException; @@ -55,7 +53,6 @@ import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.dag.CycleDetectedException; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; import java.io.IOException; @@ -82,15 +79,13 @@ public class DefaultMaven // ---------------------------------------------------------------------- protected MavenProjectBuilder projectBuilder; - + protected LifecycleExecutor lifecycleExecutor; protected PlexusContainer container; protected Map errorDiagnosers; - protected MavenProfilesBuilder profilesBuilder; - protected RuntimeInformation runtimeInformation; private static final long MB = 1024 * 1024; @@ -132,22 +127,23 @@ public class DefaultMaven ReactorManager rm; + ProfileManager globalProfileManager = request.getGlobalProfileManager(); + try { + loadSettingsProfiles( globalProfileManager, request.getSettings() ); + List files = getProjectFiles( request ); List projects = collectProjects( files, request.getLocalRepository(), request.isRecursive(), - request.getSettings() ); + request.getSettings(), globalProfileManager ); // the reasoning here is that the list is still unsorted according to dependency, so the first project // SHOULD BE the top-level, or the one we want to start with if we're doing an aggregated build. if ( projects.isEmpty() ) { - List externalProfiles = getActiveExternalProfiles( null, request.getSettings() ); - - MavenProject superProject = projectBuilder.buildStandaloneSuperProject( request.getLocalRepository(), - externalProfiles ); + MavenProject superProject = projectBuilder.buildStandaloneSuperProject( request.getLocalRepository() ); projects.add( superProject ); } @@ -176,6 +172,10 @@ public class DefaultMaven { return dispatchErrorResponse( dispatcher, event, request.getBaseDirectory(), e ); } + catch ( ProfileActivationException e ) + { + return dispatchErrorResponse( dispatcher, event, request.getBaseDirectory(), e ); + } try { @@ -324,8 +324,9 @@ public class DefaultMaven return response; } - private List collectProjects( List files, ArtifactRepository localRepository, boolean recursive, Settings settings ) - throws ProjectBuildingException, ReactorException, IOException, ArtifactResolutionException + private List collectProjects( List files, ArtifactRepository localRepository, boolean recursive, Settings settings, + ProfileManager globalProfileManager ) + throws ProjectBuildingException, ReactorException, IOException, ArtifactResolutionException, ProfileActivationException { List projects = new ArrayList( files.size() ); @@ -338,7 +339,7 @@ public class DefaultMaven getLogger().info( "NOTE: Using release-pom: " + file + " in reactor build." ); } - MavenProject project = getProject( file, localRepository, settings ); + MavenProject project = getProject( file, localRepository, settings, globalProfileManager ); if ( project.getPrerequesites() != null && project.getPrerequesites().getMaven() != null ) { @@ -365,7 +366,7 @@ public class DefaultMaven moduleFiles.add( new File( basedir, name + "/pom.xml" ) ); } - List collectedProjects = collectProjects( moduleFiles, localRepository, recursive, settings ); + List collectedProjects = collectProjects( moduleFiles, localRepository, recursive, settings, globalProfileManager ); projects.addAll( collectedProjects ); project.setCollectedProjects( collectedProjects ); } @@ -375,8 +376,9 @@ public class DefaultMaven return projects; } - public MavenProject getProject( File pom, ArtifactRepository localRepository, Settings settings ) - throws ProjectBuildingException, ArtifactResolutionException + public MavenProject getProject( File pom, ArtifactRepository localRepository, Settings settings, + ProfileManager globalProfileManager ) + throws ProjectBuildingException, ArtifactResolutionException, ProfileActivationException { if ( pom.exists() ) { @@ -387,22 +389,18 @@ public class DefaultMaven } } - List externalProfiles = getActiveExternalProfiles( pom, settings ); - - return projectBuilder.build( pom, localRepository, externalProfiles ); + return projectBuilder.build( pom, localRepository, globalProfileManager ); } - private List getActiveExternalProfiles( File pom, Settings settings ) - throws ProjectBuildingException + private void loadSettingsProfiles( ProfileManager profileManager, Settings settings ) { - // TODO: apply profiles.xml and settings.xml Profiles here. - List externalProfiles = new ArrayList(); - List settingsProfiles = settings.getProfiles(); if ( settingsProfiles != null && !settingsProfiles.isEmpty() ) { List settingsActiveProfileIds = settings.getActiveProfiles(); + + profileManager.explicitlyActivate( settingsActiveProfileIds ); for ( Iterator it = settings.getProfiles().iterator(); it.hasNext(); ) { @@ -410,44 +408,12 @@ public class DefaultMaven Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile ); - if ( settingsActiveProfileIds.contains( rawProfile.getId() ) ) - { - profile.setActivation( new AlwaysOnActivation() ); - } - - externalProfiles.add( profile ); + profileManager.addProfile( profile ); } } - - if ( pom != null ) - { - try - { - ProfilesRoot root = profilesBuilder.buildProfiles( pom.getParentFile() ); - - if ( root != null ) - { - for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); ) - { - org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next(); - - externalProfiles.add( ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile ) ); - } - } - } - 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 externalProfiles; + } - + // ---------------------------------------------------------------------- // Methods used by all execution request handlers // ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java index 3eec145e49..71dce8d169 100644 --- a/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java @@ -38,7 +38,8 @@ import org.apache.maven.monitor.event.DefaultEventDispatcher; import org.apache.maven.monitor.event.DefaultEventMonitor; import org.apache.maven.monitor.event.EventDispatcher; import org.apache.maven.plugin.Mojo; -import org.apache.maven.profiles.activation.ProfileActivationUtils; +import org.apache.maven.profiles.DefaultProfileManager; +import org.apache.maven.profiles.ProfileManager; import org.apache.maven.reactor.ReactorException; import org.apache.maven.settings.MavenSettingsBuilder; import org.apache.maven.settings.Settings; @@ -55,6 +56,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Properties; +import java.util.StringTokenizer; /** * @author Jason van Zyl @@ -112,12 +114,6 @@ public class MavenCli initializeSystemProperties( commandLine ); - if ( commandLine.hasOption( CLIManager.ACTIVATE_PROFILES ) ) - { - System.setProperty( ProfileActivationUtils.ACTIVE_PROFILE_IDS, - commandLine.getOptionValue( CLIManager.ACTIVATE_PROFILES ) ); - } - boolean debug = commandLine.hasOption( CLIManager.DEBUG ); boolean showErrors = debug || commandLine.hasOption( CLIManager.ERRORS ); @@ -240,17 +236,45 @@ public class MavenCli Maven maven = null; MavenExecutionRequest request = null; - LoggerManager manager = null; + LoggerManager loggerManager = null; try { // logger must be created first - manager = (LoggerManager) embedder.lookup( LoggerManager.ROLE ); + loggerManager = (LoggerManager) embedder.lookup( LoggerManager.ROLE ); if ( debug ) { - manager.setThreshold( Logger.LEVEL_DEBUG ); + loggerManager.setThreshold( Logger.LEVEL_DEBUG ); + } + + ProfileManager profileManager = new DefaultProfileManager( embedder.getContainer() ); + + if ( commandLine.hasOption( CLIManager.ACTIVATE_PROFILES ) ) + { + String profilesLine = commandLine.getOptionValue( CLIManager.ACTIVATE_PROFILES ); + + StringTokenizer profileTokens = new StringTokenizer( profilesLine, "," ); + + while( profileTokens.hasMoreTokens() ) + { + String profileAction = profileTokens.nextToken().trim(); + + if ( profileAction.startsWith( "-" ) ) + { + profileManager.explicitlyDeactivate( profileAction.substring( 1 ) ); + } + else if ( profileAction.startsWith( "+" ) ) + { + profileManager.explicitlyActivate(profileAction.substring( 1 ) ); + } + else + { + // TODO: deprecate this eventually! + profileManager.explicitlyActivate( profileAction ); + } + } } - request = createRequest( embedder, commandLine, settings, eventDispatcher, manager ); + request = createRequest( embedder, commandLine, settings, eventDispatcher, loggerManager, profileManager ); setProjectFileOptions( commandLine, request ); @@ -263,11 +287,11 @@ public class MavenCli } finally { - if ( manager != null ) + if ( loggerManager != null ) { try { - embedder.release( manager ); + embedder.release( loggerManager ); } catch ( ComponentLifecycleException e ) { @@ -324,7 +348,7 @@ public class MavenCli private static MavenExecutionRequest createRequest( Embedder embedder, CommandLine commandLine, Settings settings, EventDispatcher eventDispatcher, - LoggerManager manager ) + LoggerManager loggerManager, ProfileManager profileManager ) throws ComponentLookupException { MavenExecutionRequest request = null; @@ -332,10 +356,10 @@ public class MavenCli ArtifactRepository localRepository = createLocalRepository( embedder, settings, commandLine ); request = new DefaultMavenExecutionRequest( localRepository, settings, eventDispatcher, - commandLine.getArgList(), userDir.getPath() ); + commandLine.getArgList(), userDir.getPath(), profileManager ); // TODO [BP]: do we set one per mojo? where to do it? - Logger logger = manager.getLoggerForComponent( Mojo.ROLE ); + Logger logger = loggerManager.getLoggerForComponent( Mojo.ROLE ); if ( logger != null ) { request.addEventMonitor( new DefaultEventMonitor( logger ) ); diff --git a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java index d1a382280b..55f0fc2793 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java @@ -19,6 +19,7 @@ package org.apache.maven.execution; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.monitor.event.EventDispatcher; import org.apache.maven.monitor.event.EventMonitor; +import org.apache.maven.profiles.ProfileManager; import org.apache.maven.settings.Settings; import java.util.List; @@ -53,8 +54,11 @@ public class DefaultMavenExecutionRequest private String failureBehavior; + private final ProfileManager globalProfileManager; + public DefaultMavenExecutionRequest( ArtifactRepository localRepository, Settings settings, - EventDispatcher eventDispatcher, List goals, String baseDirectory ) + EventDispatcher eventDispatcher, List goals, String baseDirectory, + ProfileManager globalProfileManager ) { this.localRepository = localRepository; @@ -65,6 +69,8 @@ public class DefaultMavenExecutionRequest this.eventDispatcher = eventDispatcher; this.baseDirectory = baseDirectory; + + this.globalProfileManager = globalProfileManager; } public Settings getSettings() @@ -150,4 +156,9 @@ public class DefaultMavenExecutionRequest { return failureBehavior; } + + public ProfileManager getGlobalProfileManager() + { + return globalProfileManager; + } } diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java index 3fad6a914a..0e633ddb2f 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java @@ -19,6 +19,7 @@ package org.apache.maven.execution; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.monitor.event.EventDispatcher; import org.apache.maven.monitor.event.EventMonitor; +import org.apache.maven.profiles.ProfileManager; import org.apache.maven.settings.Settings; import java.util.List; @@ -60,4 +61,6 @@ public interface MavenExecutionRequest void setFailureBehavior( String failureBehavior ); String getFailureBehavior(); + + ProfileManager getGlobalProfileManager(); } diff --git a/maven-core/src/main/resources/META-INF/plexus/components.xml b/maven-core/src/main/resources/META-INF/plexus/components.xml index 73af020167..7f14ca82b5 100644 --- a/maven-core/src/main/resources/META-INF/plexus/components.xml +++ b/maven-core/src/main/resources/META-INF/plexus/components.xml @@ -71,9 +71,6 @@ org.apache.maven.usability.ErrorDiagnoser errorDiagnosers - - org.apache.maven.profiles.MavenProfilesBuilder - org.apache.maven.execution.RuntimeInformation diff --git a/maven-mboot2/src/main/java/MBoot.java b/maven-mboot2/src/main/java/MBoot.java index 0d6c29b9c6..f8a6338404 100644 --- a/maven-mboot2/src/main/java/MBoot.java +++ b/maven-mboot2/src/main/java/MBoot.java @@ -40,7 +40,7 @@ public class MBoot String[] builds = new String[]{"maven-model", "maven-settings", "maven-monitor", "maven-plugin-api", "maven-artifact", "maven-plugin-descriptor", "maven-artifact-manager", "maven-artifact-test", "maven-plugin-mapping", - "maven-script/maven-script-beanshell", "maven-script/maven-script-marmalade", "maven-project", "maven-profile", + "maven-script/maven-script-beanshell", "maven-script/maven-script-marmalade", "maven-profile", "maven-project", "maven-plugin-registry", "maven-reporting/maven-reporting-api", "maven-reporting/maven-reporting-impl", "maven-core", "maven-archiver", "maven-plugin-tools/maven-plugin-tools-api", "maven-plugin-tools/maven-plugin-tools-java", "maven-plugin-tools/maven-plugin-tools-beanshell", "maven-plugin-tools/maven-plugin-tools-pluggy", diff --git a/maven-plugins/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/EclipsePluginTest.java b/maven-plugins/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/EclipsePluginTest.java index 111ef1ebd0..bdef58be96 100644 --- a/maven-plugins/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/EclipsePluginTest.java +++ b/maven-plugins/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/EclipsePluginTest.java @@ -29,7 +29,6 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -72,7 +71,7 @@ public class EclipsePluginTest ArtifactRepository localRepository = new DefaultArtifactRepository( "local", "file://" + repo.getAbsolutePath(), localRepositoryLayout ); - MavenProject project = builder.buildWithDependencies( new File( basedir, "project.xml" ), localRepository, Collections.EMPTY_LIST ); + MavenProject project = builder.buildWithDependencies( new File( basedir, "project.xml" ), localRepository, null ); for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); ) { diff --git a/maven-plugins/maven-release-plugin/src/main/java/org/apache/maven/plugins/release/PerformReleaseMojo.java b/maven-plugins/maven-release-plugin/src/main/java/org/apache/maven/plugins/release/PerformReleaseMojo.java index 600c0b7aac..2c158b8366 100644 --- a/maven-plugins/maven-release-plugin/src/main/java/org/apache/maven/plugins/release/PerformReleaseMojo.java +++ b/maven-plugins/maven-release-plugin/src/main/java/org/apache/maven/plugins/release/PerformReleaseMojo.java @@ -101,7 +101,7 @@ public class PerformReleaseMojo cl.createArgument().setLine( goals ); - cl.createArgument().setLine( "-DupdateReleaseInfo=true" ); + cl.createArgument().setLine( "-DperformRelease=true" ); cl.createArgument().setLine( "--no-plugin-updates" ); diff --git a/maven-plugins/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java b/maven-plugins/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java index a93a9c9730..56b29efb5d 100644 --- a/maven-plugins/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java +++ b/maven-plugins/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java @@ -54,6 +54,11 @@ public class JarSourceMojo * @required */ private String finalName; + + /** + * @parameter expression="${attach}" default-value="true" + */ + private boolean attach = true; /** * @parameter expression="${project.compileSourceRoots}" @@ -66,10 +71,17 @@ public class JarSourceMojo * @required */ private File outputDirectory; - + public void execute() throws MojoExecutionException { + if ( !attach ) + { + getLog().info( "NOT adding java-sources to attached artifacts list." ); + + return; + } + // TODO: this should be via a release profile instead if ( project.getVersion().indexOf( "SNAPSHOT" ) < 0 ) { diff --git a/maven-profile/pom.xml b/maven-profile/pom.xml index 221d6a3121..d04a0f01bd 100644 --- a/maven-profile/pom.xml +++ b/maven-profile/pom.xml @@ -22,11 +22,11 @@ maven-model 2.0-beta-1-SNAPSHOT - + diff --git a/maven-project/src/main/java/org/apache/maven/profiles/AlwaysOnActivation.java b/maven-profile/src/main/java/org/apache/maven/profiles/AlwaysOnActivation.java similarity index 100% rename from maven-project/src/main/java/org/apache/maven/profiles/AlwaysOnActivation.java rename to maven-profile/src/main/java/org/apache/maven/profiles/AlwaysOnActivation.java diff --git a/maven-project/pom.xml b/maven-project/pom.xml index e2bb092d97..4601fc40f5 100755 --- a/maven-project/pom.xml +++ b/maven-project/pom.xml @@ -18,6 +18,11 @@ maven-model 2.0-beta-1-SNAPSHOT + + org.apache.maven + maven-profile + 2.0-beta-1-SNAPSHOT + org.apache.maven maven-artifact diff --git a/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java b/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java new file mode 100644 index 0000000000..afa732c5a5 --- /dev/null +++ b/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java @@ -0,0 +1,214 @@ +package org.apache.maven.profiles; + +import org.apache.maven.model.Profile; +import org.apache.maven.profiles.activation.ProfileActivationException; +import org.apache.maven.profiles.activation.ProfileActivator; +import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Map.Entry; + +/* + * 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 DefaultProfileManager implements ProfileManager +{ + private PlexusContainer container; + + private Set activatedIds = new HashSet(); + private Set deactivatedIds = new HashSet(); + + private Map profilesById = new HashMap(); + + public DefaultProfileManager( PlexusContainer container ) + { + this.container = container; + } + + public DefaultProfileManager( ProfileManager globals, PlexusContainer container ) + { + this.container = container; + + this.activatedIds.addAll( globals.getActivatedIds() ); + this.deactivatedIds.addAll( globals.getDeactivatedIds() ); + this.profilesById.putAll( globals.getProfilesById() ); + } + + public Set getActivatedIds() + { + return activatedIds; + } + + public Set getDeactivatedIds() + { + return deactivatedIds; + } + + public Map getProfilesById() + { + return profilesById; + } + + /* (non-Javadoc) + * @see org.apache.maven.profiles.ProfileManager#addProfile(org.apache.maven.model.Profile) + */ + public void addProfile( Profile profile ) + { + String profileId = profile.getId(); + + Profile existing = (Profile) profilesById.get( profileId ); + if ( existing != null ) + { + container.getLogger().warn( + "Overriding profile: \'" + profileId + "\' (source: " + existing.getSource() + + ") with new instance from source: " + profile.getSource() ); + } + + profilesById.put( profile.getId(), profile ); + } + + /* (non-Javadoc) + * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String) + */ + public void explicitlyActivate( String profileId ) + { + container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly activated." ); + + activatedIds.add( profileId ); + } + + /* (non-Javadoc) + * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List) + */ + public void explicitlyActivate( List profileIds ) + { + for ( Iterator it = profileIds.iterator(); it.hasNext(); ) + { + String profileId = (String) it.next(); + + explicitlyActivate( profileId ); + } + } + + /* (non-Javadoc) + * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String) + */ + public void explicitlyDeactivate( String profileId ) + { + container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." ); + + deactivatedIds.add( profileId ); + } + + /* (non-Javadoc) + * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List) + */ + public void explicitlyDeactivate( List profileIds ) + { + for ( Iterator it = profileIds.iterator(); it.hasNext(); ) + { + String profileId = (String) it.next(); + + explicitlyDeactivate( profileId ); + } + } + + /* (non-Javadoc) + * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles() + */ + public List getActiveProfiles() throws ProfileActivationException + { + List active = new ArrayList( profilesById.size() ); + + for ( Iterator it = profilesById.entrySet().iterator(); it.hasNext(); ) + { + Map.Entry entry = (Entry) it.next(); + + String profileId = (String) entry.getKey(); + Profile profile = (Profile) entry.getValue(); + + if ( activatedIds.contains( profileId ) ) + { + active.add( profile ); + } + else if ( !deactivatedIds.contains( profileId ) && isActive( profile ) ) + { + active.add( profile ); + } + } + + return active; + } + + private boolean isActive( Profile profile ) + throws ProfileActivationException + { + List activators = null; + try + { + activators = container.lookupList( ProfileActivator.ROLE ); + + for ( Iterator activatorIterator = activators.iterator(); activatorIterator.hasNext(); ) + { + ProfileActivator activator = (ProfileActivator) activatorIterator.next(); + + if ( activator.canDetermineActivation( profile ) ) + { + return activator.isActive( profile ); + } + } + + return false; + } + catch ( ComponentLookupException e ) + { + throw new ProfileActivationException( "Cannot retrieve list of profile activators.", e ); + } + finally + { + try + { + container.releaseAll( activators ); + } + catch ( ComponentLifecycleException e ) + { + container.getLogger().debug( "Error releasing profile activators - ignoring.", e ); + } + } + } + + /* (non-Javadoc) + * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List) + */ + public void addProfiles( List profiles ) + { + for ( Iterator it = profiles.iterator(); it.hasNext(); ) + { + Profile profile = (Profile) it.next(); + + addProfile( profile ); + } + } + +} diff --git a/maven-project/src/main/java/org/apache/maven/profiles/ProfileManager.java b/maven-project/src/main/java/org/apache/maven/profiles/ProfileManager.java new file mode 100644 index 0000000000..aa8ec1fd6c --- /dev/null +++ b/maven-project/src/main/java/org/apache/maven/profiles/ProfileManager.java @@ -0,0 +1,34 @@ +package org.apache.maven.profiles; + +import org.apache.maven.model.Profile; +import org.apache.maven.profiles.activation.ProfileActivationException; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public interface ProfileManager +{ + + void addProfile( Profile profile ); + + void explicitlyActivate( String profileId ); + + void explicitlyActivate( List profileIds ); + + void explicitlyDeactivate( String profileId ); + + void explicitlyDeactivate( List profileIds ); + + List getActiveProfiles() + throws ProfileActivationException; + + void addProfiles( List profiles ); + + public Set getActivatedIds(); + + public Set getDeactivatedIds(); + + public Map getProfilesById(); + +} \ No newline at end of file diff --git a/maven-project/src/main/java/org/apache/maven/profiles/activation/DetectedProfileActivator.java b/maven-project/src/main/java/org/apache/maven/profiles/activation/DetectedProfileActivator.java index b2cadf7a12..a52b5ba65f 100644 --- a/maven-project/src/main/java/org/apache/maven/profiles/activation/DetectedProfileActivator.java +++ b/maven-project/src/main/java/org/apache/maven/profiles/activation/DetectedProfileActivator.java @@ -23,7 +23,7 @@ public abstract class DetectedProfileActivator { public boolean canDetermineActivation( Profile profile ) { - return !ProfileActivationUtils.profilesWereExplicitlyGiven() && canDetectActivation( profile ); + return canDetectActivation( profile ); } protected abstract boolean canDetectActivation( Profile profile ); diff --git a/maven-project/src/main/java/org/apache/maven/profiles/activation/ExplicitListingProfileActivator.java b/maven-project/src/main/java/org/apache/maven/profiles/activation/ExplicitListingProfileActivator.java deleted file mode 100644 index 128fa8ed5f..0000000000 --- a/maven-project/src/main/java/org/apache/maven/profiles/activation/ExplicitListingProfileActivator.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.apache.maven.profiles.activation; - -import org.apache.maven.model.Profile; - -/* - * 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 ExplicitListingProfileActivator - implements ProfileActivator -{ - public boolean canDetermineActivation( Profile profile ) - { - return ProfileActivationUtils.profilesWereExplicitlyGiven(); - } - - public boolean isActive( Profile profile ) - { - return ProfileActivationUtils.getExplicitProfileList().contains( profile.getId() ); - } - -} diff --git a/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationCalculator.java b/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationCalculator.java deleted file mode 100644 index 9dde6c5151..0000000000 --- a/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationCalculator.java +++ /dev/null @@ -1,105 +0,0 @@ -package org.apache.maven.profiles.activation; - -import org.apache.maven.model.Activation; -import org.apache.maven.model.Profile; -import org.apache.maven.project.ProjectBuildingException; -import org.codehaus.plexus.PlexusConstants; -import org.codehaus.plexus.PlexusContainer; -import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -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; - -/* - * 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 ProfileActivationCalculator - extends AbstractLogEnabled - implements Contextualizable -{ - public static final String ROLE = ProfileActivationCalculator.class.getName(); - - private PlexusContainer container; - - public List calculateActiveProfiles( List profiles ) - throws ProjectBuildingException - { - List activators = null; - try - { - activators = container.lookupList( ProfileActivator.ROLE ); - - List active = new ArrayList( profiles.size() ); - - for ( Iterator it = profiles.iterator(); it.hasNext(); ) - { - Profile profile = (Profile) it.next(); - - boolean isActive = true; - - Activation activation = profile.getActivation(); - - activatorLoop: - for ( Iterator activatorIterator = activators.iterator(); activatorIterator.hasNext(); ) - { - ProfileActivator activator = (ProfileActivator) activatorIterator.next(); - - if ( activator.canDetermineActivation( profile ) ) - { - if ( activator.isActive( profile ) ) - { - active.add( profile ); - } - else - { - break activatorLoop; - } - } - } - } - - return active; - } - catch ( ComponentLookupException e ) - { - throw new ProjectBuildingException( "Cannot retrieve list of profile activators.", e ); - } - finally - { - try - { - container.releaseAll( activators ); - } - catch ( ComponentLifecycleException e ) - { - getLogger().debug( "Error releasing profile activators - ignoring.", e ); - } - } - } - - public void contextualize( Context context ) - throws ContextException - { - this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); - } - -} diff --git a/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationException.java b/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationException.java new file mode 100644 index 0000000000..b8755e65f0 --- /dev/null +++ b/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationException.java @@ -0,0 +1,17 @@ +package org.apache.maven.profiles.activation; + +public class ProfileActivationException + extends Exception +{ + + public ProfileActivationException( String message, Throwable cause ) + { + super( message, cause ); + } + + public ProfileActivationException( String message ) + { + super( message ); + } + +} diff --git a/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationUtils.java b/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationUtils.java deleted file mode 100644 index 58c39f0b2d..0000000000 --- a/maven-project/src/main/java/org/apache/maven/profiles/activation/ProfileActivationUtils.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.apache.maven.profiles.activation; - -import org.codehaus.plexus.util.StringUtils; - -import java.util.ArrayList; -import java.util.List; -import java.util.StringTokenizer; - -/* - * 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 final class ProfileActivationUtils -{ - public static final String ACTIVE_PROFILE_IDS = "org.apache.maven.ActiveProfiles"; - - private static List profileList; - - private ProfileActivationUtils() - { - } - - public static boolean profilesWereExplicitlyGiven() - { - return StringUtils.isNotEmpty( System.getProperty( ACTIVE_PROFILE_IDS ) ); - } - - public static List getExplicitProfileList() - { - if ( !profilesWereExplicitlyGiven() ) - { - return null; - } - - if ( profileList == null ) - { - profileList = new ArrayList(); - - StringTokenizer profileTokens = new StringTokenizer( System.getProperty( ACTIVE_PROFILE_IDS ), "," ); - - while ( profileTokens.hasMoreTokens() ) - { - String token = profileTokens.nextToken().trim(); - - if ( StringUtils.isNotEmpty( token ) ) - { - profileList.add( token ); - } - } - } - - return profileList; - } - -} diff --git a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 2a310a1166..46c2623d38 100644 --- a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -40,7 +40,12 @@ import org.apache.maven.model.Profile; import org.apache.maven.model.ReportPlugin; import org.apache.maven.model.Repository; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.profiles.activation.ProfileActivationCalculator; +import org.apache.maven.profiles.DefaultProfileManager; +import org.apache.maven.profiles.MavenProfilesBuilder; +import org.apache.maven.profiles.ProfileManager; +import org.apache.maven.profiles.ProfilesConversionUtils; +import org.apache.maven.profiles.ProfilesRoot; +import org.apache.maven.profiles.activation.ProfileActivationException; import org.apache.maven.project.inheritance.ModelInheritanceAssembler; import org.apache.maven.project.injection.ModelDefaultsInjector; import org.apache.maven.project.injection.ProfileInjector; @@ -90,6 +95,8 @@ public class DefaultMavenProjectBuilder // TODO: remove private PlexusContainer container; + protected MavenProfilesBuilder profilesBuilder; + protected ArtifactResolver artifactResolver; protected ArtifactMetadataSource artifactMetadataSource; @@ -113,8 +120,6 @@ public class DefaultMavenProjectBuilder private ArtifactRepositoryFactory artifactRepositoryFactory; - private ProfileActivationCalculator profileActivationCalculator; - private final Map modelCache = new HashMap(); public static final String MAVEN_MODEL_VERSION = "4.0.0"; @@ -133,11 +138,10 @@ public class DefaultMavenProjectBuilder /** * @todo move to metadatasource itself? */ - public MavenProject buildWithDependencies( File projectDescriptor, ArtifactRepository localRepository, - List externalProfiles ) + public MavenProject buildWithDependencies( File projectDescriptor, ArtifactRepository localRepository, ProfileManager profileManager ) throws ProjectBuildingException, ArtifactResolutionException { - MavenProject project = buildFromSourceFile( projectDescriptor, localRepository, externalProfiles ); + MavenProject project = buildFromSourceFile( projectDescriptor, localRepository, profileManager ); // ---------------------------------------------------------------------- // Typically when the project builder is being used from maven proper @@ -224,14 +228,13 @@ public class DefaultMavenProjectBuilder return map; } - public MavenProject build( File projectDescriptor, ArtifactRepository localRepository, List externalProfiles ) + public MavenProject build( File projectDescriptor, ArtifactRepository localRepository, ProfileManager profileManager ) throws ProjectBuildingException { - return buildFromSourceFile( projectDescriptor, localRepository, externalProfiles ); + return buildFromSourceFile( projectDescriptor, localRepository, profileManager ); } - private MavenProject buildFromSourceFile( File projectDescriptor, ArtifactRepository localRepository, - List externalProfiles ) + private MavenProject buildFromSourceFile( File projectDescriptor, ArtifactRepository localRepository, ProfileManager profileManager ) throws ProjectBuildingException { Model model = readModel( projectDescriptor ); @@ -240,8 +243,7 @@ public class DefaultMavenProjectBuilder modelCache.put( createCacheKey( model.getGroupId(), model.getArtifactId(), model.getVersion() ), model ); MavenProject project = build( projectDescriptor.getAbsolutePath(), model, localRepository, - Collections.EMPTY_LIST, externalProfiles, - projectDescriptor.getAbsoluteFile().getParentFile() ); + Collections.EMPTY_LIST, projectDescriptor.getAbsoluteFile().getParentFile(), profileManager ); if ( project.getDistributionManagement() != null && project.getDistributionManagement().getStatus() != null ) { @@ -277,7 +279,7 @@ public class DefaultMavenProjectBuilder Model model = findModelFromRepository( artifact, remoteArtifactRepositories, localRepository ); return build( "Artifact [" + artifact.getId() + "]", model, localRepository, remoteArtifactRepositories, - Collections.EMPTY_LIST, null ); + null, null ); } private Model findModelFromRepository( Artifact artifact, List remoteArtifactRepositories, @@ -387,7 +389,7 @@ public class DefaultMavenProjectBuilder } private MavenProject build( String pomLocation, Model model, ArtifactRepository localRepository, - List parentSearchRepositories, List externalProfiles, File projectDir ) + List parentSearchRepositories, File projectDir, ProfileManager profileManager ) throws ProjectBuildingException { Model superModel = getSuperModel(); @@ -401,7 +403,24 @@ public class DefaultMavenProjectBuilder artifactRepositoryFactory, container ) ); - for ( Iterator i = externalProfiles.iterator(); i.hasNext(); ) + List activeExternalProfiles; + try + { + if ( profileManager != null ) + { + activeExternalProfiles = profileManager.getActiveProfiles(); + } + else + { + activeExternalProfiles = Collections.EMPTY_LIST; + } + } + catch ( ProfileActivationException e ) + { + throw new ProjectBuildingException( "Failed to calculate active external profiles.", e ); + } + + for ( Iterator i = activeExternalProfiles.iterator(); i.hasNext(); ) { Profile externalProfile = (Profile) i.next(); @@ -419,8 +438,8 @@ public class DefaultMavenProjectBuilder Model originalModel = ModelUtils.cloneModel( model ); - MavenProject project = assembleLineage( model, lineage, localRepository, externalProfiles, projectDir, - parentSearchRepositories, aggregatedRemoteWagonRepositories ); + MavenProject project = assembleLineage( model, lineage, localRepository, projectDir, parentSearchRepositories, + aggregatedRemoteWagonRepositories ); project.setOriginalModel( originalModel ); @@ -440,8 +459,7 @@ public class DefaultMavenProjectBuilder try { - project = processProjectLogic( pomLocation, project, new ArrayList( aggregatedRemoteWagonRepositories ), - externalProfiles ); + project = processProjectLogic( pomLocation, project, new ArrayList( aggregatedRemoteWagonRepositories ), profileManager ); } catch ( ModelInterpolationException e ) { @@ -460,8 +478,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, - List externalProfiles ) + private MavenProject processProjectLogic( String pomLocation, MavenProject project, List remoteRepositories, ProfileManager profileMgr ) throws ProjectBuildingException, ModelInterpolationException { Model model = project.getModel(); @@ -471,26 +488,26 @@ public class DefaultMavenProjectBuilder { modelCache.put( key, model ); } - - List activeProfiles = new ArrayList( externalProfiles ); - - List activePomProfiles = profileActivationCalculator.calculateActiveProfiles( model.getProfiles() ); - - activeProfiles.addAll( activePomProfiles ); - - Properties profileProperties = new Properties(); - - for ( Iterator it = activeProfiles.iterator(); it.hasNext(); ) + + Properties profileProperties = project.getProfileProperties(); + + if ( profileProperties == null ) { - Profile profile = (Profile) it.next(); - - profileInjector.inject( profile, model ); - - profileProperties.putAll( profile.getProperties() ); + profileProperties = new Properties(); } + + List activeProfiles = project.getActiveProfiles(); + + if ( activeProfiles == null ) + { + activeProfiles = new ArrayList(); + } + + List injectedProfiles = injectActiveProfiles( profileMgr, model, profileProperties ); + + activeProfiles.addAll( injectedProfiles ); // TODO: Clean this up...we're using this to 'jump' the interpolation step for model properties not expressed in XML. - model = modelInterpolator.interpolate( model ); // interpolation is before injection, because interpolation is off-limits in the injected variables @@ -507,13 +524,14 @@ public class DefaultMavenProjectBuilder //======================================================================= project = new MavenProject( model ); - project.addProfileProperties( profileProperties ); - - project.setActiveProfiles( activeProfiles ); - project.setOriginalModel( originalModel ); - + project.setActiveProfiles( activeProfiles ); + + project.addProfileProperties( profileProperties ); + + project.assembleProfilePropertiesInheritance(); + // TODO: maybe not strictly correct, while we should enfore that packaging has a type handler of the same id, we don't Artifact projectArtifact = artifactFactory.createBuildArtifact( project.getGroupId(), project.getArtifactId(), project.getVersion(), project.getPackaging() ); @@ -569,7 +587,7 @@ public class DefaultMavenProjectBuilder * @noinspection CollectionDeclaredAsConcreteClass */ private MavenProject assembleLineage( Model model, LinkedList lineage, ArtifactRepository localRepository, - List externalProfiles, File projectDir, List parentSearchRepositories, + File projectDir, List parentSearchRepositories, Set aggregatedRemoteWagonRepositories ) throws ProjectBuildingException { @@ -589,8 +607,31 @@ public class DefaultMavenProjectBuilder } } + ProfileManager profileManager = new DefaultProfileManager( container ); + + List activeProfiles; + + Properties profileProperties = new Properties(); + + try + { + profileManager.addProfiles( model.getProfiles() ); + + loadProjectExternalProfiles( profileManager, projectDir ); + + activeProfiles = injectActiveProfiles( profileManager, model, profileProperties ); + } + catch ( ProfileActivationException e ) + { + throw new ProjectBuildingException( "Failed to activate local (project-level) build profiles.", e ); + } + MavenProject project = new MavenProject( model ); + project.addProfileProperties( profileProperties ); + + project.setActiveProfiles( activeProfiles ); + lineage.addFirst( project ); Parent parentModel = model.getParent(); @@ -686,8 +727,8 @@ public class DefaultMavenProjectBuilder model = findModelFromRepository( parentArtifact, remoteRepositories, localRepository ); } - MavenProject parent = assembleLineage( model, lineage, localRepository, externalProfiles, parentProjectDir, - parentSearchRepositories, aggregatedRemoteWagonRepositories ); + MavenProject parent = assembleLineage( model, lineage, localRepository, parentProjectDir, parentSearchRepositories, + aggregatedRemoteWagonRepositories ); project.setParent( parent ); @@ -697,6 +738,69 @@ public class DefaultMavenProjectBuilder return project; } + private List injectActiveProfiles( ProfileManager profileManager, Model model, Properties profileProperties ) + throws ProjectBuildingException + { + List activeProfiles; + + if ( profileManager != null ) + { + try + { + activeProfiles = profileManager.getActiveProfiles(); + } + catch ( ProfileActivationException e ) + { + throw new ProjectBuildingException( "Failed to calculate active build profiles.", e ); + } + + for ( Iterator it = activeProfiles.iterator(); it.hasNext(); ) + { + Profile profile = (Profile) it.next(); + + profileInjector.inject( profile, model ); + + profileProperties.putAll( profile.getProperties() ); + } + } + else + { + activeProfiles = Collections.EMPTY_LIST; + } + + return activeProfiles; + } + + private void loadProjectExternalProfiles( ProfileManager profileManager, File projectDir ) + throws ProfileActivationException + { + if ( projectDir != null ) + { + try + { + ProfilesRoot root = profilesBuilder.buildProfiles( projectDir ); + + if ( root != null ) + { + for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); ) + { + org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next(); + + profileManager.addProfile( ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile ) ); + } + } + } + catch ( IOException e ) + { + throw new ProfileActivationException( "Cannot read profiles.xml resource from directory: " + projectDir, e ); + } + catch ( XmlPullParserException e ) + { + throw new ProfileActivationException( "Cannot parse profiles.xml resource from directory: " + projectDir, e ); + } + } + } + private Model readModel( File file ) throws ProjectBuildingException { @@ -885,7 +989,7 @@ public class DefaultMavenProjectBuilder return extensionArtifacts; } - public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository, List externalProfiles ) + public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository ) throws ProjectBuildingException { Model superModel = getSuperModel(); @@ -907,7 +1011,7 @@ public class DefaultMavenProjectBuilder List remoteRepositories = ProjectUtils.buildArtifactRepositories( superModel.getRepositories(), artifactRepositoryFactory, container ); - project = processProjectLogic( "", project, remoteRepositories, externalProfiles ); + project = processProjectLogic( "", project, remoteRepositories, null ); return project; } diff --git a/maven-project/src/main/java/org/apache/maven/project/MavenProject.java b/maven-project/src/main/java/org/apache/maven/project/MavenProject.java index dd6670e7f8..223ae964d6 100644 --- a/maven-project/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-project/src/main/java/org/apache/maven/project/MavenProject.java @@ -61,6 +61,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; +import java.util.Stack; /** * The concern of the project is provide runtime values based on the model.

@@ -1309,5 +1310,32 @@ public class MavenProject { return groupId + ":" + artifactId; } + + public void assembleProfilePropertiesInheritance() + { + Stack propertyStack = new Stack(); + + MavenProject current = this; + while( current != null ) + { + Properties toAdd = current.profileProperties; + + if ( toAdd != null && !toAdd.isEmpty() ) + { + propertyStack.push( toAdd ); + } + + current = current.getParent(); + } + + Properties newProfilesProperties = new Properties(); + + while( !propertyStack.isEmpty() ) + { + newProfilesProperties.putAll( (Properties) propertyStack.pop() ); + } + + this.profileProperties = newProfilesProperties; + } } diff --git a/maven-project/src/main/java/org/apache/maven/project/MavenProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/MavenProjectBuilder.java index 2d8fbc27aa..f6f43374c4 100644 --- a/maven-project/src/main/java/org/apache/maven/project/MavenProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/MavenProjectBuilder.java @@ -19,6 +19,7 @@ package org.apache.maven.project; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.profiles.ProfileManager; import java.io.File; import java.util.List; @@ -33,10 +34,10 @@ public interface MavenProjectBuilder String STANDALONE_SUPERPOM_VERSION = "2.0"; - MavenProject build( File project, ArtifactRepository localRepository, List profiles ) + MavenProject build( File project, ArtifactRepository localRepository, ProfileManager globalProfileManager ) throws ProjectBuildingException; - MavenProject buildWithDependencies( File project, ArtifactRepository localRepository, List externalProfiles ) + MavenProject buildWithDependencies( File project, ArtifactRepository localRepository, ProfileManager globalProfileManager ) throws ProjectBuildingException, ArtifactResolutionException; /** @@ -52,6 +53,6 @@ public interface MavenProjectBuilder ArtifactRepository localRepository ) throws ProjectBuildingException; - MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository, List externalProfiles ) + MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository ) throws ProjectBuildingException; } diff --git a/maven-project/src/main/resources/META-INF/plexus/components.xml b/maven-project/src/main/resources/META-INF/plexus/components.xml index 077274bed1..30ceb99b03 100644 --- a/maven-project/src/main/resources/META-INF/plexus/components.xml +++ b/maven-project/src/main/resources/META-INF/plexus/components.xml @@ -36,6 +36,9 @@ org.apache.maven.project.MavenProjectBuilder org.apache.maven.project.DefaultMavenProjectBuilder + + org.apache.maven.profiles.MavenProfilesBuilder + org.apache.maven.project.injection.ProfileInjector @@ -63,20 +66,8 @@ org.apache.maven.artifact.repository.ArtifactRepositoryFactory - - org.apache.maven.profiles.activation.ProfileActivationCalculator - - - - org.apache.maven.profiles.activation.ProfileActivationCalculator - org.apache.maven.profiles.activation.ProfileActivationCalculator - - - org.apache.maven.profiles.activation.ProfileActivator - explicit-listing - org.apache.maven.profiles.activation.ExplicitListingProfileActivator - diff --git a/maven-project/src/test/java/org/apache/maven/project/MavenProjectTestCase.java b/maven-project/src/test/java/org/apache/maven/project/MavenProjectTestCase.java index e5bdb69ccd..566c559cd4 100644 --- a/maven-project/src/test/java/org/apache/maven/project/MavenProjectTestCase.java +++ b/maven-project/src/test/java/org/apache/maven/project/MavenProjectTestCase.java @@ -20,12 +20,10 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.DefaultArtifactRepository; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import java.io.File; import java.io.FileNotFoundException; import java.net.URL; -import java.util.Collections; /** * @author Jason van Zyl @@ -104,13 +102,13 @@ public abstract class MavenProjectTestCase protected MavenProject getProjectWithDependencies( File pom ) throws Exception { - return projectBuilder.buildWithDependencies( pom, getLocalRepository(), Collections.EMPTY_LIST ); + return projectBuilder.buildWithDependencies( pom, getLocalRepository(), null ); } protected MavenProject getProject( File pom ) throws Exception { - return projectBuilder.build( pom, getLocalRepository(), Collections.EMPTY_LIST ); + return projectBuilder.build( pom, getLocalRepository(), null ); } } diff --git a/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml b/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml index a301024f9c..7b83dbfcd8 100644 --- a/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml +++ b/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml @@ -37,6 +37,9 @@ test org.apache.maven.project.TestProjectBuilder + + org.apache.maven.profiles.MavenProfilesBuilder + org.apache.maven.project.injection.ModelDefaultsInjector @@ -61,9 +64,6 @@ org.apache.maven.artifact.repository.ArtifactRepositoryFactory - - org.apache.maven.profiles.activation.ProfileActivationCalculator -