o Reworked handling of model problems to allow for easier reusage across all the other model diddling components

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@800413 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-08-03 14:58:05 +00:00
parent 0373dfb46b
commit 5e36879dd4
7 changed files with 155 additions and 118 deletions

View File

@ -32,9 +32,7 @@ import org.apache.maven.model.Build;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginContainer;
import org.apache.maven.model.building.ModelBuildingProblems;
import org.apache.maven.model.building.ModelProblem;
import org.apache.maven.model.building.ModelProblemUtils;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.merge.MavenModelMerger;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
@ -54,7 +52,7 @@ public class DefaultLifecycleBindingsInjector
@Requirement
private LifecycleExecutor lifecycle;
public void injectLifecycleBindings( Model model, ModelBuildingProblems problems )
public void injectLifecycleBindings( Model model, ModelProblemCollector problems )
{
String packaging = model.getPackaging();
@ -62,9 +60,7 @@ public class DefaultLifecycleBindingsInjector
if ( defaultPlugins == null )
{
String source = ModelProblemUtils.toSourceHint( model );
problems.add( new ModelProblem( "Invalid POM " + source + ": Unknown packaging: " + packaging,
ModelProblem.Severity.ERROR, source ) );
problems.addError( "Unknown packaging: " + packaging );
}
else if ( !defaultPlugins.isEmpty() )
{

View File

@ -115,10 +115,11 @@ public class DefaultModelBuilder
{
DefaultModelBuildingResult result = new DefaultModelBuildingResult();
DefaultModelBuildingProblems problems = new DefaultModelBuildingProblems( null );
DefaultModelProblemCollector problems = new DefaultModelProblemCollector( null );
ProfileActivationContext profileActivationContext = getProfileActivationContext( request );
problems.setSourceHint( "(external profiles)" );
List<Profile> activeExternalProfiles = getActiveExternalProfiles( request, profileActivationContext, problems );
Model inputModel = readModel( request.getModelSource(), request.getPomFile(), request, problems.getProblems() );
@ -136,6 +137,8 @@ public class DefaultModelBuilder
Model rawModel = ModelUtils.cloneModel( tmpModel );
currentData.setRawModel( rawModel );
problems.setSourceHint( tmpModel );
modelNormalizer.mergeDuplicates( tmpModel, request );
List<Profile> activePomProfiles = getActivePomProfiles( rawModel, profileActivationContext, problems );
@ -168,6 +171,8 @@ public class DefaultModelBuilder
Model resultModel = resultData.getModel();
problems.setSourceHint( resultModel );
resultModel = interpolateModel( resultModel, request, problems );
resultData.setModel( resultModel );
@ -203,7 +208,8 @@ public class DefaultModelBuilder
{
Model resultModel = result.getEffectiveModel();
DefaultModelBuildingProblems problems = new DefaultModelBuildingProblems( result.getProblems() );
DefaultModelProblemCollector problems = new DefaultModelProblemCollector( result.getProblems() );
problems.setSourceHint( resultModel );
modelPathTranslator.alignToBaseDirectory( resultModel, resultModel.getProjectDirectory(), request );
@ -338,43 +344,42 @@ public class DefaultModelBuilder
}
private List<Profile> getActiveExternalProfiles( ModelBuildingRequest request, ProfileActivationContext context,
ModelBuildingProblems problems )
ModelProblemCollector problems )
{
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(), ModelProblem.Severity.ERROR, "(external profiles)", e ) );
problems.addError( "Invalid activation condition for external profile " + e.getProfile().getId() + ": "
+ e.getMessage(), e );
}
return result.getActiveProfiles();
}
private List<Profile> getActivePomProfiles( Model model, ProfileActivationContext context,
ModelBuildingProblems problems )
ModelProblemCollector problems )
{
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 "
+ ModelProblemUtils.toSourceHint( model ) + ": " + e.getMessage(),
ModelProblem.Severity.ERROR, ModelProblemUtils.toSourceHint( model ), e ) );
problems.addError( "Invalid activation condition for project profile " + e.getProfile().getId() + ": "
+ e.getMessage(), e );
}
return result.getActiveProfiles();
}
private void configureResolver( ModelResolver modelResolver, Model model, ModelBuildingProblems problems )
private void configureResolver( ModelResolver modelResolver, Model model, DefaultModelProblemCollector problems )
{
if ( modelResolver == null )
{
return;
}
problems.setSourceHint( model );
List<Repository> repositories = model.getRepositories();
Collections.reverse( repositories );
@ -386,9 +391,7 @@ public class DefaultModelBuilder
}
catch ( InvalidRepositoryException e )
{
problems.add( new ModelProblem( "Invalid repository " + repository.getId() + " in POM "
+ ModelProblemUtils.toSourceHint( model ) + ": " + e.getMessage(), ModelProblem.Severity.ERROR,
ModelProblemUtils.toSourceHint( model ), e ) );
problems.addError( "Invalid repository " + repository.getId() + ": " + e.getMessage(), e );
}
}
}
@ -403,7 +406,7 @@ public class DefaultModelBuilder
}
}
private Model interpolateModel( Model model, ModelBuildingRequest request, ModelBuildingProblems problems )
private Model interpolateModel( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
{
try
{
@ -413,8 +416,7 @@ public class DefaultModelBuilder
}
catch ( ModelInterpolationException e )
{
problems.add( new ModelProblem( "Invalid expression in POM " + ModelProblemUtils.toSourceHint( model )
+ ": " + e.getMessage(), ModelProblem.Severity.ERROR, ModelProblemUtils.toSourceHint( model ), e ) );
problems.addError( "Invalid expression: " + e.getMessage(), e );
return model;
}
@ -585,7 +587,8 @@ public class DefaultModelBuilder
return ModelUtils.cloneModel( superPomProvider.getSuperModel( "4.0.0" ) );
}
private void importDependencyManagement( Model model, ModelBuildingRequest request, ModelBuildingProblems problems )
private void importDependencyManagement( Model model, ModelBuildingRequest request,
DefaultModelProblemCollector problems )
{
DependencyManagement depMngt = model.getDependencyManagement();
@ -634,10 +637,8 @@ public class DefaultModelBuilder
}
catch ( UnresolvableModelException e )
{
problems.add( new ModelProblem( "Non-resolvable import POM "
+ ModelProblemUtils.toId( groupId, artifactId, version ) + " for POM "
+ ModelProblemUtils.toSourceHint( model ) + ": " + e.getMessage(), ModelProblem.Severity.ERROR,
ModelProblemUtils.toSourceHint( model ), e ) );
problems.addError( "Non-resolvable import POM "
+ ModelProblemUtils.toId( groupId, artifactId, version ) + ": " + e.getMessage(), e );
continue;
}
@ -657,11 +658,11 @@ public class DefaultModelBuilder
}
catch ( ModelBuildingException e )
{
problems.addAll( e.getProblems() );
problems.getProblems().addAll( e.getProblems() );
continue;
}
problems.addAll( importResult.getProblems() );
problems.getProblems().addAll( importResult.getProblems() );
Model importModel = importResult.getEffectiveModel();
@ -709,7 +710,7 @@ public class DefaultModelBuilder
return null;
}
private void fireBuildExtensionsAssembled( Model model, ModelBuildingRequest request, ModelBuildingProblems problems )
private void fireBuildExtensionsAssembled( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
throws ModelBuildingException
{
if ( request.getModelBuildingListeners().isEmpty() )
@ -727,8 +728,7 @@ public class DefaultModelBuilder
}
catch ( Exception e )
{
problems.add( new ModelProblem( "Invalid build extensions: " + e.getMessage(),
ModelProblem.Severity.ERROR, ModelProblemUtils.toSourceHint( model ), e ) );
problems.addError( "Invalid build extensions: " + e.getMessage(), e );
}
}
}

View File

@ -1,70 +0,0 @@
package org.apache.maven.model.building;
/*
* 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.Collection;
import java.util.List;
/**
* Collects the problems that were encountered during model building.
*
* @author Benjamin Bentmann
*/
class DefaultModelBuildingProblems
implements ModelBuildingProblems
{
private List<ModelProblem> problems;
public DefaultModelBuildingProblems( List<ModelProblem> problems )
{
this.problems = ( problems != null ) ? problems : new ArrayList<ModelProblem>();
}
public List<ModelProblem> getProblems()
{
return problems;
}
public void add( ModelProblem problem )
{
if ( problem == null )
{
throw new IllegalArgumentException( "model problem missing" );
}
problems.add( problem );
}
public void addAll( Collection<ModelProblem> problems )
{
if ( problems == null )
{
throw new IllegalArgumentException( "model problems missing" );
}
for ( ModelProblem problem : problems )
{
add( problem );
}
}
}

View File

@ -0,0 +1,97 @@
package org.apache.maven.model.building;
/*
* 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.Model;
/**
* Collects problems that are encountered during model building. The primary purpose of this component is to account for
* the fact that the problem reporter has/should not have information about the calling context and hence cannot provide
* an expressive source hint for the model problem. Instead, the source hint is configured by the model builder before
* it delegates to other components that potentially encounter problems. Then, the problem reporter can focus on
* providing a simple error message, leaving the donkey work of creating a nice model problem to this component.
*
* @author Benjamin Bentmann
*/
class DefaultModelProblemCollector
implements ModelProblemCollector
{
private List<ModelProblem> problems;
private String sourceHint;
private Model sourceModel;
public DefaultModelProblemCollector( List<ModelProblem> problems )
{
this.problems = ( problems != null ) ? problems : new ArrayList<ModelProblem>();
}
public List<ModelProblem> getProblems()
{
return problems;
}
public void setSourceHint( String sourceHint )
{
this.sourceHint = sourceHint;
this.sourceModel = null;
}
public void setSourceHint( Model sourceModel )
{
this.sourceModel = sourceModel;
this.sourceHint = null;
}
private String getSourceHint()
{
if ( sourceHint == null && sourceModel != null )
{
sourceHint = ModelProblemUtils.toSourceHint( sourceModel );
}
return sourceHint;
}
public void addError( String message )
{
problems.add( new ModelProblem( message, ModelProblem.Severity.ERROR, getSourceHint() ) );
}
public void addError( String message, Exception cause )
{
problems.add( new ModelProblem( message, ModelProblem.Severity.ERROR, getSourceHint(), cause ) );
}
public void addWarning( String message )
{
problems.add( new ModelProblem( message, ModelProblem.Severity.WARNING, getSourceHint() ) );
}
public void addWarning( String message, Exception cause )
{
problems.add( new ModelProblem( message, ModelProblem.Severity.WARNING, getSourceHint(), cause ) );
}
}

View File

@ -19,28 +19,42 @@ package org.apache.maven.model.building;
* under the License.
*/
import java.util.Collection;
/**
* Collects the problems that are encountered during model building.
* Collects problems that are encountered during model building.
*
* @author Benjamin Bentmann
*/
public interface ModelBuildingProblems
public interface ModelProblemCollector
{
/**
* Adds the specified problem.
* Adds the specified error.
*
* @param problem The problem to add, must not be {@code null}.
* @param message The detail message of the error, may be {@code null}.
*/
void add( ModelProblem problem );
void addError( String message );
/**
* Adds the specified problems.
* Adds the specified error.
*
* @param problems The problems to add, must not be {@code null} nor contain {@code null} elements.
* @param message The detail message of the error, may be {@code null}.
* @param cause The cause of the error, may be {@code null}.
*/
void addAll( Collection<ModelProblem> problems );
void addError( String message, Exception cause );
/**
* Adds the specified warning.
*
* @param message The detail message of the warning, may be {@code null}.
*/
void addWarning( String message );
/**
* Adds the specified warning.
*
* @param message The detail message of the warning, may be {@code null}.
* @param cause The cause of the warning, may be {@code null}.
*/
void addWarning( String message, Exception cause );
}

View File

@ -28,7 +28,7 @@ import org.apache.maven.model.Model;
*
* @author Benjamin Bentmann
*/
public class ModelProblemUtils
class ModelProblemUtils
{
/**

View File

@ -20,7 +20,7 @@ package org.apache.maven.model.plugin;
*/
import org.apache.maven.model.Model;
import org.apache.maven.model.building.ModelBuildingProblems;
import org.apache.maven.model.building.ModelProblemCollector;
/**
* Handles injection of plugin executions induced by the lifecycle bindings for a packaging.
@ -37,6 +37,6 @@ public interface LifecycleBindingsInjector
* <code>null</code>.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
*/
void injectLifecycleBindings( Model model, ModelBuildingProblems problems );
void injectLifecycleBindings( Model model, ModelProblemCollector problems );
}