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 index 1cfb79104b..512a1f739a 100644 --- a/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java +++ b/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java @@ -30,6 +30,7 @@ import org.apache.maven.profiles.matchers.DefaultMatcher; import org.apache.maven.profiles.matchers.ProfileMatcher; import org.apache.maven.profiles.matchers.PropertyMatcher; import org.apache.maven.shared.model.InterpolatorProperty; +import org.apache.maven.project.ProjectBuilderConfiguration; import org.apache.maven.project.builder.PomInterpolatorTag; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.MutablePlexusContainer; @@ -45,6 +46,11 @@ public class DefaultProfileManager private Map profilesById = new LinkedHashMap(); private ProfileActivationContext profileActivationContext; + + private static final ProfileMatcher defaultMatcher = new DefaultMatcher(); + + private static final List matchers = + Collections.unmodifiableList( Arrays.asList( new DefaultMatcher(), new PropertyMatcher() ) ); /** * the properties passed to the profile manager are the props that @@ -195,6 +201,83 @@ public class DefaultProfileManager return allActive; } + public static List getActiveProfilesFrom(ProjectBuilderConfiguration config, Model model, PlexusContainer container) + throws ProfileActivationException + { + List projectProfiles = new ArrayList(); + ProfileManager externalProfileManager = config.getGlobalProfileManager(); + + ProfileActivationContext profileActivationContext = (externalProfileManager == null) ? new ProfileActivationContext( config.getExecutionProperties(), false ): + externalProfileManager.getProfileActivationContext(); + + if(externalProfileManager != null) + { + projectProfiles.addAll( externalProfileManager.getActiveProfiles() ); + } + + ProfileManager profileManager = new DefaultProfileManager( container, profileActivationContext ); + profileManager.addProfiles( model.getProfiles() ); + projectProfiles.addAll( profileManager.getActiveProfiles() ); + return projectProfiles; + } + + public static Collection getActiveProfiles(List profiles, ProfileManagerInfo profileContextInfo) + { + List properties = profileContextInfo.getInterpolatorProperties(); + Collection activeProfileIds = profileContextInfo.getActiveProfileIds(); + Collection inactiveProfileIds = profileContextInfo.getInactiveProfileIds(); + + List matchedProfiles = new ArrayList(); + List defaultProfiles = new ArrayList(); + for ( Profile profile : profiles ) + { + String profileId = profile.getId(); + + if ( !inactiveProfileIds.contains( profileId ) ) + { + if ( activeProfileIds.contains( profileId ) ) + { + matchedProfiles.add( profile ); + } + else if ( defaultMatcher.isMatch( profile, properties ) ) + { + defaultProfiles.add( profile ); + } + else + { + for ( ProfileMatcher matcher : matchers ) + { + if ( matcher.isMatch( profile, properties ) ) + { + matchedProfiles.add( profile ); + break; + } + } + } + } + } + + if ( matchedProfiles.isEmpty() ) + { + matchedProfiles = defaultProfiles; + } + + return matchedProfiles; + } + + /* (non-Javadoc) + * @see org.apache.maven.project.ProfileManager#addProfiles(java.util.List) + */ + public void addProfiles( List profiles ) + { + for ( Iterator it = profiles.iterator(); it.hasNext(); ) + { + Profile profile = (Profile) it.next(); + + addProfile( profile ); + } + } + private static List getDefaultProfiles(List profiles) { List defaults = new ArrayList(); @@ -208,9 +291,6 @@ public class DefaultProfileManager return defaults; } - private static List matchers = Arrays.asList( (ProfileMatcher) new DefaultMatcher(), - (ProfileMatcher) new PropertyMatcher()); - private boolean isActive( Profile profile, ProfileActivationContext context ) throws ProfileActivationException { @@ -232,19 +312,6 @@ public class DefaultProfileManager return false; } - /* (non-Javadoc) - * @see org.apache.maven.project.ProfileManager#addProfiles(java.util.List) - */ - public void addProfiles( List profiles ) - { - for ( Iterator it = profiles.iterator(); it.hasNext(); ) - { - Profile profile = (Profile) it.next(); - - addProfile( profile ); - } - } - private void activateAsDefault( String profileId ) { List defaultIds = profileActivationContext.getActiveByDefaultProfileIds(); diff --git a/maven-project/src/main/java/org/apache/maven/profiles/ProfileContext.java b/maven-project/src/main/java/org/apache/maven/profiles/ProfileContext.java deleted file mode 100644 index 9527792035..0000000000 --- a/maven-project/src/main/java/org/apache/maven/profiles/ProfileContext.java +++ /dev/null @@ -1,122 +0,0 @@ -package org.apache.maven.profiles; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -import org.apache.maven.profiles.matchers.DefaultMatcher; -import org.apache.maven.profiles.matchers.ProfileMatcher; -import org.apache.maven.profiles.matchers.PropertyMatcher; -import org.apache.maven.project.ProjectBuilderConfiguration; -import org.apache.maven.project.ProjectBuildingException; -import org.apache.maven.shared.model.InterpolatorProperty; - -import org.apache.maven.model.Model; -import org.apache.maven.model.Profile; -import org.codehaus.plexus.PlexusContainer; - -public class ProfileContext -{ - private List properties; - - private Collection activeProfileIds; - - private Collection inactiveProfileIds; - - private List profiles; - - private ProfileMatcher defaultMatcher = new DefaultMatcher(); - - private List matchers = - Collections.unmodifiableList( Arrays.asList( new DefaultMatcher(), new PropertyMatcher() ) ); - - public ProfileContext( List profiles, ProfileContextInfo profileContextInfo ) - { - this.profiles = new ArrayList( profiles ); - this.properties = profileContextInfo.getInterpolatorProperties(); - this.activeProfileIds = profileContextInfo.getActiveProfileIds(); - this.inactiveProfileIds = profileContextInfo.getInactiveProfileIds(); - } - - - public static List getActiveProfilesFrom(ProjectBuilderConfiguration config, Model model, PlexusContainer container) - throws ProfileActivationException - { - List projectProfiles = new ArrayList(); - ProfileManager externalProfileManager = config.getGlobalProfileManager(); - - ProfileActivationContext profileActivationContext = (externalProfileManager == null) ? new ProfileActivationContext( config.getExecutionProperties(), false ): - externalProfileManager.getProfileActivationContext(); - - if(externalProfileManager != null) - { - projectProfiles.addAll( externalProfileManager.getActiveProfiles() ); - } - - ProfileManager profileManager = new DefaultProfileManager( container, profileActivationContext ); - profileManager.addProfiles( model.getProfiles() ); - projectProfiles.addAll( profileManager.getActiveProfiles() ); - return projectProfiles; - } - - public Collection getActiveProfiles() - { - List matchedProfiles = new ArrayList(); - List defaultProfiles = new ArrayList(); - for ( Profile profile : profiles ) - { - String profileId = profile.getId(); - - if ( !inactiveProfileIds.contains( profileId ) ) - { - if ( activeProfileIds.contains( profileId ) ) - { - matchedProfiles.add( profile ); - } - else if ( defaultMatcher.isMatch( profile, properties ) ) - { - defaultProfiles.add( profile ); - } - else - { - for ( ProfileMatcher matcher : matchers ) - { - if ( matcher.isMatch( profile, properties ) ) - { - matchedProfiles.add( profile ); - break; - } - } - } - } - } - - if ( matchedProfiles.isEmpty() ) - { - matchedProfiles = defaultProfiles; - } - - return matchedProfiles; - } -} 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 index 6ed8d4cf44..de5d42a0ba 100644 --- a/maven-project/src/main/java/org/apache/maven/profiles/ProfileManager.java +++ b/maven-project/src/main/java/org/apache/maven/profiles/ProfileManager.java @@ -40,5 +40,5 @@ public interface ProfileManager throws ProfileActivationException; List getActiveProfiles( ) - throws ProfileActivationException; + throws ProfileActivationException; } \ No newline at end of file diff --git a/maven-project/src/main/java/org/apache/maven/profiles/ProfileContextInfo.java b/maven-project/src/main/java/org/apache/maven/profiles/ProfileManagerInfo.java similarity index 91% rename from maven-project/src/main/java/org/apache/maven/profiles/ProfileContextInfo.java rename to maven-project/src/main/java/org/apache/maven/profiles/ProfileManagerInfo.java index 47906b9ca9..05eb403d09 100644 --- a/maven-project/src/main/java/org/apache/maven/profiles/ProfileContextInfo.java +++ b/maven-project/src/main/java/org/apache/maven/profiles/ProfileManagerInfo.java @@ -6,7 +6,7 @@ import java.util.List; import org.apache.maven.shared.model.InterpolatorProperty; -public class ProfileContextInfo +public class ProfileManagerInfo { private List interpolatorProperties; @@ -14,7 +14,7 @@ public class ProfileContextInfo private Collection inactiveProfileIds; - public ProfileContextInfo(List interpolatorProperties, Collection activeProfileIds, Collection inactiveProfileIds) + public ProfileManagerInfo(List interpolatorProperties, Collection activeProfileIds, Collection inactiveProfileIds) { this.interpolatorProperties = (interpolatorProperties != null) ? interpolatorProperties : new ArrayList(); this.activeProfileIds = (activeProfileIds != null) ? activeProfileIds : new ArrayList(); 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 1d17e0be77..afa97dccbb 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 @@ -45,12 +45,11 @@ import org.apache.maven.model.io.xpp3.MavenXpp3Writer; import org.apache.maven.profiles.DefaultProfileManager; import org.apache.maven.profiles.ProfileActivationContext; import org.apache.maven.profiles.ProfileActivationException; -import org.apache.maven.profiles.ProfileContextInfo; +import org.apache.maven.profiles.ProfileManagerInfo; import org.apache.maven.profiles.ProfileManager; import org.apache.maven.project.artifact.InvalidDependencyVersionException; import org.apache.maven.project.builder.PomClassicDomainModel; import org.apache.maven.project.builder.PomInterpolatorTag; -import org.apache.maven.profiles.ProfileContext; import org.apache.maven.project.processor.ProcessorContext; import org.apache.maven.project.validation.ModelValidationResult; import org.apache.maven.project.validation.ModelValidator; @@ -127,7 +126,7 @@ public class DefaultMavenProjectBuilder PomClassicDomainModel domainModel; try { - domainModel = buildWithoutProfiles( "unknown", pomFile, configuration ); + domainModel = build( "unknown", pomFile, configuration ); } catch (IOException e) { @@ -138,7 +137,7 @@ public class DefaultMavenProjectBuilder List projectProfiles; try { - projectProfiles = ProfileContext.getActiveProfilesFrom(configuration, domainModel.getModel(), container); + projectProfiles = DefaultProfileManager.getActiveProfilesFrom(configuration, domainModel.getModel(), container); } catch ( ProfileActivationException e ) { @@ -220,7 +219,7 @@ public class DefaultMavenProjectBuilder PomClassicDomainModel domainModel; try { - domainModel = buildWithoutProfiles( "unknown", artifact.getFile(), configuration ); + domainModel = build( "unknown", artifact.getFile(), configuration ); } catch (IOException e) { @@ -230,7 +229,7 @@ public class DefaultMavenProjectBuilder List projectProfiles; try { - projectProfiles = ProfileContext.getActiveProfilesFrom(configuration, domainModel.getModel(), container); + projectProfiles = DefaultProfileManager.getActiveProfilesFrom(configuration, domainModel.getModel(), container); } catch ( ProfileActivationException e ) { @@ -414,7 +413,7 @@ public class DefaultMavenProjectBuilder return project; } - private PomClassicDomainModel buildWithoutProfiles( String projectId, File pomFile, ProjectBuilderConfiguration projectBuilderConfiguration ) + private PomClassicDomainModel build( String projectId, File pomFile, ProjectBuilderConfiguration projectBuilderConfiguration ) throws ProjectBuildingException, IOException { List activeProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration.getGlobalProfileManager() @@ -424,7 +423,7 @@ public class DefaultMavenProjectBuilder .getGlobalProfileManager().getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyInactiveProfileIds() : new ArrayList(); - ProfileContextInfo profileInfo = new ProfileContextInfo(null, activeProfileIds, inactiveProfileIds); + ProfileManagerInfo profileInfo = new ProfileManagerInfo(null, activeProfileIds, inactiveProfileIds); PomClassicDomainModel domainModel = new PomClassicDomainModel( pomFile ); domainModel.setProjectDirectory( pomFile.getParentFile() ); domainModel.setMostSpecialized( true ); @@ -469,8 +468,7 @@ public class DefaultMavenProjectBuilder if(!dm.getModel().getProfiles().isEmpty()) { - ProfileContext profileContext1 = new ProfileContext( dm.getModel().getProfiles(), profileInfo ); - Collection profiles = profileContext1.getActiveProfiles(); + Collection profiles = DefaultProfileManager.getActiveProfiles(dm.getModel().getProfiles(), profileInfo); if(!profiles.isEmpty()) { profileModels.add(ProcessorContext.mergeProfilesIntoModel( profiles, dm ));