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 c60c446f44..4e429a4aa0 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 @@ -139,8 +139,7 @@ public class DefaultModelValidator validateStringNotEmpty( "version", problems, false, model.getVersion() ); - boolean warnOnBadBoolean = request.getValidationLevel() < ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_0; - boolean warnOnBadDependencyScope = request.getValidationLevel() < ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_0; + boolean warnOnly = request.getValidationLevel() < ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_0; for ( Dependency d : model.getDependencies() ) { @@ -178,12 +177,15 @@ public class DefaultModelValidator if ( request.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0 ) { - validateBoolean( "dependencies.dependency.optional", problems, warnOnBadBoolean, d.getOptional(), + validateVersion( "dependencies.dependency.version", problems, warnOnly, d.getVersion(), + d.getManagementKey() ); + + validateBoolean( "dependencies.dependency.optional", problems, warnOnly, d.getOptional(), d.getManagementKey() ); /* * TODO: Extensions like Flex Mojos use custom scopes like "merged", "internal", "external", etc. In - * order to don't break backward-compat with those, only warn but don't error our. + * order to don't break backward-compat with those, only warn but don't error out. */ validateEnum( "dependencies.dependency.scope", problems, true, d.getScope(), d.getManagementKey(), "provided", "compile", "runtime", "test", "system" ); @@ -227,8 +229,8 @@ public class DefaultModelValidator if ( request.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0 ) { - validateBoolean( "dependencyManagement.dependencies.dependency.optional", problems, - warnOnBadBoolean, d.getOptional(), d.getManagementKey() ); + validateBoolean( "dependencyManagement.dependencies.dependency.optional", problems, warnOnly, + d.getOptional(), d.getManagementKey() ); } } } @@ -250,16 +252,16 @@ public class DefaultModelValidator validateStringNotEmpty( "build.plugins.plugin.version", problems, warnOnMissingPluginVersion, p.getVersion(), p.getKey() ); - validateBoolean( "build.plugins.plugin.inherited", problems, warnOnBadBoolean, p.getInherited(), + validateBoolean( "build.plugins.plugin.inherited", problems, warnOnly, p.getInherited(), p.getKey() ); - validateBoolean( "build.plugins.plugin.extensions", problems, warnOnBadBoolean, p.getExtensions(), + validateBoolean( "build.plugins.plugin.extensions", problems, warnOnly, p.getExtensions(), p.getKey() ); for ( Dependency d : p.getDependencies() ) { validateEnum( "build.plugins.plugin[" + p.getKey() + "].dependencies.dependency.scope", - problems, warnOnBadDependencyScope, d.getScope(), d.getManagementKey(), + problems, warnOnly, d.getScope(), d.getManagementKey(), "compile", "runtime", "system" ); } } @@ -578,11 +580,12 @@ public class DefaultModelValidator if ( sourceHint != null ) { - addViolation( problems, warning, "'" + fieldName + "' must be 'true' or 'false' for " + sourceHint ); + addViolation( problems, warning, "'" + fieldName + "' must be 'true' or 'false' for " + sourceHint + + " but is '" + string + "'." ); } else { - addViolation( problems, warning, "'" + fieldName + "' must be 'true' or 'false'." ); + addViolation( problems, warning, "'" + fieldName + "' must be 'true' or 'false' but is '" + string + "'." ); } return false; @@ -605,11 +608,39 @@ public class DefaultModelValidator if ( sourceHint != null ) { - addViolation( problems, warning, "'" + fieldName + "' must be one of " + values + " for " + sourceHint ); + addViolation( problems, warning, "'" + fieldName + "' must be one of " + values + " for " + sourceHint + + " but is '" + string + "'." ); } else { - addViolation( problems, warning, "'" + fieldName + "' must be one of " + values ); + addViolation( problems, warning, "'" + fieldName + "' must be one of " + values + " but is '" + string + + "'." ); + } + + return false; + } + + private boolean validateVersion( String fieldName, ModelProblemCollector problems, boolean warning, String string, + String sourceHint ) + { + if ( string == null || string.length() <= 0 ) + { + return true; + } + + if ( !hasExpression( string ) ) + { + return true; + } + + if ( sourceHint != null ) + { + addViolation( problems, warning, "'" + fieldName + "' must be a valid version for " + sourceHint + + " but is '" + string + "'." ); + } + else + { + addViolation( problems, warning, "'" + fieldName + "' must be a valid version but is '" + string + "'." ); } return false; 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 ecd5d03f02..75e46f19a8 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 @@ -334,4 +334,14 @@ public class DefaultModelValidatorTest assertTrue( result.getWarnings().get( 1 ).contains( "test:g" ) ); } + public void testBadDependencyVersion() + throws Exception + { + SimpleProblemCollector result = validate( "bad-dependency-version.xml" ); + + assertViolations( result, 1, 0 ); + + assertTrue( result.getErrors().get( 0 ).contains( "test:b" ) ); + } + } diff --git a/maven-model-builder/src/test/resources/poms/validation/bad-dependency-version.xml b/maven-model-builder/src/test/resources/poms/validation/bad-dependency-version.xml new file mode 100644 index 0000000000..dc632d7c28 --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/validation/bad-dependency-version.xml @@ -0,0 +1,38 @@ + + + + 4.0.0 + aid + gid + 0.1 + + + + test + a + 0.2 + + + test + b + ${missing.property} + + +