From 38e858685277d79a363e974159f5b33f7e927a99 Mon Sep 17 00:00:00 2001 From: Britton Isbell Date: Thu, 2 Apr 2009 00:30:00 +0000 Subject: [PATCH] Collapsed methods, moved logic for active profiles from ProjectBuilder to ProfileContext. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@761121 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/maven/profiles/ProfileContext.java | 25 ++- .../project/DefaultMavenProjectBuilder.java | 186 +++++++----------- 2 files changed, 99 insertions(+), 112 deletions(-) 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 index 281a2b38f9..9527792035 100644 --- a/maven-project/src/main/java/org/apache/maven/profiles/ProfileContext.java +++ b/maven-project/src/main/java/org/apache/maven/profiles/ProfileContext.java @@ -28,9 +28,13 @@ 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 { @@ -55,7 +59,26 @@ public class ProfileContext this.inactiveProfileIds = profileContextInfo.getInactiveProfileIds(); } - // public Collection getActiveProfilesFrom(ProfileManager manaa) + + 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() { 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 31775d9559..e38d1834b9 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 @@ -306,34 +306,10 @@ public class DefaultMavenProjectBuilder String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); - List projectProfiles = new ArrayList(); - ProfileManager externalProfileManager = config.getGlobalProfileManager(); - - ProfileActivationContext profileActivationContext = (externalProfileManager == null) ? new ProfileActivationContext( config.getExecutionProperties(), false ): - externalProfileManager.getProfileActivationContext(); - - if(externalProfileManager != null) - { - //System.out.println("PROFILES = " + externalProfileManager.getProfilesById().toString()); - - try - { - projectProfiles.addAll( externalProfileManager.getActiveProfiles() ); - } - catch ( ProfileActivationException e ) - { - throw new ProjectBuildingException( projectId, "Failed to activate external profiles.", projectDescriptor, - e ); - } - } - - ProfileManager profileManager = new DefaultProfileManager( container, profileActivationContext ); - profileManager.addProfiles( model.getProfiles() ); - //System.out.println("PROFILE POM: COUNT = " + model.getProfiles().size()); + List projectProfiles; try { - //System.out.println("PROFILE POM - ACTIVE: COUNT = " + profileManager.getActiveProfiles( model ).size() +"," + projectProfiles.size()); - projectProfiles.addAll( profileManager.getActiveProfiles() ); + projectProfiles = ProfileContext.getActiveProfilesFrom(config, model, container); } catch ( ProfileActivationException e ) { @@ -388,19 +364,85 @@ public class DefaultMavenProjectBuilder return project; } - private PomClassicDomainModel buildWithoutProfiles( String projectId, File pomFile, ProjectBuilderConfiguration projectBuilderConfiguration ) throws ProjectBuildingException, IOException - { - + { List activeProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration.getGlobalProfileManager() .getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyActiveProfileIds() : new ArrayList(); List inactiveProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration .getGlobalProfileManager().getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyInactiveProfileIds() : new ArrayList(); - - return buildModel( pomFile, new ProfileContextInfo(null, activeProfileIds, inactiveProfileIds), projectBuilderConfiguration ); + + ProfileContextInfo profileInfo = new ProfileContextInfo(null, activeProfileIds, inactiveProfileIds); + PomClassicDomainModel domainModel = new PomClassicDomainModel( pomFile ); + domainModel.setProjectDirectory( pomFile.getParentFile() ); + domainModel.setMostSpecialized( true ); + + List domainModels = new ArrayList(); + + domainModels.add( domainModel ); + ArtifactRepository localRepository = projectBuilderConfiguration.getLocalRepository(); + List remoteRepositories = projectBuilderConfiguration.getRemoteRepositories(); + + File parentFile = null; + int lineageCount = 0; + if ( domainModel.getParentId() != null ) + { + List mavenParents; + if ( isParentLocal( domainModel.getRelativePathOfParent(), pomFile.getParentFile() ) ) + { + mavenParents = getDomainModelParentsFromLocalPath( domainModel, localRepository, remoteRepositories, pomFile.getParentFile() ); + } + else + { + mavenParents = getDomainModelParentsFromRepository( domainModel, localRepository, remoteRepositories ); + } + + if ( mavenParents.size() > 0 ) + { + PomClassicDomainModel dm = (PomClassicDomainModel) mavenParents.get( 0 ); + parentFile = dm.getFile(); + domainModel.setParentFile( parentFile ); + lineageCount = mavenParents.size(); + } + + domainModels.addAll( mavenParents ); + } + + domainModels.add( convertToDomainModel( getSuperModel(), false ) ); + List profileModels = new ArrayList(); + //Process Profiles + for(DomainModel domain : domainModels) + { + PomClassicDomainModel dm = (PomClassicDomainModel) domain; + + if(!dm.getModel().getProfiles().isEmpty()) + { + ProfileContext profileContext1 = new ProfileContext( dm.getModel().getProfiles(), profileInfo ); + Collection profiles = profileContext1.getActiveProfiles(); + if(!profiles.isEmpty()) + { + profileModels.add(ProcessorContext.mergeProfilesIntoModel( profileContext1.getActiveProfiles(), dm )); + } + else + { + profileModels.add( dm ); + } + } + else + { + profileModels.add( dm ); + } + } + + PomClassicDomainModel transformedDomainModel = ProcessorContext.build( profileModels, null ); + + // Lineage count is inclusive to add the POM read in itself. + transformedDomainModel.setLineageCount( lineageCount + 1 ); + transformedDomainModel.setParentFile( parentFile ); + + return transformedDomainModel; } private void validateModel( Model model, File pomFile ) @@ -450,85 +492,7 @@ public class DefaultMavenProjectBuilder } } - private PomClassicDomainModel buildModel( File pom, ProfileContextInfo profileInfo, ProjectBuilderConfiguration config ) - throws IOException - { - if ( pom == null ) - { - throw new IllegalArgumentException( "pom: null" ); - } - - PomClassicDomainModel domainModel = new PomClassicDomainModel( pom ); - domainModel.setProjectDirectory( pom.getParentFile() ); - domainModel.setMostSpecialized( true ); - - List domainModels = new ArrayList(); - - domainModels.add( domainModel ); - ArtifactRepository localRepository = config.getLocalRepository(); - List remoteRepositories = config.getRemoteRepositories(); - - File parentFile = null; - int lineageCount = 0; - if ( domainModel.getParentId() != null ) - { - List mavenParents; - if ( isParentLocal( domainModel.getRelativePathOfParent(), pom.getParentFile() ) ) - { - mavenParents = getDomainModelParentsFromLocalPath( domainModel, localRepository, remoteRepositories, pom.getParentFile() ); - } - else - { - mavenParents = getDomainModelParentsFromRepository( domainModel, localRepository, remoteRepositories ); - } - - if ( mavenParents.size() > 0 ) - { - PomClassicDomainModel dm = (PomClassicDomainModel) mavenParents.get( 0 ); - parentFile = dm.getFile(); - domainModel.setParentFile( parentFile ); - lineageCount = mavenParents.size(); - } - - domainModels.addAll( mavenParents ); - } - - domainModels.add( convertToDomainModel( getSuperModel(), false ) ); - List profileModels = new ArrayList(); - //Process Profiles - for(DomainModel domain : domainModels) - { - PomClassicDomainModel dm = (PomClassicDomainModel) domain; - - if(!dm.getModel().getProfiles().isEmpty()) - { - ProfileContext profileContext1 = new ProfileContext( dm.getModel().getProfiles(), profileInfo ); - Collection profiles = profileContext1.getActiveProfiles(); - if(!profiles.isEmpty()) - { - profileModels.add(ProcessorContext.mergeProfilesIntoModel( profileContext1.getActiveProfiles(), dm )); - } - else - { - profileModels.add( dm ); - } - } - else - { - profileModels.add( dm ); - } - } - - PomClassicDomainModel transformedDomainModel = ProcessorContext.build( profileModels, null ); - - // Lineage count is inclusive to add the POM read in itself. - transformedDomainModel.setLineageCount( lineageCount + 1 ); - transformedDomainModel.setParentFile( parentFile ); - - return transformedDomainModel; - } - - private PomClassicDomainModel convertToDomainModel( Model model, boolean isMostSpecialized ) + private static PomClassicDomainModel convertToDomainModel( Model model, boolean isMostSpecialized ) throws IOException { if ( model == null ) @@ -562,7 +526,7 @@ public class DefaultMavenProjectBuilder * @return true if the relative path of the specified parent references a pom, otherwise returns * fals */ - private boolean isParentLocal( String relativePath, File projectDirectory ) + private static boolean isParentLocal( String relativePath, File projectDirectory ) { try {