From bfe55515a03db70c4c70634d75db7085e2903df1 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Sat, 6 Jun 2009 18:09:42 +0000 Subject: [PATCH] o Updated profile selector to support collecting activation errors git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@782293 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven/model/DefaultModelBuilder.java | 28 +++--- .../maven/model/ModelBuildingException.java | 4 +- .../model/profile/DefaultProfileSelector.java | 28 ++++-- .../profile/ProfileActivationException.java | 1 + .../model/profile/ProfileSelectionResult.java | 99 +++++++++++++++++++ .../maven/model/profile/ProfileSelector.java | 8 +- 6 files changed, 136 insertions(+), 32 deletions(-) create mode 100644 maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileSelectionResult.java diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java index 233a842c1f..d8b6973471 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java @@ -41,6 +41,7 @@ import org.apache.maven.model.profile.DefaultProfileActivationContext; import org.apache.maven.model.profile.ProfileActivationContext; import org.apache.maven.model.profile.ProfileActivationException; import org.apache.maven.model.profile.ProfileInjector; +import org.apache.maven.model.profile.ProfileSelectionResult; import org.apache.maven.model.profile.ProfileSelector; import org.apache.maven.model.resolution.InvalidRepositoryException; import org.apache.maven.model.resolution.ModelResolver; @@ -251,37 +252,30 @@ public class DefaultModelBuilder private List getActiveExternalProfiles( ModelBuildingRequest request, ProfileActivationContext context, List problems ) { - try - { - return profileSelector.getActiveProfiles( request.getProfiles(), context ); - } - catch ( ProfileActivationException e ) + ProfileSelectionResult result = profileSelector.getActiveProfiles( request.getProfiles(), context ); + + for ( ProfileActivationException e : result.getActivationExceptions() ) { problems.add( new ModelProblem( "Invalid activation condition for external profile " + e.getProfile().getId() + ": " + e.getMessage(), "(external profiles)", e ) ); - - // FIXME: Update profile selector to integrate better with the problem reporting - return new ArrayList(); } + + return result.getActiveProfiles(); } private List getActiveProjectProfiles( Model model, ProfileActivationContext context, List problems ) - throws ModelBuildingException { - try - { - return profileSelector.getActiveProfiles( model.getProfiles(), context ); - } - catch ( ProfileActivationException e ) + ProfileSelectionResult result = profileSelector.getActiveProfiles( model.getProfiles(), context ); + + for ( ProfileActivationException e : result.getActivationExceptions() ) { problems.add( new ModelProblem( "Invalid activation condition for project profile " + e.getProfile().getId() + " in POM " + toSourceHint( model ) + ": " + e.getMessage(), toSourceHint( model ), e ) ); - - // FIXME: Update profile selector to integrate better with the problem reporting - return new ArrayList(); } + + return result.getActiveProfiles(); } private void configureResolver( ModelResolver modelResolver, Model model, List problems ) diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingException.java b/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingException.java index 83f712c670..05b4d9a089 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingException.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingException.java @@ -25,7 +25,9 @@ import java.util.ArrayList; import java.util.List; /** - * Signals an error during model building. + * Signals one ore more errors during model building. The model builder tries to collect as many problems as possible + * before eventually failing to provide callers with rich error information. Use {@link #getProblems()} to query the + * details of the failure. * * @author Benjamin Bentmann */ diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java index 0276ce392c..5360381696 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java @@ -43,8 +43,7 @@ public class DefaultProfileSelector @Requirement( role = ProfileActivator.class ) private List activators; - public List getActiveProfiles( Collection profiles, ProfileActivationContext context ) - throws ProfileActivationException + public ProfileSelectionResult getActiveProfiles( Collection profiles, ProfileActivationContext context ) { Collection activatedIds = new HashSet( context.getActiveProfileIds() ); Collection deactivatedIds = new HashSet( context.getInactiveProfileIds() ); @@ -53,11 +52,13 @@ public class DefaultProfileSelector List activePomProfilesByDefault = new ArrayList(); boolean activatedPomProfileNotByDefault = false; + List activationExceptions = new ArrayList(); + for ( Profile profile : profiles ) { if ( !deactivatedIds.contains( profile.getId() ) ) { - if ( activatedIds.contains( profile.getId() ) || isActive( profile, context ) ) + if ( activatedIds.contains( profile.getId() ) || isActive( profile, context, activationExceptions ) ) { activeProfiles.add( profile ); @@ -86,17 +87,28 @@ public class DefaultProfileSelector activeProfiles.addAll( activePomProfilesByDefault ); } - return activeProfiles; + ProfileSelectionResult result = new ProfileSelectionResult(); + result.setActiveProfiles( activeProfiles ); + result.setActivationExceptions( activationExceptions ); + + return result; } - private boolean isActive( Profile profile, ProfileActivationContext context ) - throws ProfileActivationException + private boolean isActive( Profile profile, ProfileActivationContext context, + List exceptions ) { for ( ProfileActivator activator : activators ) { - if ( activator.isActive( profile, context ) ) + try { - return true; + if ( activator.isActive( profile, context ) ) + { + return true; + } + } + catch ( ProfileActivationException e ) + { + exceptions.add( e ); } } return false; diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationException.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationException.java index 492383eb0e..a509d93acb 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationException.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationException.java @@ -57,6 +57,7 @@ public class ProfileActivationException public ProfileActivationException( String message, Profile profile ) { super( message ); + this.profile = profile; } /** diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileSelectionResult.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileSelectionResult.java new file mode 100644 index 0000000000..aea0ab5e92 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileSelectionResult.java @@ -0,0 +1,99 @@ +package org.apache.maven.model.profile; + +/* + * 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.List; + +import org.apache.maven.model.Profile; + +/** + * Collects the results of the profile selector. + * + * @author Benjamin Bentmann + */ +public class ProfileSelectionResult +{ + + private List activeProfiles; + + private List activationExceptions; + + public ProfileSelectionResult() + { + activeProfiles = new ArrayList(); + activationExceptions = new ArrayList(); + } + + /** + * Gets the profiles that have been activated. + * + * @return The profiles that have been activated, never {@code null}. + */ + public List getActiveProfiles() + { + return this.activeProfiles; + } + + /** + * Sets the profiles that have been activated. + * + * @param activeProfiles The profiles that have been activated, may be {@code null}. + * @return This result, never {@code null}. + */ + public ProfileSelectionResult setActiveProfiles( List activeProfiles ) + { + this.activeProfiles.clear(); + if ( activeProfiles != null ) + { + this.activeProfiles.addAll( activeProfiles ); + } + + return this; + } + + /** + * Gets the exceptions that have occurred during profile activation. + * + * @return The exceptions that have occurred during profile activation, never {@code null}. + */ + public List getActivationExceptions() + { + return activationExceptions; + } + + /** + * Sets the exceptions that have occurred during profile activation. + * + * @param activationExceptions The exceptions that have occurred during profile activation, may be {@code null}. + * @return This result, never {@code null}. + */ + public ProfileSelectionResult setActivationExceptions( List activationExceptions ) + { + this.activationExceptions.clear(); + if ( activationExceptions != null ) + { + this.activationExceptions.addAll( activationExceptions ); + } + + return this; + } + +} diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileSelector.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileSelector.java index 5afda026e6..2f34aff38d 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileSelector.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileSelector.java @@ -20,7 +20,6 @@ package org.apache.maven.model.profile; */ import java.util.Collection; -import java.util.List; import org.apache.maven.model.Profile; @@ -39,11 +38,8 @@ public interface ProfileSelector * @param profiles The profiles whose activation status should be determined, must not be {@code null}. * @param context The environmental context used to determine the activation status of a profile, must not be * {@code null}. - * @return The list of active profiles, never {@code null}. - * @throws ProfileActivationException If the activation status of any profile could not be determined (e.g. due to - * missing values or bad syntax). + * @return The result of the selection process, never {@code null}. */ - List getActiveProfiles( Collection profiles, ProfileActivationContext context ) - throws ProfileActivationException; + ProfileSelectionResult getActiveProfiles( Collection profiles, ProfileActivationContext context ); }