diff --git a/maven-compat/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java b/maven-compat/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java index 5b46573d81..de83facd00 100644 --- a/maven-compat/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java +++ b/maven-compat/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java @@ -22,6 +22,7 @@ import org.apache.maven.model.Model; import org.apache.maven.model.building.DefaultModelBuildingRequest; import org.apache.maven.model.building.ModelBuildingRequest; +import org.apache.maven.model.building.ModelProblemCollector; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; @@ -34,7 +35,7 @@ public class DefaultModelValidator implements ModelValidator { - + @Requirement private org.apache.maven.model.validation.ModelValidator modelValidator; @@ -45,12 +46,44 @@ public ModelValidationResult validate( Model model ) ModelBuildingRequest request = new DefaultModelBuildingRequest().setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0 ); - for ( String message : modelValidator.validateEffectiveModel( model, request ).getErrors() ) - { - result.addMessage( message ); - } + SimpleModelProblemCollector problems = new SimpleModelProblemCollector( result ); + + modelValidator.validateEffectiveModel( model, request, problems ); return result; } + private static class SimpleModelProblemCollector + implements ModelProblemCollector + { + + ModelValidationResult result; + + public SimpleModelProblemCollector( ModelValidationResult result ) + { + this.result = result; + } + + public void addError( String message ) + { + result.addMessage( message ); + } + + public void addError( String message, Exception cause ) + { + result.addMessage( message ); + } + + public void addWarning( String message ) + { + // not supported + } + + public void addWarning( String message, Exception cause ) + { + // not supported + } + + } + } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index 10030a2b06..9b919991cb 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -53,7 +53,6 @@ import org.apache.maven.model.resolution.ModelResolver; import org.apache.maven.model.resolution.UnresolvableModelException; import org.apache.maven.model.superpom.SuperPomProvider; -import org.apache.maven.model.validation.ModelValidationResult; import org.apache.maven.model.validation.ModelValidator; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; @@ -121,7 +120,7 @@ public ModelBuildingResult build( ModelBuildingRequest request ) List activeExternalProfiles = profileSelector.getActiveProfiles( request.getProfiles(), profileActivationContext, problems ); - Model inputModel = readModel( request.getModelSource(), request.getPomFile(), request, problems.getProblems() ); + Model inputModel = readModel( request.getModelSource(), request.getPomFile(), request, problems ); ModelData resultData = new ModelData( inputModel ); @@ -159,7 +158,7 @@ public ModelBuildingResult build( ModelBuildingRequest request ) configureResolver( request.getModelResolver(), tmpModel, problems ); - currentData = readParent( tmpModel, request, problems.getProblems() ); + currentData = readParent( tmpModel, request, problems ); } ModelData superData = new ModelData( getSuperModel() ); @@ -235,8 +234,7 @@ public ModelBuildingResult build( ModelBuildingRequest request, ModelBuildingRes pluginConfigurationExpander.expandPluginConfiguration( resultModel, request ); } - ModelValidationResult validationResult = modelValidator.validateEffectiveModel( resultModel, request ); - addProblems( resultModel, validationResult, problems.getProblems() ); + modelValidator.validateEffectiveModel( resultModel, request, problems ); if ( hasErrors( problems.getProblems() ) ) { @@ -247,7 +245,7 @@ public ModelBuildingResult build( ModelBuildingRequest request, ModelBuildingRes } private Model readModel( ModelSource modelSource, File pomFile, ModelBuildingRequest request, - List problems ) + DefaultModelProblemCollector problems ) throws ModelBuildingException { Model model; @@ -277,19 +275,18 @@ private Model readModel( ModelSource modelSource, File pomFile, ModelBuildingReq { problems.add( new ModelProblem( "Non-parseable POM " + modelSource.getLocation() + ": " + e.getMessage(), ModelProblem.Severity.FATAL, modelSource.getLocation(), e ) ); - throw new ModelBuildingException( problems ); + throw new ModelBuildingException( problems.getProblems() ); } catch ( IOException e ) { problems.add( new ModelProblem( "Non-readable POM " + modelSource.getLocation() + ": " + e.getMessage(), ModelProblem.Severity.FATAL, modelSource.getLocation(), e ) ); - throw new ModelBuildingException( problems ); + throw new ModelBuildingException( problems.getProblems() ); } model.setPomFile( pomFile ); - ModelValidationResult validationResult = modelValidator.validateRawModel( model, request ); - addProblems( model, validationResult, problems ); + modelValidator.validateRawModel( model, request, problems ); return model; } @@ -310,26 +307,6 @@ private boolean hasErrors( List problems ) return false; } - private void addProblems( Model model, ModelValidationResult result, List problems ) - { - if ( !result.getWarnings().isEmpty() || !result.getErrors().isEmpty() ) - { - String source = ModelProblemUtils.toSourceHint( model ); - - for ( String message : result.getWarnings() ) - { - problems.add( new ModelProblem( "Invalid POM " + source + ": " + message, - ModelProblem.Severity.WARNING, source ) ); - } - - for ( String message : result.getErrors() ) - { - problems.add( new ModelProblem( "Invalid POM " + source + ": " + message, ModelProblem.Severity.ERROR, - source ) ); - } - } - } - private ProfileActivationContext getProfileActivationContext( ModelBuildingRequest request ) { ProfileActivationContext context = new DefaultProfileActivationContext(); @@ -394,7 +371,7 @@ private Model interpolateModel( Model model, ModelBuildingRequest request, Model } } - private ModelData readParent( Model childModel, ModelBuildingRequest request, List problems ) + private ModelData readParent( Model childModel, ModelBuildingRequest request, DefaultModelProblemCollector problems ) throws ModelBuildingException { ModelData parentData; @@ -449,7 +426,8 @@ private ModelData readParent( Model childModel, ModelBuildingRequest request, Li return parentData; } - private ModelData readParentLocally( Model childModel, ModelBuildingRequest request, List problems ) + private ModelData readParentLocally( Model childModel, ModelBuildingRequest request, + DefaultModelProblemCollector problems ) throws ModelBuildingException { File pomFile = getParentPomFile( childModel ); @@ -514,7 +492,8 @@ private File getParentPomFile( Model childModel ) return pomFile; } - private ModelData readParentExternally( Model childModel, ModelBuildingRequest request, List problems ) + private ModelData readParentExternally( Model childModel, ModelBuildingRequest request, + DefaultModelProblemCollector problems ) throws ModelBuildingException { Parent parent = childModel.getParent(); @@ -543,7 +522,7 @@ private ModelData readParentExternally( Model childModel, ModelBuildingRequest r + ModelProblemUtils.toId( groupId, artifactId, version ) + " for POM " + ModelProblemUtils.toSourceHint( childModel ) + ": " + e.getMessage(), ModelProblem.Severity.FATAL, ModelProblemUtils.toSourceHint( childModel ), e ) ); - throw new ModelBuildingException( problems ); + throw new ModelBuildingException( problems.getProblems() ); } Model parentModel = readModel( modelSource, null, request, problems ); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblemCollector.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblemCollector.java index ef555003e2..bfade8de2a 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblemCollector.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblemCollector.java @@ -74,6 +74,11 @@ private String getSourceHint() return sourceHint; } + public void add( ModelProblem problem ) + { + problems.add( problem ); + } + public void addError( String message ) { problems.add( new ModelProblem( message, ModelProblem.Severity.ERROR, getSourceHint() ) ); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java index 0feaff8bc2..c59b3e478c 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java @@ -36,6 +36,7 @@ import org.apache.maven.model.Repository; import org.apache.maven.model.Resource; import org.apache.maven.model.building.ModelBuildingRequest; +import org.apache.maven.model.building.ModelProblemCollector; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.util.StringUtils; @@ -50,78 +51,72 @@ public class DefaultModelValidator private static final String ID_REGEX = "[A-Za-z0-9_\\-.]+"; - public ModelValidationResult validateRawModel( Model model, ModelBuildingRequest request ) + public void validateRawModel( Model model, ModelBuildingRequest request, ModelProblemCollector problems ) { - ModelValidationResult result = new ModelValidationResult(); - Parent parent = model.getParent(); if ( parent != null ) { - validateStringNotEmpty( "parent.groupId", result, false, parent.getGroupId() ); + validateStringNotEmpty( "parent.groupId", problems, false, parent.getGroupId() ); - validateStringNotEmpty( "parent.artifactId", result, false, parent.getArtifactId() ); + validateStringNotEmpty( "parent.artifactId", problems, false, parent.getArtifactId() ); - validateStringNotEmpty( "parent.version", result, false, parent.getVersion() ); + validateStringNotEmpty( "parent.version", problems, false, parent.getVersion() ); if ( parent.getGroupId().equals( model.getGroupId() ) && parent.getArtifactId().equals( model.getArtifactId() ) ) { - addViolation( result, false, "The parent element cannot have the same ID as the project." ); + addViolation( problems, false, "The parent element cannot have the same ID as the project." ); } } if ( request.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0 ) { - validateDependencies( result, model.getDependencies(), "dependencies.dependency", request ); + validateDependencies( problems, model.getDependencies(), "dependencies.dependency", request ); if ( model.getDependencyManagement() != null ) { - validateDependencies( result, model.getDependencyManagement().getDependencies(), + validateDependencies( problems, model.getDependencyManagement().getDependencies(), "dependencyManagement.dependencies.dependency", request ); } - validateRepositories( result, model.getRepositories(), "repositories.repository", request ); + validateRepositories( problems, model.getRepositories(), "repositories.repository", request ); - validateRepositories( result, model.getPluginRepositories(), "pluginRepositories.pluginRepository", request ); + validateRepositories( problems, model.getPluginRepositories(), "pluginRepositories.pluginRepository", request ); for ( Profile profile : model.getProfiles() ) { - validateDependencies( result, profile.getDependencies(), "profiles.profile[" + profile.getId() + validateDependencies( problems, profile.getDependencies(), "profiles.profile[" + profile.getId() + "].dependencies.dependency", request ); if ( profile.getDependencyManagement() != null ) { - validateDependencies( result, profile.getDependencyManagement().getDependencies(), + validateDependencies( problems, profile.getDependencyManagement().getDependencies(), "profiles.profile[" + profile.getId() + "].dependencyManagement.dependencies.dependency", request ); } - validateRepositories( result, profile.getRepositories(), "profiles.profile[" + profile.getId() + validateRepositories( problems, profile.getRepositories(), "profiles.profile[" + profile.getId() + "].repositories.repository", request ); - validateRepositories( result, profile.getPluginRepositories(), "profiles.profile[" + profile.getId() + validateRepositories( problems, profile.getPluginRepositories(), "profiles.profile[" + profile.getId() + "].pluginRepositories.pluginRepository", request ); } } - - return result; } - public ModelValidationResult validateEffectiveModel( Model model, ModelBuildingRequest request ) + public void validateEffectiveModel( Model model, ModelBuildingRequest request, ModelProblemCollector problems ) { - ModelValidationResult result = new ModelValidationResult(); + validateStringNotEmpty( "modelVersion", problems, false, model.getModelVersion() ); - validateStringNotEmpty( "modelVersion", result, false, model.getModelVersion() ); + validateId( "groupId", problems, model.getGroupId() ); - validateId( "groupId", result, model.getGroupId() ); + validateId( "artifactId", problems, model.getArtifactId() ); - validateId( "artifactId", result, model.getArtifactId() ); - - validateStringNotEmpty( "packaging", result, false, model.getPackaging() ); + validateStringNotEmpty( "packaging", problems, false, model.getPackaging() ); if ( !model.getModules().isEmpty() && !"pom".equals( model.getPackaging() ) ) { - addViolation( result, false, "Packaging '" + model.getPackaging() + "' is invalid. Aggregator projects " + + addViolation( problems, false, "Packaging '" + model.getPackaging() + "' is invalid. Aggregator projects " + "require 'pom' as packaging." ); } @@ -131,21 +126,21 @@ public ModelValidationResult validateEffectiveModel( Model model, ModelBuildingR if ( parent.getGroupId().equals( model.getGroupId() ) && parent.getArtifactId().equals( model.getArtifactId() ) ) { - addViolation( result, false, "The parent element cannot have the same ID as the project." ); + addViolation( problems, false, "The parent element cannot have the same ID as the project." ); } } - validateStringNotEmpty( "version", result, false, model.getVersion() ); + validateStringNotEmpty( "version", problems, false, model.getVersion() ); for ( Dependency d : model.getDependencies() ) { - validateId( "dependencies.dependency.artifactId", result, d.getArtifactId() ); + validateId( "dependencies.dependency.artifactId", problems, d.getArtifactId() ); - validateId( "dependencies.dependency.groupId", result, d.getGroupId() ); + validateId( "dependencies.dependency.groupId", problems, d.getGroupId() ); - validateStringNotEmpty( "dependencies.dependency.type", result, false, d.getType(), d.getManagementKey() ); + validateStringNotEmpty( "dependencies.dependency.type", problems, false, d.getType(), d.getManagementKey() ); - validateStringNotEmpty( "dependencies.dependency.version", result, false, d.getVersion(), + validateStringNotEmpty( "dependencies.dependency.version", problems, false, d.getVersion(), d.getManagementKey() ); if ( "system".equals( d.getScope() ) ) @@ -154,20 +149,20 @@ public ModelValidationResult validateEffectiveModel( Model model, ModelBuildingR if ( StringUtils.isEmpty( systemPath ) ) { - addViolation( result, false, "For dependency " + d + ": system-scoped dependency must specify systemPath." ); + addViolation( problems, false, "For dependency " + d + ": system-scoped dependency must specify systemPath." ); } else { if ( !new File( systemPath ).isAbsolute() ) { - addViolation( result, false, "For dependency " + d + ": system-scoped dependency must " + + addViolation( problems, false, "For dependency " + d + ": system-scoped dependency must " + "specify an absolute path systemPath." ); } } } else if ( StringUtils.isNotEmpty( d.getSystemPath() ) ) { - addViolation( result, false, + addViolation( problems, false, "For dependency " + d + ": only dependency with system scope can specify systemPath." ); } } @@ -177,10 +172,10 @@ else if ( StringUtils.isNotEmpty( d.getSystemPath() ) ) { for ( Dependency d : mgmt.getDependencies() ) { - validateSubElementStringNotEmpty( d, "dependencyManagement.dependencies.dependency.artifactId", result, + validateSubElementStringNotEmpty( d, "dependencyManagement.dependencies.dependency.artifactId", problems, d.getArtifactId() ); - validateSubElementStringNotEmpty( d, "dependencyManagement.dependencies.dependency.groupId", result, + validateSubElementStringNotEmpty( d, "dependencyManagement.dependencies.dependency.groupId", problems, d.getGroupId() ); if ( "system".equals( d.getScope() ) ) @@ -189,21 +184,21 @@ else if ( StringUtils.isNotEmpty( d.getSystemPath() ) ) if ( StringUtils.isEmpty( systemPath ) ) { - addViolation( result, false, + addViolation( problems, false, "For managed dependency " + d + ": system-scoped dependency must specify systemPath." ); } else { if ( !new File( systemPath ).isAbsolute() ) { - addViolation( result, false, "For managed dependency " + d + ": system-scoped dependency must " + + addViolation( problems, false, "For managed dependency " + d + ": system-scoped dependency must " + "specify an absolute path systemPath." ); } } } else if ( StringUtils.isNotEmpty( d.getSystemPath() ) ) { - addViolation( result, false, + addViolation( problems, false, "For managed dependency " + d + ": only dependency with system scope can specify systemPath." ); } } @@ -219,17 +214,17 @@ else if ( StringUtils.isNotEmpty( d.getSystemPath() ) ) { for ( Plugin p : build.getPlugins() ) { - validateStringNotEmpty( "build.plugins.plugin.artifactId", result, false, p.getArtifactId() ); + validateStringNotEmpty( "build.plugins.plugin.artifactId", problems, false, p.getArtifactId() ); - validateStringNotEmpty( "build.plugins.plugin.groupId", result, false, p.getGroupId() ); + validateStringNotEmpty( "build.plugins.plugin.groupId", problems, false, p.getGroupId() ); - validateStringNotEmpty( "build.plugins.plugin.version", result, warnOnMissingPluginVersion, + validateStringNotEmpty( "build.plugins.plugin.version", problems, warnOnMissingPluginVersion, p.getVersion(), p.getKey() ); } - validateResources( result, build.getResources(), "build.resources.resource" ); + validateResources( problems, build.getResources(), "build.resources.resource" ); - validateResources( result, build.getTestResources(), "build.testResources.testResource" ); + validateResources( problems, build.getTestResources(), "build.testResources.testResource" ); } Reporting reporting = model.getReporting(); @@ -237,24 +232,22 @@ else if ( StringUtils.isNotEmpty( d.getSystemPath() ) ) { for ( ReportPlugin p : reporting.getPlugins() ) { - validateStringNotEmpty( "reporting.plugins.plugin.artifactId", result, false, p.getArtifactId() ); + validateStringNotEmpty( "reporting.plugins.plugin.artifactId", problems, false, p.getArtifactId() ); - validateStringNotEmpty( "reporting.plugins.plugin.groupId", result, false, p.getGroupId() ); + validateStringNotEmpty( "reporting.plugins.plugin.groupId", problems, false, p.getGroupId() ); - validateStringNotEmpty( "reporting.plugins.plugin.version", result, warnOnMissingPluginVersion, + validateStringNotEmpty( "reporting.plugins.plugin.version", problems, warnOnMissingPluginVersion, p.getVersion(), p.getKey() ); } } - forcePluginExecutionIdCollision( model, result ); + forcePluginExecutionIdCollision( model, problems ); } - - return result; } - private boolean validateId( String fieldName, ModelValidationResult result, String id ) + private boolean validateId( String fieldName, ModelProblemCollector problems, String id ) { - if ( !validateStringNotEmpty( fieldName, result, false, id ) ) + if ( !validateStringNotEmpty( fieldName, problems, false, id ) ) { return false; } @@ -263,13 +256,13 @@ private boolean validateId( String fieldName, ModelValidationResult result, Stri boolean match = id.matches( ID_REGEX ); if ( !match ) { - addViolation( result, false, "'" + fieldName + "' with value '" + id + "' does not match a valid id pattern." ); + addViolation( problems, false, "'" + fieldName + "' with value '" + id + "' does not match a valid id pattern." ); } return match; } } - private void validateDependencies( ModelValidationResult result, List dependencies, String prefix, + private void validateDependencies( ModelProblemCollector problems, List dependencies, String prefix, ModelBuildingRequest request ) { Map index = new HashMap(); @@ -281,7 +274,7 @@ private void validateDependencies( ModelValidationResult result, List " + existing.getVersion() + " vs " + dependency.getVersion() ); } else @@ -300,16 +293,16 @@ private void validateDependencies( ModelValidationResult result, List repositories, String prefix, + private void validateRepositories( ModelProblemCollector problems, List repositories, String prefix, ModelBuildingRequest request ) { Map index = new HashMap(); for ( Repository repository : repositories ) { - validateStringNotEmpty( prefix + ".id", result, false, repository.getId() ); + validateStringNotEmpty( prefix + ".id", problems, false, repository.getId() ); - validateStringNotEmpty( prefix + ".url", result, false, repository.getUrl() ); + validateStringNotEmpty( prefix + ".url", problems, false, repository.getUrl() ); String key = repository.getId(); @@ -319,7 +312,7 @@ private void validateRepositories( ModelValidationResult result, List " + addViolation( problems, warning, "'" + prefix + ".id' must be unique: " + repository.getId() + " -> " + existing.getUrl() + " vs " + repository.getUrl() ); } else @@ -329,15 +322,15 @@ private void validateRepositories( ModelValidationResult result, List resources, String prefix ) + private void validateResources( ModelProblemCollector problems, List resources, String prefix ) { for ( Resource resource : resources ) { - validateStringNotEmpty( prefix + ".directory", result, false, resource.getDirectory() ); + validateStringNotEmpty( prefix + ".directory", problems, false, resource.getDirectory() ); } } - private void forcePluginExecutionIdCollision( Model model, ModelValidationResult result ) + private void forcePluginExecutionIdCollision( Model model, ModelProblemCollector problems ) { Build build = model.getBuild(); @@ -356,7 +349,7 @@ private void forcePluginExecutionIdCollision( Model model, ModelValidationResult } catch ( IllegalStateException collisionException ) { - addViolation( result, false, collisionException.getMessage() ); + addViolation( problems, false, collisionException.getMessage() ); } } } @@ -367,9 +360,9 @@ private void forcePluginExecutionIdCollision( Model model, ModelValidationResult // Field validation // ---------------------------------------------------------------------- - private boolean validateStringNotEmpty( String fieldName, ModelValidationResult result, boolean warning, String string ) + private boolean validateStringNotEmpty( String fieldName, ModelProblemCollector problems, boolean warning, String string ) { - return validateStringNotEmpty( fieldName, result, warning, string, null ); + return validateStringNotEmpty( fieldName, problems, warning, string, null ); } /** @@ -380,10 +373,10 @@ private boolean validateStringNotEmpty( String fieldName, ModelValidationResult *
  • string.length > 0 * */ - private boolean validateStringNotEmpty( String fieldName, ModelValidationResult result, boolean warning, + private boolean validateStringNotEmpty( String fieldName, ModelProblemCollector problems, boolean warning, String string, String sourceHint ) { - if ( !validateNotNull( fieldName, result, warning, string, sourceHint ) ) + if ( !validateNotNull( fieldName, problems, warning, string, sourceHint ) ) { return false; } @@ -395,11 +388,11 @@ private boolean validateStringNotEmpty( String fieldName, ModelValidationResult if ( sourceHint != null ) { - addViolation( result, false, "'" + fieldName + "' is missing for " + sourceHint ); + addViolation( problems, false, "'" + fieldName + "' is missing for " + sourceHint ); } else { - addViolation( result, false, "'" + fieldName + "' is missing." ); + addViolation( problems, false, "'" + fieldName + "' is missing." ); } return false; @@ -414,9 +407,9 @@ private boolean validateStringNotEmpty( String fieldName, ModelValidationResult * */ private boolean validateSubElementStringNotEmpty( Object subElementInstance, String fieldName, - ModelValidationResult result, String string ) + ModelProblemCollector problems, String string ) { - if ( !validateSubElementNotNull( subElementInstance, fieldName, result, string ) ) + if ( !validateSubElementNotNull( subElementInstance, fieldName, problems, string ) ) { return false; } @@ -426,7 +419,7 @@ private boolean validateSubElementStringNotEmpty( Object subElementInstance, Str return true; } - addViolation( result, false, "In " + subElementInstance + ":\n\n -> '" + fieldName + "' is missing." ); + addViolation( problems, false, "In " + subElementInstance + ":\n\n -> '" + fieldName + "' is missing." ); return false; } @@ -438,7 +431,7 @@ private boolean validateSubElementStringNotEmpty( Object subElementInstance, Str *
  • string != null * */ - private boolean validateNotNull( String fieldName, ModelValidationResult result, boolean warning, Object object, String sourceHint ) + private boolean validateNotNull( String fieldName, ModelProblemCollector problems, boolean warning, Object object, String sourceHint ) { if ( object != null ) { @@ -447,11 +440,11 @@ private boolean validateNotNull( String fieldName, ModelValidationResult result, if ( sourceHint != null ) { - addViolation( result, warning, "'" + fieldName + "' is missing for " + sourceHint ); + addViolation( problems, warning, "'" + fieldName + "' is missing for " + sourceHint ); } else { - addViolation( result, warning, "'" + fieldName + "' is missing." ); + addViolation( problems, warning, "'" + fieldName + "' is missing." ); } return false; @@ -465,27 +458,27 @@ private boolean validateNotNull( String fieldName, ModelValidationResult result, * */ private boolean validateSubElementNotNull( Object subElementInstance, String fieldName, - ModelValidationResult result, Object object ) + ModelProblemCollector problems, Object object ) { if ( object != null ) { return true; } - addViolation( result, false, "In " + subElementInstance + ":\n\n -> '" + fieldName + "' is missing." ); + addViolation( problems, false, "In " + subElementInstance + ":\n\n -> '" + fieldName + "' is missing." ); return false; } - private void addViolation( ModelValidationResult result, boolean warning, String message ) + private void addViolation( ModelProblemCollector problems, boolean warning, String message ) { if ( warning ) { - result.addWarning( message ); + problems.addWarning( message ); } else { - result.addError( message ); + problems.addError( message ); } } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java b/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java index d6087eb39d..58d8eeb3c7 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java @@ -21,6 +21,7 @@ import org.apache.maven.model.Model; import org.apache.maven.model.building.ModelBuildingRequest; +import org.apache.maven.model.building.ModelProblemCollector; /** * Checks the model for missing or invalid values. @@ -37,9 +38,9 @@ public interface ModelValidator * * @param model The model to validate, must not be {@code null}. * @param request The model building request that holds further settings, must not be {@code null}. - * @return The result of the validation, never {@code null}. + * @param problems The container used to collect problems that were encountered, must not be {@code null}. */ - ModelValidationResult validateRawModel( Model model, ModelBuildingRequest request ); + void validateRawModel( Model model, ModelBuildingRequest request, ModelProblemCollector problems ); /** * Checks the specified (effective) model for missing or invalid values. The effective model is fully assembled and @@ -47,8 +48,8 @@ public interface ModelValidator * * @param model The model to validate, must not be {@code null}. * @param request The model building request that holds further settings, must not be {@code null}. - * @return The result of the validation, never {@code null}. + * @param problems The container used to collect problems that were encountered, must not be {@code null}. */ - ModelValidationResult validateEffectiveModel( Model model, ModelBuildingRequest request ); + void validateEffectiveModel( Model model, ModelBuildingRequest request, ModelProblemCollector problems ); } diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java index 9f4f208fef..edad34c848 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java @@ -20,11 +20,13 @@ */ import java.io.InputStream; +import java.util.ArrayList; import java.util.List; import org.apache.maven.model.Model; import org.apache.maven.model.building.DefaultModelBuildingRequest; import org.apache.maven.model.building.ModelBuildingRequest; +import org.apache.maven.model.building.ModelProblemCollector; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.codehaus.plexus.PlexusTestCase; @@ -38,6 +40,46 @@ public class DefaultModelValidatorTest private DefaultModelValidator validator; + private static class SimpleProblemCollector + implements ModelProblemCollector + { + + private List warnings = new ArrayList(); + + private List errors = new ArrayList(); + + public void addError( String message ) + { + errors.add( message ); + } + + public void addError( String message, Exception cause ) + { + addError( message ); + } + + public void addWarning( String message ) + { + warnings.add( message ); + } + + public void addWarning( String message, Exception cause ) + { + addWarning( message ); + } + + public List getWarnings() + { + return warnings; + } + + public List getErrors() + { + return errors; + } + + } + private Model read( String pom ) throws Exception { @@ -47,26 +89,34 @@ private Model read( String pom ) return new MavenXpp3Reader().read( is ); } - private ModelValidationResult validate( String pom ) + private SimpleProblemCollector validate( String pom ) throws Exception { return validateEffective( pom, ModelBuildingRequest.VALIDATION_LEVEL_STRICT ); } - private ModelValidationResult validateEffective( String pom, int level ) + private SimpleProblemCollector validateEffective( String pom, int level ) throws Exception { ModelBuildingRequest request = new DefaultModelBuildingRequest().setValidationLevel( level ); - return validator.validateEffectiveModel( read( pom ), request ); + SimpleProblemCollector problems = new SimpleProblemCollector(); + + validator.validateEffectiveModel( read( pom ), request, problems ); + + return problems; } - private ModelValidationResult validateRaw( String pom, int level ) + private SimpleProblemCollector validateRaw( String pom, int level ) throws Exception { ModelBuildingRequest request = new DefaultModelBuildingRequest().setValidationLevel( level ); - return validator.validateRawModel( read( pom ), request ); + SimpleProblemCollector problems = new SimpleProblemCollector(); + + validator.validateRawModel( read( pom ), request, problems ); + + return problems; } @Override @@ -87,7 +137,7 @@ protected void tearDown() super.tearDown(); } - private void assertViolations( ModelValidationResult result, int errors, int warnings ) + private void assertViolations( SimpleProblemCollector result, int errors, int warnings ) { assertEquals( errors, result.getErrors().size() ); assertEquals( warnings, result.getWarnings().size() ); @@ -96,7 +146,7 @@ private void assertViolations( ModelValidationResult result, int errors, int war public void testMissingModelVersion() throws Exception { - ModelValidationResult result = validate( "missing-modelVersion-pom.xml" ); + SimpleProblemCollector result = validate( "missing-modelVersion-pom.xml" ); assertViolations( result, 1, 0 ); @@ -106,7 +156,7 @@ public void testMissingModelVersion() public void testMissingArtifactId() throws Exception { - ModelValidationResult result = validate( "missing-artifactId-pom.xml" ); + SimpleProblemCollector result = validate( "missing-artifactId-pom.xml" ); assertViolations( result, 1, 0 ); @@ -116,7 +166,7 @@ public void testMissingArtifactId() public void testMissingGroupId() throws Exception { - ModelValidationResult result = validate( "missing-groupId-pom.xml" ); + SimpleProblemCollector result = validate( "missing-groupId-pom.xml" ); assertViolations( result, 1, 0 ); @@ -126,7 +176,7 @@ public void testMissingGroupId() public void testInvalidIds() throws Exception { - ModelValidationResult result = validate( "invalid-ids-pom.xml" ); + SimpleProblemCollector result = validate( "invalid-ids-pom.xml" ); assertViolations( result, 2, 0 ); @@ -138,7 +188,7 @@ public void testInvalidIds() public void testMissingType() throws Exception { - ModelValidationResult result = validate( "missing-type-pom.xml" ); + SimpleProblemCollector result = validate( "missing-type-pom.xml" ); assertViolations( result, 1, 0 ); @@ -148,7 +198,7 @@ public void testMissingType() public void testMissingVersion() throws Exception { - ModelValidationResult result = validate( "missing-version-pom.xml" ); + SimpleProblemCollector result = validate( "missing-version-pom.xml" ); assertViolations( result, 1, 0 ); @@ -158,7 +208,7 @@ public void testMissingVersion() public void testInvalidAggregatorPackaging() throws Exception { - ModelValidationResult result = validate( "invalid-aggregator-packaging-pom.xml" ); + SimpleProblemCollector result = validate( "invalid-aggregator-packaging-pom.xml" ); assertViolations( result, 1, 0 ); @@ -168,7 +218,7 @@ public void testInvalidAggregatorPackaging() public void testMissingDependencyArtifactId() throws Exception { - ModelValidationResult result = validate( "missing-dependency-artifactId-pom.xml" ); + SimpleProblemCollector result = validate( "missing-dependency-artifactId-pom.xml" ); assertViolations( result, 1, 0 ); @@ -178,7 +228,7 @@ public void testMissingDependencyArtifactId() public void testMissingDependencyGroupId() throws Exception { - ModelValidationResult result = validate( "missing-dependency-groupId-pom.xml" ); + SimpleProblemCollector result = validate( "missing-dependency-groupId-pom.xml" ); assertViolations( result, 1, 0 ); @@ -188,7 +238,7 @@ public void testMissingDependencyGroupId() public void testMissingDependencyVersion() throws Exception { - ModelValidationResult result = validate( "missing-dependency-version-pom.xml" ); + SimpleProblemCollector result = validate( "missing-dependency-version-pom.xml" ); assertViolations( result, 1, 0 ); @@ -198,7 +248,7 @@ public void testMissingDependencyVersion() public void testMissingDependencyManagementArtifactId() throws Exception { - ModelValidationResult result = validate( "missing-dependency-mgmt-artifactId-pom.xml" ); + SimpleProblemCollector result = validate( "missing-dependency-mgmt-artifactId-pom.xml" ); assertViolations( result, 1, 0 ); @@ -209,7 +259,7 @@ public void testMissingDependencyManagementArtifactId() public void testMissingDependencyManagementGroupId() throws Exception { - ModelValidationResult result = validate( "missing-dependency-mgmt-groupId-pom.xml" ); + SimpleProblemCollector result = validate( "missing-dependency-mgmt-groupId-pom.xml" ); assertViolations( result, 1, 0 ); @@ -220,7 +270,7 @@ public void testMissingDependencyManagementGroupId() public void testMissingAll() throws Exception { - ModelValidationResult result = validate( "missing-1-pom.xml" ); + SimpleProblemCollector result = validate( "missing-1-pom.xml" ); assertViolations( result, 4, 0 ); @@ -236,7 +286,7 @@ public void testMissingAll() public void testMissingPluginArtifactId() throws Exception { - ModelValidationResult result = validate( "missing-plugin-artifactId-pom.xml" ); + SimpleProblemCollector result = validate( "missing-plugin-artifactId-pom.xml" ); assertViolations( result, 1, 0 ); @@ -246,7 +296,7 @@ public void testMissingPluginArtifactId() public void testMissingPluginVersion() throws Exception { - ModelValidationResult result = + SimpleProblemCollector result = validateEffective( "missing-plugin-version-pom.xml", ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_1 ); assertViolations( result, 1, 0 ); @@ -262,7 +312,7 @@ public void testMissingPluginVersion() public void testMissingRepositoryId() throws Exception { - ModelValidationResult result = + SimpleProblemCollector result = validateRaw( "missing-repository-id-pom.xml", ModelBuildingRequest.VALIDATION_LEVEL_STRICT ); assertViolations( result, 4, 0 ); @@ -279,7 +329,7 @@ public void testMissingRepositoryId() public void testMissingResourceDirectory() throws Exception { - ModelValidationResult result = validate( "missing-resource-directory-pom.xml" ); + SimpleProblemCollector result = validate( "missing-resource-directory-pom.xml" ); assertViolations( result, 2, 0 );